|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2009 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Decorators for the Task API. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Daniel Hans" <daniel.m.hans@gmail.com>', |
|
22 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
|
23 ] |
|
24 |
|
25 |
|
26 import logging |
|
27 |
|
28 from functools import wraps |
|
29 |
|
30 from soc.tasks import responses as task_responses |
|
31 |
|
32 |
|
33 def task(func): |
|
34 """Task decorator wrapper method |
|
35 """ |
|
36 |
|
37 @wraps(func) |
|
38 def wrapper(request, *args, **kwargs): |
|
39 """Decorator wrapper method |
|
40 """ |
|
41 |
|
42 try: |
|
43 return func(request, *args, **kwargs) |
|
44 except task_responses.FatalTaskError, error: |
|
45 logging.exception(error) |
|
46 return task_responses.terminateTask() |
|
47 except Exception, exception: |
|
48 logging.exception(exception) |
|
49 return task_responses.repeatTask() |
|
50 |
|
51 return wrapper |
|
52 |
|
53 |
|
54 def iterative_task(func): |
|
55 """Iterative wrapper method |
|
56 """ |
|
57 |
|
58 @wraps(func) |
|
59 def wrapper(request, *args, **kwargs): |
|
60 """Decorator wrapper method |
|
61 |
|
62 Params usage: |
|
63 logic: name of the logic for the data model to iterate through |
|
64 filter: a dict for the properties that the entities should have |
|
65 order: a list with the sort order |
|
66 json: json object with additional parameters |
|
67 |
|
68 Returns: |
|
69 Standard http django response |
|
70 """ |
|
71 |
|
72 post_dict = request.POST |
|
73 |
|
74 if 'logic' not in post_dict: |
|
75 return task_responses.terminateTask() |
|
76 |
|
77 _temp = __import__(post_dict['logic'], globals(), locals(), ['logic'], -1) |
|
78 logic = _temp.logic |
|
79 |
|
80 filter = None |
|
81 if 'filter' in post_dict: |
|
82 filter = simplejson.loads(post_dict['filter']) |
|
83 |
|
84 order = None |
|
85 if 'order' in post_dict: |
|
86 order = simplejson.loads(post_dict['order']) |
|
87 |
|
88 start_key = None |
|
89 if 'next_key' in post_dict: |
|
90 start_key = db.Key(post_dict['start_key']) |
|
91 |
|
92 json = None |
|
93 if 'json' in post_dict: |
|
94 json = post_dict['json'] |
|
95 |
|
96 entities, start_key = logic.getBatchOfData(filter, order, start_key) |
|
97 |
|
98 try: |
|
99 new_json = func(request, entities=entities, json=json, *args, **kwargs) |
|
100 except task_responses.FatalTaskError, error: |
|
101 logging.error(error) |
|
102 return task_responses.terminateTask() |
|
103 except Exception, exception: |
|
104 logging.error(exception) |
|
105 return task_responses.repeatTask() |
|
106 |
|
107 if start_key is None: |
|
108 logging.debug('Task sucessfully completed') |
|
109 else: |
|
110 context = post_dict.copy() |
|
111 |
|
112 if 'json' in context: |
|
113 del context['json'] |
|
114 |
|
115 context.update({'start_key': start_key}) |
|
116 |
|
117 if new_json is not None: |
|
118 context.update({'json': new_json}) |
|
119 |
|
120 task_responses.startTask(url=request.path, context=context) |
|
121 |
|
122 return task_responses.terminateTask() |
|
123 |
|
124 return wrapper |