95 lines
3.2 KiB
Python
95 lines
3.2 KiB
Python
from odoo import models, fields, _
|
|
|
|
class MasterControl(models.Model):
|
|
_name = 'master.control'
|
|
_description = 'Master Control'
|
|
|
|
sequence = fields.Integer()
|
|
name = fields.Char(string='Master Name', required=True)
|
|
code = fields.Char(string='Code', required=True)
|
|
default_show = fields.Boolean(default=True)
|
|
access_group_ids = fields.One2many('group.access.line','master_control_id',string='Roles')
|
|
|
|
|
|
def action_generate_groups(self):
|
|
"""Generate category → groups list"""
|
|
for rec in self:
|
|
|
|
# Clear old groups
|
|
rec.access_group_ids.unlink()
|
|
|
|
groups = self.env['res.groups'].sudo().search([],order='category_id')
|
|
show_group = True if rec.default_show else False
|
|
print(show_group)
|
|
for grp in groups:
|
|
self.env['group.access.line'].create({
|
|
'master_control_id': rec.id,
|
|
'group_id': grp.id,
|
|
'show_group': show_group,
|
|
})
|
|
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': _('Success'),
|
|
'message': _('Groups generated successfully!'),
|
|
'type': 'success',
|
|
}
|
|
}
|
|
|
|
# -----------------------------------------
|
|
# UPDATE GROUPS (Detect new groups)
|
|
# -----------------------------------------
|
|
def action_update_groups(self):
|
|
for rec in self:
|
|
created_count = 0
|
|
|
|
existing_ids = set(rec.access_group_ids.mapped('group_id.id'))
|
|
|
|
categories = self.env['ir.module.category'].search([])
|
|
|
|
for category in categories:
|
|
groups = self.env['res.groups'].search([
|
|
('category_id', '=', category.id)
|
|
])
|
|
|
|
for grp in groups:
|
|
# create only missing group
|
|
if grp.id not in existing_ids:
|
|
rec.access_group_ids.create({
|
|
'master_control_id': rec.id,
|
|
'category_id': category.id,
|
|
'group_id': grp.id,
|
|
'show_group': True if rec.default_show else False,
|
|
})
|
|
created_count += 1
|
|
existing_ids.add(grp.id)
|
|
|
|
if created_count:
|
|
message = f"Added {created_count} new groups."
|
|
msg_type = "success"
|
|
else:
|
|
message = "No new groups found."
|
|
msg_type = "info"
|
|
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': _('Group Update'),
|
|
'message': _(message),
|
|
'type': msg_type,
|
|
}
|
|
}
|
|
|
|
|
|
class GroupsAccessLine(models.Model):
|
|
_name = 'group.access.line'
|
|
_description = 'Group Access Line'
|
|
_rec_name = 'group_id'
|
|
|
|
category_id = fields.Many2one('ir.module.category', related='group_id.category_id')
|
|
group_id = fields.Many2one('res.groups', string="Role")
|
|
show_group = fields.Boolean(string="Show", default=True)
|
|
master_control_id = fields.Many2one('master.control') |