Browse Source

[IMP] partner_identification: Add python validation, improve view and fix default partner

14.0
Tran Thanh Phuc 3 years ago
parent
commit
2d8aef4bf5
  1. 14
      partner_identification/models/res_partner_id_category.py
  2. 11
      partner_identification/models/res_partner_id_number.py
  3. 36
      partner_identification/views/res_partner_id_category_view.xml

14
partner_identification/models/res_partner_id_category.py

@ -33,21 +33,9 @@ class ResPartnerIdCategory(models.Model):
)
active = fields.Boolean(string="Active", default=True)
validation_code = fields.Text(
"Python validation code",
help="Python code called to validate an id number.",
default=lambda self: self._default_validation_code(),
"Python validation code", help="Python code called to validate an id number."
)
def _default_validation_code(self):
return _(
"\n# Python code. Use failed = True to specify that the id "
"number is not valid.\n"
"# You can use the following variables :\n"
"# - self: browse_record of the current ID Category "
"browse_record\n"
"# - id_number: browse_record of ID number to validate"
)
def _validation_eval_context(self, id_number):
self.ensure_one()
return {"self": self, "id_number": id_number}

11
partner_identification/models/res_partner_id_number.py

@ -68,3 +68,14 @@ class ResPartnerIdNumber(models.Model):
]
)
active = fields.Boolean(string="Active", default=True)
@api.model
def default_get(self, fields):
res = super(ResPartnerIdNumber, self).default_get(fields)
# It seems to be a bug in native odoo that the field partner_id
# is not in the fields list by default. A workaround is required
# to force this.
if "default_partner_id" in self._context and "partner_id" not in fields:
fields.append("partner_id")
res["partner_id"] = self._context.get("default_partner_id")
return res

36
partner_identification/views/res_partner_id_category_view.xml

@ -7,10 +7,38 @@
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Partner Identification Categories">
<group>
<field name="name" />
<field name="code" />
</group>
<sheet>
<group>
<group>
<field name="name" />
<field name="code" />
<field name="validation_code" />
</group>
<group>
<div class="alert alert-primary" role="alert">
<h4>Python Validation Code:</h4>
<pre>
# Use failed = True to specify that the id number is not valid.
# You can use the following variables :
# - self: browse_record of the current ID Category browse_record
# - id_number: browse_record of ID number to validate
<b><i># Sample 1: ID number only contains digits</i></b>
if not id_number.name.isdigit():
failed = True
else:
failed = False
<b><i># Sample 2: Length of ID number cannot exceed 10 chars</i></b>
failed = len(id_number.name) > 10 and True or False
<b><i># Sample 3: ID number must start with the category code</i></b>
failed = not id_number.name.startswith(self.code) and True or False
</pre>
</div>
</group>
</group>
</sheet>
</form>
</field>
</record>

Loading…
Cancel
Save