forked from itpp-labs/access-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
68 lines (58 loc) · 3.38 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from openerp import fields, api, models
from lxml import etree
from lxml.builder import E
from openerp.tools.translate import _
from openerp.addons.base.res.res_users import name_boolean_group, name_selection_groups
class GroupsView(models.Model):
_inherit = 'res.groups'
is_custom_group = fields.Boolean("Custom Group", help="show group at the top of Access Rights tab in user form")
@api.model
def _update_user_groups_view(self):
# call super to make module compatible with other modules (e.g. access_restricted)
super(GroupsView, self)._update_user_groups_view()
if self._context.get('install_mode'):
# use installation/admin language for translatable names in the view
user_context = self.env['res.users'].context_get()
self = self.with_context(**user_context)
# We have to try-catch this, because at first init the view does not
# exist but we are already creating some basic groups.
view = self.env.ref('base.user_groups_view', raise_if_not_found=False)
if view and view.exists() and view._name == 'ir.ui.view':
group_no_one = view.env.ref('base.group_no_one')
xml1, xml2 = [], []
xml1.append(E.separator(string=_('Application'), colspan="2"))
xml3 = []
xml3.append(E.separator(string=_('Custom User Groups'), colspan="4"))
for app, kind, gs in self.get_groups_by_application():
xml = None
custom = False
if kind == 'selection' and any([g.is_custom_group for g in gs]) or all([g.is_custom_group for g in gs]):
xml = xml3
custom = True
# hide groups in category 'Hidden' (except to group_no_one)
attrs = {'groups': 'base.group_no_one'} if app and (app.xml_id == 'base.module_category_hidden' or app.xml_id == 'base.module_category_extra') and not custom else {}
if kind == 'selection':
xml = xml or xml1
# application name with a selection field
field_name = name_selection_groups(map(int, gs))
xml.append(E.field(name=field_name, **attrs))
xml.append(E.newline())
else:
xml = xml or xml2
# application separator with boolean fields
app_name = app and app.name or _('Other')
if not custom:
xml.append(E.separator(string=app_name, colspan="4", **attrs))
for g in gs:
field_name = name_boolean_group(g.id)
if g == group_no_one:
# make the group_no_one invisible in the form view
xml.append(E.field(name=field_name, invisible="1", **attrs))
else:
xml.append(E.field(name=field_name, **attrs))
xml2.append({'class': "o_label_nowrap"})
xml = E.field(E.group(*(xml3), col="2"), E.group(*(xml2), col="4"), E.group(*(xml1), col="2"), name="groups_id", position="replace")
xml.addprevious(etree.Comment("GENERATED AUTOMATICALLY BY GROUPS"))
xml_content = etree.tostring(xml, pretty_print=True, xml_declaration=True, encoding="utf-8")
view.write({'arch': xml_content})
return True