generic_validators
Idea
Allow users to add some set of custom validators based on conditions to any model that supports this. It could be implemented as:
- Implement it like automated actions do, or just extend automated actions to do that
- Use same approach as generic tags do:
- core addon that implements logic
- and set of addons that provide integrations to original odoo addons.
Validator have following main fields:
- name
- model_id
- field_ids
- pre_condition_ids
- post_condition_ids
- check_condition_ids
- message
- exception_type:
- UserError
- ValidationError
- AccessError
Validation algorithm
Idea is following. On any write on object, we have to check pre conditions and after write check post conditions If object passes both pre and post conditions, that we test it with check conditions, and if object does not pass check conditions we raise exception. Thus pre/post conditions used to filter objects that have to be tested. For example in case of Sale Order we may want to raise validation error on attempt to confirm sale order if selected customer have not phone. To implement this, we may configure validator in following way:
- field_ids: state
- pre_conditions: state != confirmed
- post_conditions: state == confirmed
- check_conditions: partner_id.phone == False
To Do
-
Create addon
generic_validator
-
Create model
generic.validator.model
-
fields
- res_model_id: m2o('ir.model', delegate, index, required, ondelete=cascade)
- validator_ids: o2m('generic.validator', 'model_id', readonly)
- validator_count: integer(computed len(validator_ids)
-
views
- tree (no create, no edit, no delete)
- name
- vallidator_count
- button to open validators
- form (no create, no edit, no delete)
- name (as
oe_title
) - stat-button Validators
- display
validator_count
- open view for
generic.validator
- path in context
default_model_id=id
- display
- name (as
- tree (no create, no edit, no delete)
- ACL: readonly for all
- records in this model may be created only via module data.
-
fields
-
Create model
generic.validator
-
inherit
mail.thread
-
fields
- name: Char(translate, required, track onchange)
- active: Boolean(index, default=True)
- sequence: Integer(index, default=5)
- model_id: m2o('generic.validator.model', required, index, track onchange)
- field_ids: m2m('ir.model.fields', string='Trigger fields')
- pre_condition_ids: m2m('generic.condition')
- post_condition_ids: m2m('generic.condition')
- check_condition_ids: m2m('generic.condition')
- message: Text(required)
- exception_type: Selection(required)
- UserError
- ValidationError
- AccessError
-
Onchange
model_id
cleanfield_ids
pre_condition_ids
post_condition_ids
check_condition_ids
- Constraint to check if all condition and fields do not conflict with selected model in record
-
views:
-
tree
- sequence (widget
handler
) - name
- model
- exception_type
- field_ids (widget
many2many_tags
) - active
- sequence (widget
-
form
- name (as
oe_title
) - stat-button Archive
- left group Triggers
- field_ids
- pre_condition_ids
- post_condition_ids
- right group Check
- check_condition_ids
- exception_type
- message
- name (as
-
search
- search by:
name
,model_id
- group_by:
model_id
,exception_type
- filters: Archived
- search by:
-
tree
- ACL: readonly for all
- menuitem Settings / Technical Settings / Generic Validators
-
inherit
-
Create abstract model
generic.validator.mixin
-
Override
write
method- find validators for current model
- map validators and objects that satisfy validator's pre conditions
- call super
write
- filter out objects that does not satisfy post conditions for validators in mapping
- Check obejcts with validator's check conditions
- If object does not satisfy check conditions raise selected class of exception with validator's message
- return result of original super call
-
Override
-
Create addons
test_generic_validator
- Place here tests for
generic_validator
- tests will require model that inherit
generic.validator.mixin
. For this case test model in this addon may be created.
- Place here tests for