Skip to content
Snippets Groups Projects
Commit 180f93bd authored by Benjami's avatar Benjami Committed by konykon
Browse files

User email must be unique :envelope:

parent 61c91282
No related branches found
No related tags found
3 merge requests!239Draft: [REL] energy_communities: dependency energy_selfconsumption bump to,!227[REL] Release 16/10/23,!142Feature/assign ce admin
This commit is part of merge request !142. Comments created here will be created in the context of that merge request.
import logging
from odoo.exceptions import ValidationError
from odoo import SUPERUSER_ID, api, fields, models
......@@ -28,6 +29,15 @@ class ResPartner(models.Model):
new_partner = super().create(vals)
return new_partner
@api.constrains('email')
def _check_email(self):
count_users = self.env['res.partner'].search_count([
('email', '=', self.email),
('user_ids', '!=', False)
])
if self.email and count_users >= 1:
raise ValidationError('The email already registered, please use another email!')
def cron_update_company_ids_from_user(self):
partner_with_users = self.search(
[("user_ids", "!=", False), ("user_ids.id", "!=", SUPERUSER_ID)]
......
......@@ -2,7 +2,7 @@
from faker import Faker
from mock import patch
from odoo.tests import common
from odoo.exceptions import UserError
from odoo.exceptions import UserError, ValidationError
faker = Faker(locale='es_ES')
......@@ -229,3 +229,25 @@ class TestResUsers(common.TransactionCase):
self.assertEqual(len(rl_coord), 2)
self.assertIn(self.coordination, self.coord_admin.company_ids)
self.assertIn(other_coord, self.coord_admin.company_ids)
@patch("odoo.addons.energy_communities.models.res_users.ResUsers.create_users_on_keycloak")
@patch("odoo.addons.energy_communities.models.res_users.ResUsers.send_reset_password_mail")
def test__email_must_be_unique(
self, reset_password_mocked, create_kc_user_mocked
):
self.users_model.create_energy_community_base_user(
vat=faker.vat_id(),
first_name="Tom",
last_name="Bombadil",
lang_code="en_US",
email="random.mail@somcomunitats.coop",
)
with self.assertRaises(ValidationError):
self.users_model.create_energy_community_base_user(
vat=faker.vat_id(),
first_name="Timmy",
last_name="Box",
lang_code="en_US",
email="random.mail@somcomunitats.coop",
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment