_constraint - altering data of the tested object

Asked by Ferdinand

Hello!

the module can be found in
~openerp-commiter/openobject-addons/stable_5.0-extra-addons/

I am trying to set a default variable in the exception handler, (from lines 131 on)

The problem is, that a similar statement works for bank.statement.line but not for invoice.line nor account.move.line
the _constraint marked as FIXME in
chricar_account_analytic/account_analytic.py
is executed for sure, because the following if statement does not fail.
Hence I have deactivated it for the moment.

So I do not know if this is a programming error

QUESTIONS:
1) Is it allowed to alter object data in _constraints ?
2) if yes
2a) How to return changed values in ALL cases
2b) Pls describe the workflow of _constraints
2c) Is it possible to return some information to the constraint message like "test A failed" or "test B failed"

the developer book is not helpful
***************
http://doc.openerp.com/developer/2_5_Objects_Fields_Methods/object_attributes.html?highlight=_constraints
_constraints
The constraints on the object. See the constraints section for details.
***************
I do not find a constraint section

the Example:
***************
 class account_invoice_line(osv.osv):
    _inherit = "account.invoice.line"

    def _check_analytic_account(self, cr, uid, ids):
        for move in self.browse(cr, uid, ids):
            # FIXME - does not write the found value
            #if not move.account_analytic_id and move.account_id.account_analytic_usage in ['default','fixed']:
            # move.account_analytic_id = move.account_id.account_analytic_id
            # return True
            if not move.account_analytic_id and move.account_id.account_analytic_usage in ['mandatory','default','fixed']:
                return False
            if move.account_analytic_id and move.account_id.account_analytic_usage in ['none']:
                return False
        return True

    _constraints = [
        (_check_analytic_account,
            'You must assign an analytic account using this account', ['account_analytic_id'] ),
        ]

account_invoice_line()
*************

Question information

Language:
English Edit question
Status:
Answered
For:
Odoo Server (MOVED TO GITHUB) Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
xrg (xrg) said :
#1

On Friday 16 April 2010, you wrote:
> New question #107562 on OpenObject Server:
> https://answers.launchpad.net/openobject-server/+question/107562
>

Sorry, Ferdinard, I only spend 1-2 hours/day for OpenERP and couldn't help
earlier.

Anyway: IMHO:
1) constraints should NOT alter the data, nor the context, either. It is
better to consider those functions as /read only/ on the data. Even if they
are programatically able to perform updates, at some day, the next developer
will not expect that this had been happening, and then we'll be in trouble.

2) If you want detailed explanation about each case, you could split the
constraints into more than one. So, instead of a " 'A= Blue' and 'B is set'
are mutually exclusive" explanation, you could have one "you cannot set B if A
= Blue" and another "You cannot set A= Blue when B is set" constraints.

Usually, when we want to force some values into the tables, depending on other
columns, we code that behaviour in the write() method of the class. Note,
however, that this would /only/ be allowed if altering the column data is
absolutely trivial. Inside the write() you may also raise an Exception, with a
better explanation, when the data is not valid (rather than coding that in the
constraint functions).

Revision history for this message
Claude Brulé (claude-brule-syleam) said :
#2

Hello Ferdinand,
In my point of view, constraints are low level asserts. So we are not
supposed to upadte any data at this step.

You should implement a new method and call it from write and create,
shouldn't you ?

Regards,
Claude

Ferdinand @ ChriCar a écrit :
> New question #107562 on OpenObject Server:
> https://answers.launchpad.net/openobject-server/+question/107562
>
> Hello!
>
> the module can be found in
> ~openerp-commiter/openobject-addons/stable_5.0-extra-addons/
>
> I am trying to set a default variable in the exception handler, (from lines 131 on)
>
> The problem is, that a similar statement works for bank.statement.line but not for invoice.line nor account.move.line
> the _constraint marked as FIXME in
> chricar_account_analytic/account_analytic.py
> is executed for sure, because the following if statement does not fail.
> Hence I have deactivated it for the moment.
>
> So I do not know if this is a programming error
>
> QUESTIONS:
> 1) Is it allowed to alter object data in _constraints ?
> 2) if yes
> 2a) How to return changed values in ALL cases
> 2b) Pls describe the workflow of _constraints
> 2c) Is it possible to return some information to the constraint message like "test A failed" or "test B failed"
>
> the developer book is not helpful
> ***************
> http://doc.openerp.com/developer/2_5_Objects_Fields_Methods/object_attributes.html?highlight=_constraints
> _constraints
> The constraints on the object. See the constraints section for details.
> ***************
> I do not find a constraint section
>
> the Example:
> ***************
> class account_invoice_line(osv.osv):
> _inherit = "account.invoice.line"
>
>
> def _check_analytic_account(self, cr, uid, ids):
> for move in self.browse(cr, uid, ids):
> # FIXME - does not write the found value
> #if not move.account_analytic_id and move.account_id.account_analytic_usage in ['default','fixed']:
> # move.account_analytic_id = move.account_id.account_analytic_id
> # return True
> if not move.account_analytic_id and move.account_id.account_analytic_usage in ['mandatory','default','fixed']:
> return False
> if move.account_analytic_id and move.account_id.account_analytic_usage in ['none']:
> return False
> return True
>
> _constraints = [
> (_check_analytic_account,
> 'You must assign an analytic account using this account', ['account_analytic_id'] ),
> ]
>
> account_invoice_line()
> *************
>
>
>
>

--
SYLEAM
27 avenue Jean Mantelet
61000 Alençon
FRANCE
tel : +33 (0)2 33 31 22 10
fax : +33 (0)2 33 31 22 14

Revision history for this message
Ferdinand (office-chricar) said :
#3

I agree - we shall not use constraints to "update" data.
- should and can OpenERP enforce this ?

@Claude
the way many methods are written do not realy allow to hook in.
This was already discussed during the community days - the methods should be split up into smaller junks.

Nevertheless I'd like to be able to return a string to the error message to give a more precise feedback to the user.
Like "account %s must not have analyitc accounts" .....

Revision history for this message
Claude Brulé (claude-brule-syleam) said :
#4

Ferdinand @ ChriCar a écrit :
> Question #107562 on OpenObject Server changed:
> https://answers.launchpad.net/openobject-server/+question/107562
>
> Status: Answered => Open
>
> Ferdinand @ ChriCar is still having a problem:
> I agree - we shall not use constraints to "update" data.
> - should and can OpenERP enforce this ?
>
> @Claude
> the way many methods are written do not realy allow to hook in.
> This was already discussed during the community days - the methods should be split up into smaller junks.
>
That's right.

> Nevertheless I'd like to be able to return a string to the error message to give a more precise feedback to the user.
> Like "account %s must not have analyitc accounts" .....
>
>
OK.
I used return {'warning': {'title': 'error', 'message: 'what you want'}}
with no succes (in osv.memory, and so on, for example).

--
SYLEAM
27 avenue Jean Mantelet
61000 Alençon
FRANCE
tel : +33 (0)2 33 31 22 10
fax : +33 (0)2 33 31 22 14

Can you help with this problem?

Provide an answer of your own, or ask Ferdinand for more information if necessary.

To post a message you must log in.