Create a trigger for sending posted invoices

Rational

I tried to use notification_mail, and I have been bloked on the trigger. My need is to send automatically a mail when my invoice is posted. It is really hard to configure it because :

  • If the mode is time : the mail will be sent every 5 minutes
  • If the mode is write : the mail will be sent for each modification, without any possibility to evaluate a condition on the modification

Proposition

I propose to remove the fields condition and replace it by two other fields:

  • cron_domain : a pyson field to define the domain to use when the trigger is called by the cron
  • write_condition : a char to eval a python condition with the values passed to the write method and the existing record record.state in [‘draft’, ‘validated’] and values.get(‘state’) == 'posted’

And the method eval should be also removed to be replaced by:

  • get_cron_domain : return the decoded domain
  • eval_write_condition : do a python safe eval with the record and the values come from the write method, because the record is not modified yet.

The method trigger_write_get_eligibles (from ModelStorage) needs to have the values in the parameters, to propagate it to eval_write_condition.

Future

  • create_condition based on the values passed at the create method with his associated method
  • delete_condition based on the existing record with his associated method

This is already possible to do, you just need to set the proper condition.

In order to simplify that I created a feature request to add trigger templates. The feature is working now, we just need more testing and thinking if we need more triggers by default.

On the proposed patch there are some examples of conditions for sending invoices whe posting.

Thanks for your quick response, I understood my misunderstanding on the condition.

I just did some tests, is it actually working. I got fooled because the evaluation is done several times, the first being before the record update

(venv) jssuzanne:hb_tryton$ trytond -c trytond-dev.conf -d tryton --dev
146253 139806965098304 [2021-12-02 14:43:16,188] WARNING werkzeug  * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
146253 139806965098304 [2021-12-02 14:43:16,188] INFO werkzeug  * Running on http://172.16.1.71:8000/ (Press CTRL+C to quit)
146253 139806965098304 [2021-12-02 14:43:16,191] INFO werkzeug  * Restarting with stat
*** chargement des options (via .pythonstartup)
> /home/jssuzanne/hashbang/tryton/hb_tryton/venv/lib/python3.9/site-packages/trytond/ir/trigger.py(190)eval()
    189         ipdb.set_trace()
--> 190         return bool(PYSONDecoder(env).decode(self.condition))
    191 

ipdb> self.condition , bool(PYSONDecoder(env).decode(self.condition))
('{"__class__":"Equal","s1":{"__class__":"Get","v":{"__class__":"Eval","v":"self","d":{}},"k":"state","d":""},"s2":"posted"}', False)
ipdb> c
> /home/jssuzanne/hashbang/tryton/hb_tryton/venv/lib/python3.9/site-packages/trytond/ir/trigger.py(190)eval()
    189         ipdb.set_trace()
--> 190         return bool(PYSONDecoder(env).decode(self.condition))
    191 

ipdb> self.condition , bool(PYSONDecoder(env).decode(self.condition))
('{"__class__":"Equal","s1":{"__class__":"Get","v":{"__class__":"Eval","v":"self","d":{}},"k":"state","d":""},"s2":"posted"}', False)
ipdb> c
> /home/jssuzanne/hashbang/tryton/hb_tryton/venv/lib/python3.9/site-packages/trytond/ir/trigger.py(190)eval()
    189         ipdb.set_trace()
--> 190         return bool(PYSONDecoder(env).decode(self.condition))
    191 

ipdb> self.condition , bool(PYSONDecoder(env).decode(self.condition))
('{"__class__":"Equal","s1":{"__class__":"Get","v":{"__class__":"Eval","v":"self","d":{}},"k":"state","d":""},"s2":"posted"}', True)
ipdb> c
> /home/jssuzanne/hashbang/tryton/hb_tryton/venv/lib/python3.9/site-packages/trytond/ir/trigger.py(190)eval()
    189         ipdb.set_trace()
--> 190         return bool(PYSONDecoder(env).decode(self.condition))
    191 

ipdb> self.condition , bool(PYSONDecoder(env).decode(self.condition))
('{"__class__":"Equal","s1":{"__class__":"Get","v":{"__class__":"Eval","v":"self","d":{}},"k":"state","d":""},"s2":"posted"}', True)
ipdb> c

I close this request, sorry for the noise

Just to clarify, write triggers are evaluated before and after the write, so we ensure that the value is changed. So if you test that state is posted, the trigger will be only executed when the state before the write was not posted and when the state after the write is posted!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.