Skip to content

Commit

Permalink
[IMP] base_user_role: black, isort
Browse files Browse the repository at this point in the history
  • Loading branch information
sebalix committed Jan 30, 2020
1 parent 4f7df6d commit 7615a99
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ line_length=88
known_odoo=odoo
known_odoo_addons=odoo.addons
sections=FUTURE,STDLIB,THIRDPARTY,ODOO,ODOO_ADDONS,FIRSTPARTY,LOCALFOLDER
known_third_party=mock,psycopg2
known_third_party=mock,psycopg2,setuptools
32 changes: 15 additions & 17 deletions base_user_role/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@


{
'name': 'User roles',
'version': '13.0.1.0.0',
'category': 'Tools',
'author': 'ABF OSIELL, Odoo Community Association (OCA)',
'license': 'LGPL-3',
'maintainers': ['sebalix', 'jcdrubay', 'novawish'],
'website': 'https://github.com/OCA/server-backend',
'depends': [
'base',
"name": "User roles",
"version": "13.0.1.0.0",
"category": "Tools",
"author": "ABF OSIELL, Odoo Community Association (OCA)",
"license": "LGPL-3",
"maintainers": ["sebalix", "jcdrubay", "novawish"],
"website": "https://github.com/OCA/server-backend",
"depends": ["base"],
"data": [
"security/ir.model.access.csv",
"data/ir_cron.xml",
"data/ir_module_category.xml",
"views/role.xml",
"views/user.xml",
],
'data': [
'security/ir.model.access.csv',
'data/ir_cron.xml',
'data/ir_module_category.xml',
'views/role.xml',
'views/user.xml',
],
'installable': True,
"installable": True,
}
82 changes: 46 additions & 36 deletions base_user_role/models/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,41 @@
import datetime
import logging

from odoo import _, api, fields, models
from odoo import SUPERUSER_ID
from odoo import SUPERUSER_ID, _, api, fields, models
from odoo.exceptions import ValidationError


_logger = logging.getLogger(__name__)


class ResUsersRole(models.Model):
_name = 'res.users.role'
_inherits = {'res.groups': 'group_id'}
_name = "res.users.role"
_inherits = {"res.groups": "group_id"}
_description = "User role"

group_id = fields.Many2one(
comodel_name='res.groups', required=True, ondelete='cascade',
readonly=True, string="Associated group")
comodel_name="res.groups",
required=True,
ondelete="cascade",
readonly=True,
string="Associated group",
)
line_ids = fields.One2many(
comodel_name='res.users.role.line',
inverse_name='role_id', string="Role lines")
comodel_name="res.users.role.line", inverse_name="role_id", string="Role lines"
)
user_ids = fields.One2many(
comodel_name='res.users', string="Users list",
compute='_compute_user_ids')
comodel_name="res.users", string="Users list", compute="_compute_user_ids"
)
group_category_id = fields.Many2one(
related='group_id.category_id',
default=lambda cls: cls.env.ref(
'base_user_role.ir_module_category_role').id,
related="group_id.category_id",
default=lambda cls: cls.env.ref("base_user_role.ir_module_category_role").id,
string="Associated category",
help="Associated group's category")
help="Associated group's category",
)

@api.depends('line_ids.user_id')
@api.depends("line_ids.user_id")
def _compute_user_ids(self):
for role in self:
role.user_ids = role.line_ids.mapped('user_id')
role.user_ids = role.line_ids.mapped("user_id")

@api.model
def create(self, vals):
Expand All @@ -49,14 +51,14 @@ def write(self, vals):
return res

def unlink(self):
users = self.mapped('user_ids')
users = self.mapped("user_ids")
res = super(ResUsersRole, self).unlink()
users.set_groups_from_roles(force=True)
return res

def update_users(self):
"""Update all the users concerned by the roles identified by `ids`."""
users = self.mapped('user_ids')
users = self.mapped("user_ids")
users.set_groups_from_roles()
return True

Expand All @@ -67,33 +69,41 @@ def cron_update_users(self):


class ResUsersRoleLine(models.Model):
_name = 'res.users.role.line'
_description = 'Users associated to a role'
_name = "res.users.role.line"
_description = "Users associated to a role"

role_id = fields.Many2one(
comodel_name='res.users.role', required=True, string="Role",
ondelete='cascade')
comodel_name="res.users.role", required=True, string="Role", ondelete="cascade"
)
user_id = fields.Many2one(
comodel_name='res.users', required=True, string="User",
domain=[('id', '!=', SUPERUSER_ID)], ondelete='cascade')
comodel_name="res.users",
required=True,
string="User",
domain=[("id", "!=", SUPERUSER_ID)],
ondelete="cascade",
)
date_from = fields.Date("From")
date_to = fields.Date("To")
is_enabled = fields.Boolean("Enabled", compute='_compute_is_enabled')
is_enabled = fields.Boolean("Enabled", compute="_compute_is_enabled")
company_id = fields.Many2one(
'res.company', 'Company',
default=lambda self: self.env.user.company_id)
"res.company", "Company", default=lambda self: self.env.user.company_id
)

