From 0121876ed0c885692814b51e282b5d8fd28d968d Mon Sep 17 00:00:00 2001 From: Benjami <benjami94@gmail.com> Date: Fri, 9 Jun 2023 07:48:59 +0200 Subject: [PATCH] Add admin into communiy energy --- energy_communities/models/res_company.py | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/energy_communities/models/res_company.py b/energy_communities/models/res_company.py index b3b71ff06..c55dccc3a 100644 --- a/energy_communities/models/res_company.py +++ b/energy_communities/models/res_company.py @@ -128,6 +128,39 @@ class ResCompany(models.Model): admins_user_ids.append(role_line.user_id.id) return any([user in admins_user_ids for user in company_user_ids]) + def add_ce_admin(self, user): + if self.hierarchy_level != COMMUNITY_HIERARCHY: + raise UserError(_("Only a CE can have CE admins")) + + role = self.env["res.users.role"].sudo().search([{ + "code", "=", "role_ce_admin" + }]) + current_role = self.env["res.users.role.line"].sudo().search([ + ("user_id", "=", user.id), + ("active", "=", True), + ("allowed_company_ids", "=", self.id) # It's M2M, = is okey? + ]) + if current_role and len(current_role) > 1: + raise UserError(_("Error: This user have multiple roles for this company")) + if current_role and len(current_role.company_ids) > 1: + raise UserError(_("Error: One role line can't have multiple users")) + if current_role and current_role.role.code != "role_ce_member": + raise UserError(_("Error: You can't remove {} role").format(current_role.role.code)) + + if current_role: + current_role.write({"role_id": role}) + else: + self.env["res.users.role.line"].sudo().create({ + "user_id": user.id, + "active": True, + "role_id": role, + "community_ids": (6, 0, self.id), + }) + user = self.env["res.users"].sudo().browse(user.id) + user.write({ + "community_ids": (6, 0, self.id) + }) + def get_ce_members(self, domain_key="in_kc_and_active"): domains_dict = { "in_kc_and_active": [ -- GitLab