From 91ba67da3b49511e85bbc06cc6b9b69926796f0b Mon Sep 17 00:00:00 2001 From: KolushovAlexandr Date: Thu, 28 May 2020 18:00:47 +0500 Subject: [PATCH] :zap: different template users for each website --- web_website/__manifest__.py | 3 ++- web_website/doc/changelog.rst | 4 ++++ web_website/models/__init__.py | 1 + web_website/models/res_config.py | 30 ++++++++++++++++++++++++++++- web_website/models/website.py | 33 ++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 web_website/models/website.py diff --git a/web_website/__manifest__.py b/web_website/__manifest__.py index 6443c945a4..13e614341a 100644 --- a/web_website/__manifest__.py +++ b/web_website/__manifest__.py @@ -1,4 +1,5 @@ # Copyright 2018 Ivan Yelizariev +# Copyright 2020 Kolushov Alexandr # License MIT (https://opensource.org/licenses/MIT). { "name": """Website Switcher in Backend""", @@ -6,7 +7,7 @@ "category": "Hidden", # "live_test_url": "", "images": [], - "version": "11.0.3.0.4", + "version": "11.0.3.1.0", "application": False, "author": "IT-Projects LLC, Ivan Yelizariev", "support": "apps@itpp.dev", diff --git a/web_website/doc/changelog.rst b/web_website/doc/changelog.rst index f5c1355bfb..689f897b24 100644 --- a/web_website/doc/changelog.rst +++ b/web_website/doc/changelog.rst @@ -1,3 +1,7 @@ +`3.1.0` +------- +- **New:** Different template users for each website + `3.0.4` ------- - **Fix:** Incorrect return data in get_multi in case of 'many2one' field, id instead of a record diff --git a/web_website/models/__init__.py b/web_website/models/__init__.py index c32e1fae39..9e98b6a2c8 100644 --- a/web_website/models/__init__.py +++ b/web_website/models/__init__.py @@ -5,3 +5,4 @@ from . import res_config from . import res_users from . import ir_http +from . import website diff --git a/web_website/models/res_config.py b/web_website/models/res_config.py index 78e02e3454..75fe7f13bd 100644 --- a/web_website/models/res_config.py +++ b/web_website/models/res_config.py @@ -1,6 +1,7 @@ # Copyright 2018 Ivan Yelizariev +# Copyright 2020 Kolushov Alexandr # License MIT (https://opensource.org/licenses/MIT). -from odoo import fields, models +from odoo import api, fields, models class WebWebsiteConfigSettings(models.TransientModel): @@ -12,3 +13,30 @@ class WebWebsiteConfigSettings(models.TransientModel): help="Show Website Switcher in backend", implied_group="web_website.group_multi_website", ) + + @api.multi + def open_template_user(self): + IrConfigParameter = self.env["ir.config_parameter"].sudo() + IrProperty = self.env["ir.property"].sudo() + ResUsers = self.env["res.users"].sudo() + + # search for all properties for that case + param_id = IrConfigParameter.search( + [("key", "=", "auth_signup.template_user_id")], limit=1 + ) + field = self.env["ir.model.fields"].search( + [("model", "=", "ir.config_parameter"), ("name", "=", "value")], limit=1 + ) + prop_ids = IrProperty.search( + [ + ("fields_id", "=", field.id), + ("res_id", "=", "{},{}".format(IrConfigParameter._name, param_id.id)), + ] + ) + + website_id = ResUsers.browse(self._context["uid"]).backend_website_id + # Is it needed?? if param_id.value in prop_ids.filtered(lambda p: not p.website_id).mapped('value_text') and ... + if website_id not in prop_ids.mapped("website_id"): + # Template user was not created/set for current website + website_id.create_new_template_user_id() + return super(WebWebsiteConfigSettings, self).open_template_user() diff --git a/web_website/models/website.py b/web_website/models/website.py new file mode 100644 index 0000000000..110a6cf66e --- /dev/null +++ b/web_website/models/website.py @@ -0,0 +1,33 @@ +# Copyright 2020 Kolushov Alexandr +# License MIT (https://opensource.org/licenses/MIT). + +from odoo import api, models + + +class Website(models.Model): + _inherit = "website" + + @api.model + def create(self, vals): + website = super(Website, self).create(vals) + website.create_new_template_user_id() + return website + + @api.multi + def create_new_template_user_id(self): + IrConfigParameter = self.env["ir.config_parameter"].sudo() + user_id = IrConfigParameter.get_param("auth_signup.template_user_id", "False") + user_id = self.env["res.users"].sudo().browse(user_id and int(user_id)) + for record in self: + company_id = record.company_id.id + new_user_id = user_id.sudo().copy( + { + "login": "{} - {}".format(user_id.login, record.name), + "company_id": company_id, + "company_ids": [(6, 0, [company_id])], + "backend_website_ids": [(6, 0, record.ids)], + } + ) + IrConfigParameter.with_context(dict(website_id=record.id)).set_param( + "auth_signup.template_user_id", new_user_id.id + )