@api.constrains('user_id', 'company_id')
@api.constrains("user_id", "company_id")
def _check_company(self):
for record in self:
if (record.company_id and
record.company_id != record.user_id.company_id and
record.company_id not in record.user_id.company_ids):
if (
record.company_id
and record.company_id != record.user_id.company_id
and record.company_id not in record.user_id.company_ids
):
raise ValidationError(
_('User "{}" does not have access to the company "{}"')
.format(record.user_id.name, record.company_id.name))
_('User "{}" does not have access to the company "{}"').format(
record.user_id.name, record.company_id.name
)
)

@api.depends('date_from', 'date_to')
@api.depends("date_from", "date_to")
def _compute_is_enabled(self):
today = datetime.date.today()
for role_line in self:
Expand All @@ -108,7 +118,7 @@ def _compute_is_enabled(self):
role_line.is_enabled = False

def unlink(self):
users = self.mapped('user_id')
users = self.mapped("user_id")
res = super(ResUsersRoleLine, self).unlink()
users.set_groups_from_roles(force=True)
return res
56 changes: 30 additions & 26 deletions base_user_role/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,38 @@


class ResUsers(models.Model):
_inherit = 'res.users'
_inherit = "res.users"

role_line_ids = fields.One2many(
comodel_name='res.users.role.line',
inverse_name='user_id',
comodel_name="res.users.role.line",
inverse_name="user_id",
string="Role lines",
default=lambda self: self._default_role_lines()
default=lambda self: self._default_role_lines(),
)
role_ids = fields.One2many(
comodel_name='res.users.role', string="Roles",
compute='_compute_role_ids')
comodel_name="res.users.role", string="Roles", compute="_compute_role_ids"
)

@api.model
def _default_role_lines(self):
default_user = self.env.ref(
'base.default_user', raise_if_not_found=False)
default_user = self.env.ref("base.default_user", raise_if_not_found=False)
default_values = []
if default_user:
for role_line in default_user.role_line_ids:
default_values.append({
'role_id': role_line.role_id.id,
'date_from': role_line.date_from,
'date_to': role_line.date_to,
'is_enabled': role_line.is_enabled,
})
default_values.append(
{
"role_id": role_line.role_id.id,
"date_from": role_line.date_from,
"date_to": role_line.date_to,
"is_enabled": role_line.is_enabled,
}
)
return default_values

@api.depends('role_line_ids.role_id')
@api.depends("role_line_ids.role_id")
def _compute_role_ids(self):
for user in self:
user.role_ids = user.role_line_ids.mapped('role_id')
user.role_ids = user.role_line_ids.mapped("role_id")

@api.model
def create(self, vals):
Expand All @@ -49,8 +50,9 @@ def write(self, vals):

def _get_enabled_roles(self):
return self.role_line_ids.filtered(
lambda rec: rec.is_enabled and
(not rec.company_id or rec.company_id == rec.user_id.company_id))
lambda rec: rec.is_enabled
and (not rec.company_id or rec.company_id == rec.user_id.company_id)
)

def set_groups_from_roles(self, force=False):
"""Set (replace) the groups following the roles defined on users.
Expand All @@ -60,26 +62,28 @@ def set_groups_from_roles(self, force=False):
role_groups = {}
# We obtain all the groups associated to each role first, so that
# it is faster to compare later with each user's groups.
for role in self.mapped('role_line_ids.role_id'):
role_groups[role] = list(set(
role.group_id.ids + role.implied_ids.ids +
role.trans_implied_ids.ids))
for role in self.mapped("role_line_ids.role_id"):
role_groups[role] = list(
set(
role.group_id.ids
+ role.implied_ids.ids
+ role.trans_implied_ids.ids
)
)
for user in self:
if not user.role_line_ids and not force:
continue
group_ids = []
for role_line in user._get_enabled_roles():
role = role_line.role_id
group_ids += role_groups[role]
group_ids = list(set(group_ids)) # Remove duplicates IDs
group_ids = list(set(group_ids)) # Remove duplicates IDs
groups_to_add = list(set(group_ids) - set(user.groups_id.ids))
groups_to_remove = list(set(user.groups_id.ids) - set(group_ids))
to_add = [(4, gr) for gr in groups_to_add]
to_remove = [(3, gr) for gr in groups_to_remove]
groups = to_remove + to_add
if groups:
vals = {
'groups_id': groups,
}
vals = {"groups_id": groups}
super(ResUsers, user).write(vals)
return True
2 changes: 1 addition & 1 deletion base_user_role/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ Add users (with dates or not):

.. figure:: /OCA/server-backend/12.0/base_user_role/static/description/role_users.png
:width: 80 %
:align: center
:align: center
Loading

0 comments on commit 7615a99

Please sign in to comment.