Reviewers: ,


Please review this at http://codereview.tryton.org/685003/

Affected files:
  A examples/purchase_requests.py
  A trytond_celery.py


Index: examples/purchase_requests.py
===================================================================
new file mode 100644

--- /dev/null
+++ b/examples/purchase_requests.py
@@ -0,0 +1,46 @@
+#This file is part of Tryton.  The COPYRIGHT file at the top level of
+#this repository contains the full copyright notices and license terms.
+from itertools import groupby
+
+from celery import Celery, group
+from trytond_celery import TrytonTask
+from trytond.pool import Pool
+from trytond.transaction import Transaction
+
+celery = Celery('purchase_requests')
+
+
[email protected](base=TrytonTask)
+def generate_all():
+    pool = Pool()
+    User = pool.get('res.user')
+    Product = pool.get('product.product')
+    admin, = User.search([
+            ('login', '=', 'admin'),
+            ])
+    with Transaction().set_user(admin.id), \
+        Transaction().set_context(
+            User.get_preferences(context_only=True)):
+        products = Product.search([
+                ('type', 'in', ['goods', 'assets']),
+                ('consumable', '=', False),
+                ('purchasable', '=', True),
+                ], order=[('id', 'ASC')])
+        group(generate.s([p.id for _, p in l])
+            for _, l in groupby(enumerate(products),
+                lambda i: i[0] // 1000))()
+
+
[email protected](base=TrytonTask)
+def generate(products):
+    pool = Pool()
+    User = pool.get('res.user')
+    PurchaseRequest = pool.get('purchase.request')
+    Product = pool.get('product.product')
+    admin, = User.search([
+            ('login', '=', 'admin'),
+            ])
+    with Transaction().set_user(admin.id), \
+        Transaction().set_context(
+            User.get_preferences(context_only=True)):
+        PurchaseRequest.generate_requests(Product.browse(products))

Index: trytond_celery.py
===================================================================
new file mode 100644

--- /dev/null
+++ b/trytond_celery.py
@@ -0,0 +1,29 @@
+#This file is part of Tryton.  The COPYRIGHT file at the top level of
+#this repository contains the full copyright notices and license terms.
+from celery import Task
+from trytond.pool import Pool
+from trytond.transaction import Transaction
+from trytond.cache import Cache
+from trytond.backend import DatabaseOperationalError
+from trytond.config import CONFIG
+
+
+class TrytonTask(Task):
+    abstract = True
+
+    def __call__(self, *args, **kwargs):
+        database = self.app.conf.TRYTON_DATABASE
+        Cache.clean(database)
+        if database not in Pool.database_list():
+            with Transaction().start(database, 0, readonly=True):
+                Pool(database).init()
+        with Transaction().start(database, 0) as transaction:
+            try:
+                result = super(TrytonTask, self).__call__(*args, **kwargs)
+                transaction.cursor.commit()
+            except DatabaseOperationalError, exc:
+                transaction.cursor.rollback()
+                raise self.retry(args=args, kwargs=kwargs, exc=exc,
+                    countdown=0, max_retries=int(CONFIG['retry']))
+        Cache.resets(database)
+        return result



--
--
[email protected] mailing list

--- You received this message because you are subscribed to the Google Groups "tryton-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to