From b032978510bebdc6ed1917e1ce96012bc87d16e6 Mon Sep 17 00:00:00 2001 From: Pierrick Brun Date: Fri, 18 Dec 2020 14:05:31 +0100 Subject: [PATCH] [IMP] base_user_role_profile: black, isort, prettier --- base_user_role_profile/data/data.xml | 3 +- base_user_role_profile/models/ir_http.py | 8 +- base_user_role_profile/models/role.py | 5 +- base_user_role_profile/models/user.py | 10 +-- .../static/src/js/switch_profile_menu.js | 82 ++++++++++--------- .../static/src/xml/templates.xml | 18 +++- .../tests/test_user_role.py | 36 ++------ base_user_role_profile/views/assets.xml | 14 +++- base_user_role_profile/views/profile.xml | 18 ++-- base_user_role_profile/views/role.xml | 13 +-- base_user_role_profile/views/user.xml | 35 +++++--- .../odoo/addons/base_user_role_profile | 1 + setup/base_user_role_profile/setup.py | 6 ++ 13 files changed, 136 insertions(+), 113 deletions(-) create mode 120000 setup/base_user_role_profile/odoo/addons/base_user_role_profile create mode 100644 setup/base_user_role_profile/setup.py diff --git a/base_user_role_profile/data/data.xml b/base_user_role_profile/data/data.xml index 42920ebbd..ea7778aaf 100644 --- a/base_user_role_profile/data/data.xml +++ b/base_user_role_profile/data/data.xml @@ -1,5 +1,4 @@ - - + No profile diff --git a/base_user_role_profile/models/ir_http.py b/base_user_role_profile/models/ir_http.py index db316b36c..90de7a045 100644 --- a/base_user_role_profile/models/ir_http.py +++ b/base_user_role_profile/models/ir_http.py @@ -9,9 +9,7 @@ class Http(models.AbstractModel): def session_info(self): # pragma: no cover result = super().session_info() user = request.env.user - allowed_profiles = [ - (profile.id, profile.name) for profile in user.profile_ids - ] + allowed_profiles = [(profile.id, profile.name) for profile in user.profile_ids] if len(allowed_profiles) > 1: current_profile = (user.profile_id.id, user.profile_id.name) result["user_profiles"] = { @@ -20,7 +18,5 @@ class Http(models.AbstractModel): } else: result["user_profiles"] = False - result["profile_id"] = ( - user.profile_id.id if request.session.uid else None - ) + result["profile_id"] = user.profile_id.id if request.session.uid else None return result diff --git a/base_user_role_profile/models/role.py b/base_user_role_profile/models/role.py index 558cb6e30..7c7777a71 100644 --- a/base_user_role_profile/models/role.py +++ b/base_user_role_profile/models/role.py @@ -5,7 +5,10 @@ from odoo import fields, models class ResUsersRole(models.Model): _inherit = "res.users.role" - profile_id = fields.Many2one("res.users.profile", "Profile",) + profile_id = fields.Many2one( + "res.users.profile", + "Profile", + ) class ResUsersRoleLine(models.Model): diff --git a/base_user_role_profile/models/user.py b/base_user_role_profile/models/user.py index 536ce52e5..fb8719b91 100644 --- a/base_user_role_profile/models/user.py +++ b/base_user_role_profile/models/user.py @@ -15,7 +15,8 @@ class ResUsers(models.Model): ) profile_ids = fields.Many2many( - "res.users.profile", string="Currently allowed profiles", + "res.users.profile", + string="Currently allowed profiles", ) def _get_action_root_menu(self): @@ -55,15 +56,12 @@ class ResUsers(models.Model): def _get_applicable_roles(self): res = super()._get_applicable_roles() res = res.filtered( - lambda r: not r.profile_id - or (r.profile_id.id == r.user_id.profile_id.id) + lambda r: not r.profile_id or (r.profile_id.id == r.user_id.profile_id.id) ) return res def _update_profile_id(self): - default_profile = self.env.ref( - "base_user_role_profile.default_profile" - ) + default_profile = self.env.ref("base_user_role_profile.default_profile") if not self.profile_ids: if self.profile_id != default_profile: self.profile_id = default_profile diff --git a/base_user_role_profile/static/src/js/switch_profile_menu.js b/base_user_role_profile/static/src/js/switch_profile_menu.js index e6fe6a8ac..033029d09 100644 --- a/base_user_role_profile/static/src/js/switch_profile_menu.js +++ b/base_user_role_profile/static/src/js/switch_profile_menu.js @@ -1,79 +1,85 @@ -odoo.define('web.SwitchProfileMenu', function(require) { +odoo.define("web.SwitchProfileMenu", function (require) { "use strict"; - var config = require('web.config'); - var core = require('web.core'); - var session = require('web.session'); - var SystrayMenu = require('web.SystrayMenu'); - var Widget = require('web.Widget') + var config = require("web.config"); + var core = require("web.core"); + var session = require("web.session"); + var SystrayMenu = require("web.SystrayMenu"); + var Widget = require("web.Widget"); var _t = core._t; var SwitchProfileMenu = Widget.extend({ - template: 'SwitchProfileMenu', + template: "SwitchProfileMenu", events: { - 'click .dropdown-item[data-menu]': '_onClick', + "click .dropdown-item[data-menu]": "_onClick", }, - init: function() { + init: function () { this._super.apply(this, arguments); this.isMobile = config.device.isMobile; this._onClick = _.debounce(this._onClick, 1500, true); }, - willStart: function() { + willStart: function () { return session.user_profiles ? this._super() : $.Deferred().reject(); }, - start: function() { - var profilesList = ''; + start: function () { + var profilesList = ""; if (this.isMobile) { - profilesList = '
  • ' + - _t('Tap on the list to change profile') + '
  • '; + profilesList = + '
  • ' + + _t("Tap on the list to change profile") + + "
  • "; } else { - this.$('.oe_topbar_name').text(session.user_profiles.current_profile[1]); + this.$(".oe_topbar_name").text( + session.user_profiles.current_profile[1] + ); } - _.each(session.user_profiles.allowed_profiles, function(profile) { - var a = ''; + _.each(session.user_profiles.allowed_profiles, function (profile) { + var a = ""; if (profile[0] == session.user_profiles.current_profile[0]) { a = ''; } else { a = ''; } - profilesList += '' + a + profile[1] + ''; + profilesList += + '' + + a + + profile[1] + + ""; }); - this.$('.dropdown-menu').html(profilesList); + this.$(".dropdown-menu").html(profilesList); return this._super(); }, - _onClick: function(ev) { + _onClick: function (ev) { var self = this; ev.preventDefault(); - var profileID = $(ev.currentTarget).data('profile-id'); + var profileID = $(ev.currentTarget).data("profile-id"); // We use this instead of the location.reload() because permissions change // and we might land on a menu that we don't have permissions for. Thus it // is cleaner to reload any root menu this._rpc({ - model: 'res.users', - method: 'action_profile_change', - args: [ - [session.uid], { - 'profile_id': profileID - } - ], - }) - .done( - function(result) { - self.trigger_up('do_action', { - action: result, - }) - } - ) + model: "res.users", + method: "action_profile_change", + args: [ + [session.uid], + { + profile_id: profileID, + }, + ], + }).done(function (result) { + self.trigger_up("do_action", { + action: result, + }); + }); }, }); SystrayMenu.Items.push(SwitchProfileMenu); return SwitchProfileMenu; - }); diff --git a/base_user_role_profile/static/src/xml/templates.xml b/base_user_role_profile/static/src/xml/templates.xml index 2cff8d529..4d7162594 100644 --- a/base_user_role_profile/static/src/xml/templates.xml +++ b/base_user_role_profile/static/src/xml/templates.xml @@ -1,12 +1,22 @@ - +
  • - -
  • diff --git a/base_user_role_profile/tests/test_user_role.py b/base_user_role_profile/tests/test_user_role.py index 8d989956b..dbfd9ad13 100644 --- a/base_user_role_profile/tests/test_user_role.py +++ b/base_user_role_profile/tests/test_user_role.py @@ -30,12 +30,8 @@ class TestUserProfile(TransactionCase): } self.user_id = self.user_model.create(user_vals) - self.profile1_id = self.env["res.users.profile"].create( - {"name": "profile1"} - ) - self.profile2_id = self.env["res.users.profile"].create( - {"name": "profile2"} - ) + self.profile1_id = self.env["res.users.profile"].create({"name": "profile1"}) + self.profile2_id = self.env["res.users.profile"].create({"name": "profile2"}) # role 1 self.group_user_id = self.env.ref("base.group_user") @@ -47,16 +43,12 @@ class TestUserProfile(TransactionCase): # role 3 self.group_erp_manager_id = self.env.ref("base.group_erp_manager") - self.group_partner_manager_id = self.env.ref( - "base.group_partner_manager" - ) + self.group_partner_manager_id = self.env.ref("base.group_partner_manager") # roles 1 and 2 have a profile, role 3 no profile vals = { "name": "ROLE_1", - "implied_ids": [ - (6, 0, [self.group_user_id.id, self.group_no_one_id.id]) - ], + "implied_ids": [(6, 0, [self.group_user_id.id, self.group_no_one_id.id])], "profile_id": self.profile1_id.id, } self.role1_id = self.role_model.create(vals) @@ -97,23 +89,17 @@ class TestUserProfile(TransactionCase): self.user_id.write({"role_line_ids": [(0, 0, line1_vals)]}) line2_vals = {"role_id": self.role2_id.id, "user_id": self.user_id.id} self.user_id.write({"role_line_ids": [(0, 0, line2_vals)]}) - self.assertEqual( - self.user_id.profile_ids, self.profile1_id + self.profile2_id - ) + self.assertEqual(self.user_id.profile_ids, self.profile1_id + self.profile2_id) self.assertEqual(self.user_id.profile_id, self.profile1_id) self.user_id.action_profile_change({"profile_id": self.profile1_id.id}) - user_group_ids = sorted( - set([group.id for group in self.user_id.groups_id]) - ) + user_group_ids = sorted({group.id for group in self.user_id.groups_id}) expected_group_ids = sorted(set(self.role1_group_ids)) self.assertEqual(user_group_ids, expected_group_ids) self.user_id.action_profile_change({"profile_id": self.profile2_id.id}) - user_group_ids = sorted( - set([group.id for group in self.user_id.groups_id]) - ) + user_group_ids = sorted({group.id for group in self.user_id.groups_id}) expected_group_ids = sorted(set(self.role2_group_ids)) self.assertEqual(user_group_ids, expected_group_ids) @@ -146,16 +132,12 @@ class TestUserProfile(TransactionCase): self.user_id.write({"role_line_ids": [(0, 0, line2_vals)]}) self.assertEqual(self.user_id.profile_ids, self.profile1_id) - user_group_ids = sorted( - set([group.id for group in self.user_id.groups_id]) - ) + user_group_ids = sorted({group.id for group in self.user_id.groups_id}) expected_group_ids = sorted(set(self.role1_group_ids)) self.assertEqual(user_group_ids, expected_group_ids) self.user_id.company_id = self.company2 self.assertEqual(self.user_id.profile_ids, self.profile2_id) - user_group_ids = sorted( - set([group.id for group in self.user_id.groups_id]) - ) + user_group_ids = sorted({group.id for group in self.user_id.groups_id}) expected_group_ids = sorted(set(self.role2_group_ids)) self.assertEqual(user_group_ids, expected_group_ids) diff --git a/base_user_role_profile/views/assets.xml b/base_user_role_profile/views/assets.xml index 8368b9568..d5683038e 100644 --- a/base_user_role_profile/views/assets.xml +++ b/base_user_role_profile/views/assets.xml @@ -1,9 +1,15 @@ - - + -