--- a/app/soc/views/helper/decorators.py Sun Sep 27 01:23:58 2009 +0530
+++ b/app/soc/views/helper/decorators.py Sun Sep 27 23:31:39 2009 +0200
@@ -31,6 +31,7 @@
from django.utils.translation import ugettext
from soc.logic import dicts
+from soc.logic import tasks
from soc.views.helper import responses
@@ -116,3 +117,97 @@
return func(self, request, access_type, *args, **kwargs)
return wrapper
+
+
+def task(func):
+ """Task decorator wrapper method
+ """
+
+ @wraps(func)
+ def wrapper(request, *args, **kwargs):
+ """Decorator wrapper method
+ """
+
+ try:
+ return func(request, *args, **kwargs)
+ except tasks.FatalTaskError, error:
+ logging.exception(error)
+ return tasks.terminateTask()
+ except Exception, exception:
+ logging.exception(exception)
+ return tasks.repeatTask()
+
+ return wrapper
+
+
+def iterative_task(func):
+ """Iterative wrapper method
+ """
+
+ @wraps(func)
+ def wrapper(request, *args, **kwargs):
+ """Decorator wrapper method
+
+ Params usage:
+ logic: name of the logic for the data model to iterate through
+ filter: a dict for the properties that the entities should have
+ order: a list with the sort order
+ json: json object with additional parameters
+
+ Returns:
+ Standard http django response
+ """
+
+ post_dict = request.POST
+
+ if 'logic' not in post_dict:
+ return tasks.terminateTask()
+
+ _temp = __import__(post_dict['logic'], globals(), locals(), ['logic'], -1)
+ logic = _temp.logic
+
+ filter = None
+ if 'filter' in post_dict:
+ filter = simplejson.loads(post_dict['filter'])
+
+ order = None
+ if 'order' in post_dict:
+ order = simplejson.loads(post_dict['order'])
+
+ start_key = None
+ if 'next_key' in post_dict:
+ start_key = db.Key(post_dict['start_key'])
+
+ json = None
+ if 'json' in post_dict:
+ json = post_dict['json']
+
+ entities, start_key = logic.getBatchOfData(filter, order, start_key)
+
+ try:
+ new_json = func(request, entities=entities, json=json, *args, **kwargs)
+ except tasks.FatalTaskError, error:
+ logging.error(error)
+ return tasks.terminateTask()
+ except Exception, exception:
+ logging.error(exception)
+ return tasks.repeatTask()
+
+ if start_key is None:
+ logging.debug('Task sucessfully completed')
+ else:
+ context = post_dict.copy()
+
+ if 'json' in context:
+ del context['json']
+
+ context.update({'start_key': start_key})
+
+ if new_json is not None:
+ context.update({'json': new_json})
+
+ tasks.startTask(url=request.path, context=context)
+
+ return tasks.terminateTask()
+
+ return wrapper