Skip to content

Commit

Permalink
[IMP] l10n_br_sped_base: readonly if not draft
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalyi committed May 27, 2024
1 parent 1da5fb0 commit 7f2f655
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion l10n_br_sped_base/models/sped_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def _append_view_footer(self, form):
form.append(div)

@api.model
def _append_top_view_elements(self, group):
def _append_top_view_elements(self, group, inline=False):
group.append(E.field(name="company_id"))
group.append(E.separator(colspan="4"))

Expand Down
54 changes: 43 additions & 11 deletions l10n_br_sped_base/models/sped_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

MAX_REGISTER_NAME = 40

EDITABLE_ON_DRAFT = "{'readonly': [('state', 'not in', ['draft'])]}"


class SpedMixin(models.AbstractModel):
_name = "l10n_br_sped.mixin"
Expand Down Expand Up @@ -136,8 +138,9 @@ def _get_default_tree_view(self):
desc = self._description
tree = E.tree(string=desc)
fields = self.fields_get()

added_fields = set()

tree.append(E.field(name="state", invisible="1"))
if not self._name.endswith("0000"):
tree.append(E.field(name="declaration_id"))

Expand Down Expand Up @@ -194,14 +197,15 @@ def _get_default_tree_view(self):
return tree

@api.model
def _get_default_form_view(self):
def _get_default_form_view(self, inline=False):
"""Generate a default single-line form view using all fields
:return: a tree view as an lxml document
:rtype: etree._Element
"""
group = E.group(col="4")
self._append_top_view_elements(group)
self._append_top_view_elements(group, inline=inline)
group.append(E.field(name="state", invisible="1"))

for fname, field in self._fields.items():
if field.automatic:
Expand All @@ -218,7 +222,12 @@ def _get_default_form_view(self):
continue
elif field.type in ("one2many", "many2many", "text", "html"):
group.append(E.newline())
field_tag = E.field(name=fname, colspan="4")
field_tag = E.field(
name=fname,
colspan="4",
attrs=EDITABLE_ON_DRAFT,
context="{'default_declaration_id': declaration_id}",
)
if field.type == "one2many":
tree_fields = [
(f, native_field)
Expand All @@ -237,11 +246,19 @@ def _get_default_form_view(self):
):
# few fields -> editable tree
field_tree = E.tree(editable="bottom")
field_tree.append(E.field(name="declaration_id", invisible="1"))
field_tree.append(E.field(name="state", invisible="1"))
field_tree.append(E.field(name="reference", widget="reference"))
for tree_field in tree_fields:
field_tree.append(E.field(name=tree_field[0]))
field_tree.append(
E.field(
name=tree_field[0],
)
)
field_tag.append(field_tree)
else:
field_tree = E.tree()
field_tree.append(E.field(name="state", invisible="1"))
for index, tree_field in enumerate(tree_fields):
if index > 6:
break
Expand All @@ -251,18 +268,27 @@ def _get_default_form_view(self):
):
continue
field_tree.append(
E.field(name=tree_field[0], string=tree_field[0])
E.field(
name=tree_field[0],
string=tree_field[0],
)
)
field_tag.append(field_tree)
field_form = self.env[
field.comodel_name
]._get_default_form_view(inline=True)
field_tag.append(field_form)
group.append(field_tag)
group.append(E.newline())
elif fname.isupper():
group.append(E.field(name=fname))
group.append(E.field(name=fname, attrs=EDITABLE_ON_DRAFT))
group.append(E.separator())
form = E.form()
self._append_view_header(form)
if not inline:
self._append_view_header(form)
form.append(E.sheet(group, string=self._description))
self._append_view_footer(form)
if not inline:
self._append_view_footer(form)
return form

@api.model
Expand All @@ -274,8 +300,14 @@ def _append_view_footer(self, form):
pass

@api.model
def _append_top_view_elements(self, group):
group.append(E.field(name="declaration_id"))
def _append_top_view_elements(self, group, inline=False):
group.append(
E.field(
name="declaration_id",
attrs="{'readonly': [('state', 'not in', ['draft']), ('declaration_id', '!=', False)]}",
invisible="1" if inline else "0",
)
)
group.append(E.field(name="reference", widget="reference"))
group.append(E.separator(colspan="4"))

Expand Down

0 comments on commit 7f2f655

Please sign in to comment.