Browse Source

[IMP] base_user_role_profile: black, isort, prettier

14.0
Pierrick Brun 4 years ago
committed by KevinKhao
parent
commit
b032978510
  1. 3
      base_user_role_profile/data/data.xml
  2. 8
      base_user_role_profile/models/ir_http.py
  3. 5
      base_user_role_profile/models/role.py
  4. 10
      base_user_role_profile/models/user.py
  5. 82
      base_user_role_profile/static/src/js/switch_profile_menu.js
  6. 18
      base_user_role_profile/static/src/xml/templates.xml
  7. 36
      base_user_role_profile/tests/test_user_role.py
  8. 14
      base_user_role_profile/views/assets.xml
  9. 16
      base_user_role_profile/views/profile.xml
  10. 13
      base_user_role_profile/views/role.xml
  11. 35
      base_user_role_profile/views/user.xml
  12. 1
      setup/base_user_role_profile/odoo/addons/base_user_role_profile
  13. 6
      setup/base_user_role_profile/setup.py

3
base_user_role_profile/data/data.xml

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="default_profile" model="res.users.profile">
<field name="name">No profile</field>

8
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

5
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):

10
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

82
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 = '<li class="bg-info">' +
_t('Tap on the list to change profile') + '</li>';
profilesList =
'<li class="bg-info">' +
_t("Tap on the list to change profile") +
"</li>";
} 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 = '<i class="fa fa-check mr8"></i>';
} else {
a = '<span style="margin-right: 24px;"/>';
}
profilesList += '<a role="menuitem" href="#" class="dropdown-item" data-menu="profile" data-profile-id="' +
profile[0] + '">' + a + profile[1] + '</a>';
profilesList +=
'<a role="menuitem" href="#" class="dropdown-item" data-menu="profile" data-profile-id="' +
profile[0] +
'">' +
a +
profile[1] +
"</a>";
});
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;
});

18
base_user_role_profile/static/src/xml/templates.xml

@ -1,12 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<templates id="template" xml:space="preserve">
<t t-name="SwitchProfileMenu">
<li class="o_switch_profile_menu">
<a role="button" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false" href="#" aria-label="Dropdown menu" title="Dropdown menu">
<span t-attf-class="#{widget.isMobile ? 'fa fa-building-o' : 'oe_topbar_name'}"/>
<a
role="button"
class="dropdown-toggle"
data-toggle="dropdown"
aria-expanded="false"
href="#"
aria-label="Dropdown menu"
title="Dropdown menu"
>
<span
t-attf-class="#{widget.isMobile ? 'fa fa-building-o' : 'oe_topbar_name'}"
/>
</a>
<div class="dropdown-menu dropdown-menu-right" role="menu"/>
<div class="dropdown-menu dropdown-menu-right" role="menu" />
</li>
</t>

36
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)

14
base_user_role_profile/views/assets.xml

@ -1,9 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<template id="assets_backend" name="User Role Profile assets" inherit_id="web.assets_backend">
<template
id="assets_backend"
name="User Role Profile assets"
inherit_id="web.assets_backend"
>
<xpath expr="." position="inside">
<script type="text/javascript" src="/base_user_role_profile/static/src/js/switch_profile_menu.js"/>
<script
type="text/javascript"
src="/base_user_role_profile/static/src/js/switch_profile_menu.js"
/>
</xpath>
</template>
</odoo>

16
base_user_role_profile/views/profile.xml

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!--License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).-->
<odoo>
@ -9,8 +9,8 @@
<form>
<sheet>
<group>
<field name="name"/>
<field name="user_ids"/>
<field name="name" />
<field name="user_ids" />
</group>
</sheet>
</form>
@ -22,7 +22,7 @@
<field name="model">res.users.profile</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
<field name="name" />
</tree>
</field>
</record>
@ -32,11 +32,13 @@
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.users.profile</field>
<field name="view_type">form</field>
<field name="view_id" ref="view_res_users_profile_tree"/>
<field name="view_id" ref="view_res_users_profile_tree" />
</record>
<menuitem id="menu_action_res_users_profile_tree"
<menuitem
id="menu_action_res_users_profile_tree"
parent="base.menu_users"
action="action_res_users_profile_tree"/>
action="action_res_users_profile_tree"
/>
</odoo>

13
base_user_role_profile/views/role.xml

@ -1,17 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!--License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).-->
<odoo>
<record id="view_res_users_form_inherit" model="ir.ui.view">
<field name="name">res.users.role.form.inherit</field>
<field name="model">res.users.role</field>
<field name="inherit_id" ref="base_user_role.view_res_users_role_form"/>
<field name="inherit_id" ref="base_user_role.view_res_users_role_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='line_ids']//field[@name='company_id']" position="after">
<field name="profile_id"/>
<xpath
expr="//field[@name='line_ids']//field[@name='company_id']"
position="after"
>
<field name="profile_id" />
</xpath>
<xpath expr="//field[@name='group_id']" position="after">
<field name="profile_id"/>
<field name="profile_id" />
</xpath>
</field>
</record>

35
base_user_role_profile/views/user.xml

@ -1,21 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!--License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).-->
<odoo>
<record id="view_res_users_form_inherit_profile" model="ir.ui.view">
<field name="name">res.users.form.inherit</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="inherit_id" ref="base.view_users_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='role_line_ids']" position="before">
<group>
<field name="profile_ids" widget="many2many_tags" attrs="{'readonly': True}"/>
<field name="profile_id" domain="[('id', 'in', profile_ids)]"
options="{'no_create_edit': True, 'no_open': True}"/>
<field
name="profile_ids"
widget="many2many_tags"
attrs="{'readonly': True}"
/>
<field
name="profile_id"
domain="[('id', 'in', profile_ids)]"
options="{'no_create_edit': True, 'no_open': True}"
/>
</group>
</xpath>
<xpath expr="//field[@name='role_line_ids']//field[@name='company_id']" position="after">
<field name="profile_id"/>
<xpath
expr="//field[@name='role_line_ids']//field[@name='company_id']"
position="after"
>
<field name="profile_id" />
</xpath>
</field>
</record>
@ -23,13 +33,14 @@
<record id="view_res_users_form_show_company" model="ir.ui.view">
<field name="name">res.users.form.inherit</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base_user_role.view_res_users_form_inherit"/>
<field name="inherit_id" ref="base_user_role.view_res_users_form_inherit" />
<field name="arch" type="xml">
<xpath expr="//field[@name='role_line_ids']//field[@name='company_id']" position="attributes">
<attribute name="groups" eval=""/>
<xpath
expr="//field[@name='role_line_ids']//field[@name='company_id']"
position="attributes"
>
<attribute name="groups" eval="" />
</xpath>
</field>
</record>
</odoo>

1
setup/base_user_role_profile/odoo/addons/base_user_role_profile

@ -0,0 +1 @@
../../../../base_user_role_profile

6
setup/base_user_role_profile/setup.py

@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Loading…
Cancel
Save