19
19
#
20
20
##############################################################################
21
21
22
- import sys
23
- import logging
24
22
import inspect
23
+ import functools
24
+ import logging
25
25
import uuid
26
+ import sys
26
27
from datetime import datetime , timedelta , MINYEAR
27
28
from pickle import loads , dumps , UnpicklingError
28
29
@@ -598,6 +599,11 @@ def postpone(self, result=None, seconds=None):
598
599
if result is not None :
599
600
self .result = result
600
601
602
+ def related_action (self , session ):
603
+ if not hasattr (self .func , 'related_action' ):
604
+ return None
605
+ return self .func .related_action (session , self )
606
+
601
607
602
608
def job (func ):
603
609
""" Decorator for jobs.
@@ -630,7 +636,7 @@ def job(func):
630
636
infinite retries. Default is 5.
631
637
* eta: the job can be executed only after this datetime
632
638
(or now + timedelta if a timedelta or integer is given)
633
-
639
+
634
640
* description : a human description of the job,
635
641
intended to discriminate job instances
636
642
(Default is the func.__doc__ or 'Function %s' % func.__name__)
@@ -655,6 +661,9 @@ def export_one_thing(session, model_name, one_thing):
655
661
# => the job will be executed with a low priority and not before a
656
662
# delay of 5 hours from now
657
663
664
+ See also: :py:func:`related_action` a related action can be attached
665
+ to a job
666
+
658
667
"""
659
668
def delay (session , model_name , * args , ** kwargs ):
660
669
"""Enqueue the function. Return the uuid of the created job."""
@@ -665,3 +674,61 @@ def delay(session, model_name, *args, **kwargs):
665
674
** kwargs )
666
675
func .delay = delay
667
676
return func
677
+
678
+
679
+ def related_action (action = lambda session , job : None , ** kwargs ):
680
+ """ Attach a *Related Action* to a job.
681
+
682
+ A *Related Action* will appear as a button on the OpenERP view.
683
+ The button will execute the action, usually it will open the
684
+ form view of the record related to the job.
685
+
686
+ The ``action`` must be a callable that responds to arguments::
687
+
688
+ session, job, **kwargs
689
+
690
+ Example usage:
691
+
692
+ .. code-block:: python
693
+
694
+ def related_action_partner(session, job):
695
+ model = job.args[0]
696
+ partner_id = job.args[1]
697
+ # eventually get the real ID if partner_id is a binding ID
698
+ action = {
699
+ 'name': _("Partner"),
700
+ 'type': 'ir.actions.act_window',
701
+ 'res_model': model,
702
+ 'view_type': 'form',
703
+ 'view_mode': 'form',
704
+ 'res_id': partner_id,
705
+ }
706
+ return action
707
+
708
+ @job
709
+ @related_action(action=related_action_partner)
710
+ def export_partner(session, model_name, partner_id):
711
+ # ...
712
+
713
+ The kwargs are transmitted to the action:
714
+
715
+ .. code-block:: python
716
+
717
+ def related_action_product(session, job, extra_arg=1):
718
+ assert extra_arg == 2
719
+ model = job.args[0]
720
+ product_id = job.args[1]
721
+
722
+ @job
723
+ @related_action(action=related_action_product, extra_arg=2)
724
+ def export_product(session, model_name, product_id):
725
+ # ...
726
+
727
+ """
728
+ def decorate (func ):
729
+ if kwargs :
730
+ func .related_action = functools .partial (action , ** kwargs )
731
+ else :
732
+ func .related_action = action
733
+ return func
734
+ return decorate
0 commit comments