Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ different template users for each website #993

Open
wants to merge 1 commit into
base: 11.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion web_website/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Copyright 2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2020 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
# License MIT (https://opensource.org/licenses/MIT).
{
"name": """Website Switcher in Backend""",
"summary": """Technical module to switch Websites in Backend similarly to Company Switcher""",
"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": "[email protected]",
Expand Down
4 changes: 4 additions & 0 deletions web_website/doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions web_website/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from . import res_config
from . import res_users
from . import ir_http
from . import website
30 changes: 29 additions & 1 deletion web_website/models/res_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2020 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
# License MIT (https://opensource.org/licenses/MIT).
from odoo import fields, models
from odoo import api, fields, models


class WebWebsiteConfigSettings(models.TransientModel):
Expand All @@ -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()
33 changes: 33 additions & 0 deletions web_website/models/website.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2020 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
# 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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auth_signup is not in depedencies of web_website module

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right, forgot to test this case. Website module is in dependencies and it uses such structures but they won't lead to an error.
https://github.com/odoo/odoo/blob/11.0/addons/website/models/res_config_settings.py#L94
Should i do a new auth_signup_multi_website module then? It is a more general approach and allows to use built-in methods

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new module auth_signup_multi_website should be fine.
FYI: odoo 13 already support this feature

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
)