mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[14.0][ADD] New module web_custom_modifier
[14.0][ADD] New module web_custom_modifier
This commit is contained in:
committed by
Abdellatif Benzbiria
parent
fc63411fa1
commit
3917886453
8
web_custom_modifier/models/__init__.py
Normal file
8
web_custom_modifier/models/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
|
||||
from . import (
|
||||
base,
|
||||
ir_ui_view,
|
||||
web_custom_modifier,
|
||||
)
|
||||
19
web_custom_modifier/models/base.py
Normal file
19
web_custom_modifier/models/base.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# Copyright 2023 - today Numigi (tm) and all its contributors (https://bit.ly/numigiens)
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
|
||||
from odoo import api, models
|
||||
|
||||
from ..utils import set_custom_modifiers_on_fields
|
||||
|
||||
|
||||
class Base(models.AbstractModel):
|
||||
|
||||
_inherit = "base"
|
||||
|
||||
@api.model
|
||||
def fields_get(self, allfields=None, attributes=None):
|
||||
"""Add the custom modifiers to the fields metadata."""
|
||||
fields = super().fields_get(allfields, attributes)
|
||||
modifiers = self.env["web.custom.modifier"].get(self._name)
|
||||
set_custom_modifiers_on_fields(modifiers, fields)
|
||||
return fields
|
||||
26
web_custom_modifier/models/common.py
Normal file
26
web_custom_modifier/models/common.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# © 2023 - today Numigi (tm) and all its contributors (https://bit.ly/numigiens)
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
|
||||
from typing import List, Mapping
|
||||
|
||||
|
||||
def set_custom_modifiers_on_fields(modifiers: List[dict], fields: Mapping[str, dict]):
|
||||
_hide_selection_items(modifiers, fields)
|
||||
|
||||
|
||||
def _hide_selection_items(modifiers, fields):
|
||||
hidden_items = (
|
||||
m
|
||||
for m in modifiers
|
||||
if m["type_"] == "field" and m["modifier"] == "selection_hide"
|
||||
)
|
||||
for item in hidden_items:
|
||||
_hide_single_selection_item(item, fields)
|
||||
|
||||
|
||||
def _hide_single_selection_item(modifier, fields):
|
||||
field = fields.get(modifier["reference"])
|
||||
if field and "selection" in field:
|
||||
field["selection"] = [
|
||||
(k, v) for k, v in field["selection"] if k != modifier["key"]
|
||||
]
|
||||
24
web_custom_modifier/models/ir_ui_view.py
Normal file
24
web_custom_modifier/models/ir_ui_view.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
|
||||
from odoo import models
|
||||
|
||||
from ..utils import add_custom_modifiers_to_view_arch, set_custom_modifiers_on_fields
|
||||
|
||||
|
||||
class ViewWithCustomModifiers(models.Model):
|
||||
_inherit = "ir.ui.view"
|
||||
|
||||
def postprocess(self, node, current_node_path, editable, name_manager):
|
||||
"""Add custom modifiers to the view xml.
|
||||
|
||||
This method is called in Odoo when generating the final xml of a view.
|
||||
"""
|
||||
model_name = name_manager.Model._name
|
||||
modifiers = self.env["web.custom.modifier"].get(model_name)
|
||||
node_with_custom_modifiers = add_custom_modifiers_to_view_arch(modifiers, node)
|
||||
set_custom_modifiers_on_fields(modifiers, name_manager.available_fields)
|
||||
self.clear_caches() # Clear the cache in order to recompute _get_active_rules
|
||||
return super().postprocess(
|
||||
node_with_custom_modifiers, current_node_path, editable, name_manager
|
||||
)
|
||||
95
web_custom_modifier/models/web_custom_modifier.py
Normal file
95
web_custom_modifier/models/web_custom_modifier.py
Normal file
@@ -0,0 +1,95 @@
|
||||
# Copyright 2023 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
|
||||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
|
||||
|
||||
from odoo import api, fields, models, tools
|
||||
|
||||
|
||||
class WebCustomModifier(models.Model):
|
||||
|
||||
_name = "web.custom.modifier"
|
||||
_description = "Custom View Modifier"
|
||||
|
||||
model_ids = fields.Many2many(
|
||||
"ir.model", "ir_model_custom_modifier", "modifier_id", "model_id", "Model"
|
||||
)
|
||||
type_ = fields.Selection(
|
||||
[
|
||||
("field", "Field"),
|
||||
("xpath", "Xpath"),
|
||||
],
|
||||
string="Type",
|
||||
default="field",
|
||||
required=True,
|
||||
)
|
||||
modifier = fields.Selection(
|
||||
[
|
||||
("invisible", "Invisible"),
|
||||
("column_invisible", "Invisible (List Views)"),
|
||||
("readonly", "Readonly"),
|
||||
("force_save", "Force Save"),
|
||||
("required", "Required"),
|
||||
("selection_hide", "Hide Selection Item"),
|
||||
("widget", "Widget"),
|
||||
("limit", "Number of lines per page (List Views)"),
|
||||
("optional", "Optional"),
|
||||
],
|
||||
required=True,
|
||||
)
|
||||
reference = fields.Char(required=True)
|
||||
key = fields.Char()
|
||||
active = fields.Boolean(default=True)
|
||||
excluded_group_ids = fields.Many2many(
|
||||
"res.groups",
|
||||
"web_custom_modifier_excluded_group_rel",
|
||||
"modifier_id",
|
||||
"group_id",
|
||||
"Excluded Groups",
|
||||
)
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
new_record = super().create(vals)
|
||||
self._clear_modifier_cache()
|
||||
return new_record
|
||||
|
||||
def write(self, vals):
|
||||
super().write(vals)
|
||||
self._clear_modifier_cache()
|
||||
return True
|
||||
|
||||
def unlink(self):
|
||||
super().unlink()
|
||||
self._clear_modifier_cache()
|
||||
return True
|
||||
|
||||
def _clear_modifier_cache(self):
|
||||
for model in (
|
||||
self.sudo().env["web.custom.modifier"].search([]).mapped("model_ids.model")
|
||||
):
|
||||
self.env[model].clear_caches()
|
||||
|
||||
@tools.ormcache()
|
||||
def _get_cache(self):
|
||||
return [
|
||||
el._to_dict() for el in self.sudo().env["web.custom.modifier"].search([])
|
||||
]
|
||||
|
||||
def _to_dict(self):
|
||||
return {
|
||||
"models": self.mapped("model_ids.model"),
|
||||
"key": self.key,
|
||||
"type_": self.type_,
|
||||
"modifier": self.modifier,
|
||||
"reference": self.reference,
|
||||
"excluded_group_ids": self.excluded_group_ids.ids,
|
||||
}
|
||||
|
||||
def get(self, model):
|
||||
cache = self._get_cache()
|
||||
user_group_ids = self.env.user.groups_id.ids
|
||||
return [
|
||||
el
|
||||
for el in cache
|
||||
if model in el["models"]
|
||||
and all(id_ not in user_group_ids for id_ in el["excluded_group_ids"])
|
||||
]
|
||||
Reference in New Issue
Block a user