mirror of https://github.com/muk-it/muk_base
MuK IT GmbH
6 years ago
4 changed files with 209 additions and 1 deletions
-
3muk_utils/__manifest__.py
-
1muk_utils/models/__init__.py
-
110muk_utils/models/groups.py
-
96muk_utils/views/groups.xml
@ -0,0 +1,110 @@ |
|||||
|
################################################################################### |
||||
|
# |
||||
|
# Copyright (C) 2017 MuK IT GmbH |
||||
|
# |
||||
|
# This program is free software: you can redistribute it and/or modify |
||||
|
# it under the terms of the GNU Affero General Public License as |
||||
|
# published by the Free Software Foundation, either version 3 of the |
||||
|
# License, or (at your option) any later version. |
||||
|
# |
||||
|
# This program is distributed in the hope that it will be useful, |
||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
# GNU Affero General Public License for more details. |
||||
|
# |
||||
|
# You should have received a copy of the GNU Affero General Public License |
||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
# |
||||
|
################################################################################### |
||||
|
|
||||
|
from odoo import models, fields, api |
||||
|
|
||||
|
class Groups(models.AbstractModel): |
||||
|
|
||||
|
_name = 'muk_utils.groups' |
||||
|
|
||||
|
_parent_store = True |
||||
|
_parent_name = "parent_group" |
||||
|
|
||||
|
#---------------------------------------------------------- |
||||
|
# Database |
||||
|
#---------------------------------------------------------- |
||||
|
|
||||
|
name = fields.Char( |
||||
|
string="Group Name", |
||||
|
required=True, |
||||
|
translate=True) |
||||
|
|
||||
|
parent_path = fields.Char( |
||||
|
string="Parent Path", |
||||
|
index=True) |
||||
|
|
||||
|
count_users = fields.Integer( |
||||
|
compute='_compute_users', |
||||
|
string="Users", |
||||
|
store=True) |
||||
|
|
||||
|
@api.model |
||||
|
def _add_magic_fields(self): |
||||
|
super(Groups, self)._add_magic_fields() |
||||
|
def add(name, field): |
||||
|
if name not in self._fields: |
||||
|
self._add_field(name, field) |
||||
|
base, model = self._name.split(".") |
||||
|
add('parent_group', fields.Many2one( |
||||
|
_module=base, |
||||
|
comodel_name=self._name, |
||||
|
string='Parent Group', |
||||
|
ondelete='cascade', |
||||
|
auto_join=True, |
||||
|
index=True, |
||||
|
automatic=True)) |
||||
|
add('child_groups', fields.One2many( |
||||
|
_module=base, |
||||
|
comodel_name=self._name, |
||||
|
inverse_name='parent_group', |
||||
|
string='Child Groups', |
||||
|
automatic=True)) |
||||
|
add('groups', fields.Many2many( |
||||
|
_module=base, |
||||
|
comodel_name='res.groups', |
||||
|
relation='%s_groups_rel' % (self._table), |
||||
|
column1='gid', |
||||
|
column2='rid', |
||||
|
string='Groups', |
||||
|
automatic=True)) |
||||
|
add('explicit_users', fields.Many2many( |
||||
|
_module=base, |
||||
|
comodel_name='res.users', |
||||
|
relation='%s_explicit_users_rel' % (self._table), |
||||
|
column1='gid', |
||||
|
column2='uid', |
||||
|
string='Explicit Users', |
||||
|
automatic=True)) |
||||
|
add('users', fields.Many2many( |
||||
|
_module=base, |
||||
|
comodel_name='res.users', |
||||
|
relation='%s_users_rel' % (self._table), |
||||
|
column1='gid', |
||||
|
column2='uid', |
||||
|
string='Users', |
||||
|
compute='_compute_users', |
||||
|
store=True, |
||||
|
automatic=True)) |
||||
|
|
||||
|
_sql_constraints = [ |
||||
|
('name_uniq', 'unique (name)', 'The name of the group must be unique!') |
||||
|
] |
||||
|
|
||||
|
#---------------------------------------------------------- |
||||
|
# Read, View |
||||
|
#---------------------------------------------------------- |
||||
|
|
||||
|
@api.depends('parent_group.users', 'groups', 'groups.users', 'explicit_users') |
||||
|
def _compute_users(self): |
||||
|
print(self) |
||||
|
for record in self: |
||||
|
users = record.mapped('groups.users') |
||||
|
users |= record.mapped('explicit_users') |
||||
|
users |= record.mapped('parent_group.users') |
||||
|
record.update({'users': users, 'count_users': len(users)}) |
@ -0,0 +1,96 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
|
||||
|
<!-- |
||||
|
Copyright (C) 2017 MuK IT GmbH |
||||
|
|
||||
|
This program is free software: you can redistribute it and/or modify |
||||
|
it under the terms of the GNU Affero General Public License as |
||||
|
published by the Free Software Foundation, either version 3 of the |
||||
|
License, or (at your option) any later version. |
||||
|
|
||||
|
This program is distributed in the hope that it will be useful, |
||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
|
GNU Affero General Public License for more details. |
||||
|
|
||||
|
You should have received a copy of the GNU Affero General Public License |
||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
--> |
||||
|
|
||||
|
<odoo> |
||||
|
|
||||
|
<record id="view_groups_tree" model="ir.ui.view"> |
||||
|
<field name="name">muk_utils_groups.tree</field> |
||||
|
<field name="model">muk_utils.groups</field> |
||||
|
<field name="arch" type="xml"> |
||||
|
<tree string="Groups"> |
||||
|
<field name="name" /> |
||||
|
<field name="count_users" /> |
||||
|
</tree> |
||||
|
</field> |
||||
|
</record> |
||||
|
|
||||
|
<record id="view_groups_form" model="ir.ui.view"> |
||||
|
<field name="name">muk_utils_groups.form</field> |
||||
|
<field name="model">muk_utils.groups</field> |
||||
|
<field name="arch" type="xml"> |
||||
|
<form string="Group"> |
||||
|
<sheet> |
||||
|
<div class="oe_title"> |
||||
|
<label for="name" class="oe_edit_only" /> |
||||
|
<h1> |
||||
|
<field name="name" /> |
||||
|
</h1> |
||||
|
</div> |
||||
|
<group name="group" string="Group"> |
||||
|
<group> |
||||
|
<field name="parent_group" /> |
||||
|
</group> |
||||
|
<group> |
||||
|
<field name="count_users" /> |
||||
|
</group> |
||||
|
</group> |
||||
|
<notebook> |
||||
|
<page name="users" string="Users"> |
||||
|
<field name="users"> |
||||
|
<tree string="Users"> |
||||
|
<field name="name" /> |
||||
|
<field name="login" /> |
||||
|
<field name="lang" /> |
||||
|
<field name="login_date" /> |
||||
|
</tree> |
||||
|
</field> |
||||
|
</page> |
||||
|
<page name="groups" string="Groups"> |
||||
|
<field name="groups"> |
||||
|
<tree string="Groups"> |
||||
|
<field name="name" /> |
||||
|
<field name="comment" /> |
||||
|
</tree> |
||||
|
</field> |
||||
|
</page> |
||||
|
<page name="extra_users" string="Explicit Users"> |
||||
|
<field name="explicit_users"> |
||||
|
<tree string="Explicit Users"> |
||||
|
<field name="name" /> |
||||
|
<field name="login" /> |
||||
|
<field name="lang" /> |
||||
|
<field name="login_date" /> |
||||
|
</tree> |
||||
|
</field> |
||||
|
</page> |
||||
|
<page name="childs" string="Child Groups"> |
||||
|
<field name="child_groups"> |
||||
|
<tree string="Groups"> |
||||
|
<field name="name" /> |
||||
|
<field name="count_users" /> |
||||
|
</tree> |
||||
|
</field> |
||||
|
</page> |
||||
|
</notebook> |
||||
|
</sheet> |
||||
|
</form> |
||||
|
</field> |
||||
|
</record> |
||||
|
|
||||
|
</odoo> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue