Skip to content

Commit

Permalink
wip remake
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasProgrammer committed Jul 27, 2023
1 parent 6114185 commit 8c0b6da
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class CustomFieldRestriction(models.Model):
required = fields.Boolean()
field_invisible = fields.Boolean()
field_readonly = fields.Boolean()
visibility_field_id = fields.Many2one('ir.model.fields')

@api.onchange("field_id")
def onchange_field_id(self):
Expand All @@ -67,3 +68,31 @@ def _compute_model_name(self):
rec.model_name = rec.invisible_model_id.model
elif rec.readonly_model_id:
rec.model_name = rec.readonly_model_id.model

@api.model_create_multi
def create(self, vals_list):
res = super(CustomFieldRestriction, self).create(vals_list)
for rec in res:
if rec.condition_domain and rec.field_invisible:
rec.create_visibility_field()

def create_visibility_field(self):
field_name = "x_computed_%s_visibility" % self.field_id.name
if self.env["ir.model.fields"].search([('name', '=', field_name)]):
return
new_field = self.env["ir.model.fields"].create(
{
"name": field_name,
"model_id": self.invisible_model_id.id,
"state": "manual",
"field_description": "%s visibility field" % self.field_id.name,
"store": False,
"ttype": "boolean",
"compute": "for r in self: r.compute_xxx()"
}
)
self.visibility_field_id = new_field

def unlink(self):
# TODO
return super(CustomFieldRestriction, self).unlink()
101 changes: 66 additions & 35 deletions web_field_required_invisible_manager/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,73 @@ def fields_view_get(
doc = etree.XML(res["arch"])
for node in doc.xpath("//field"):
name = node.attrib.get("name")
if view_type == "form" and name == 'partner_id':
modifiers = simplejson.loads(node.get("modifiers"))
visibility_field_name = 'x_computed_%s_visibility' % name
modifiers["invisible"] = "[('%s', '=', True)]" % visibility_field_name
node.set("modifiers", simplejson.dumps(modifiers))
node.getparent().append(etree.fromstring("<field name='%s'/>" % visibility_field_name))
res["arch"] = etree.tostring(doc)


restrictions_filtered = restrictions.filtered(
lambda x: x.field_id.name == name
)
for r in restrictions_filtered:
if (
view_type == "form"
and self.env.context.get("params")
and self.env.context["params"].get("id")
):
rec_id = self.env[r.model_name].browse(
self.env.context["params"]["id"]
)
if r.condition_domain:
filtered_rec_id = rec_id.filtered_domain(
safe_eval(r.condition_domain)
)
if not filtered_rec_id:
continue
if len(r.group_ids) and not (r.group_ids & self.env.user.groups_id):
continue
if r.required:
node.set("required", "1")
modifiers = simplejson.loads(node.get("modifiers"))
modifiers["required"] = True
node.set("modifiers", simplejson.dumps(modifiers))
res["arch"] = etree.tostring(doc)
if r.field_invisible:
node.set("invisible", "1")
modifiers = simplejson.loads(node.get("modifiers"))
modifiers["invisible"] = True
node.set("modifiers", simplejson.dumps(modifiers))
res["arch"] = etree.tostring(doc)
if r.field_readonly:
node.set("readonly", "1")
modifiers = simplejson.loads(node.get("modifiers"))
modifiers["readonly"] = True
node.set("modifiers", simplejson.dumps(modifiers))
res["arch"] = etree.tostring(doc)
# for r in restrictions_filtered:
# if (
# view_type == "form"
# and self.env.context.get("params")
# and self.env.context["params"].get("id")
# ):
# rec_id = self.env[r.model_name].browse(
# self.env.context["params"]["id"]
# )
# if r.condition_domain:
# filtered_rec_id = rec_id.filtered_domain(
# safe_eval(r.condition_domain)
# )
# if filtered_rec_id:
# filtered_rec_id.x_computed_res_partner_visibility = True

# if len(r.group_ids) and not (r.group_ids & self.env.user.groups_id):
# continue
# if r.required:
# node.set("required", "1")
# modifiers = simplejson.loads(node.get("modifiers"))
# modifiers["required"] = True
# node.set("modifiers", simplejson.dumps(modifiers))
# res["arch"] = etree.tostring(doc)
# if r.field_invisible:
# node.set("invisible", "1")
# modifiers = simplejson.loads(node.get("modifiers"))
# modifiers["invisible"] = True
# node.set("modifiers", simplejson.dumps(modifiers))
# res["arch"] = etree.tostring(doc)
# if r.field_readonly:
# node.set("readonly", "1")
# modifiers = simplejson.loads(node.get("modifiers"))
# modifiers["readonly"] = True
# node.set("modifiers", simplejson.dumps(modifiers))
# res["arch"] = etree.tostring(doc)
return res

def compute_xxx(self):
for so in self:
restrictions = self.env["custom.field.restriction"].search(
[
("model_name", "=", self._name),
("condition_domain", "not in", [False, '']),
("visibility_field_id", "!=", False),
]
)
if not restrictions:
return
field_name = restrictions.visibility_field_id.name
so[field_name] = False
for r in restrictions:
if r.condition_domain:
filtered_rec_id = so.filtered_domain(
safe_eval(r.condition_domain)
)
if filtered_rec_id:
so[field_name] = True

0 comments on commit 8c0b6da

Please sign in to comment.