12 # distributed under the License is distributed on an "AS IS" BASIS, |
12 # distributed under the License is distributed on an "AS IS" BASIS, |
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 # See the License for the specific language governing permissions and |
14 # See the License for the specific language governing permissions and |
15 # limitations under the License. |
15 # limitations under the License. |
16 |
16 |
17 """Version update Tasks starters. |
17 """Version update Tasks runner. |
18 """ |
18 """ |
19 |
19 |
20 __authors__ = [ |
20 __authors__ = [ |
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
22 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
22 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
25 |
25 |
26 from django import http |
26 from django import http |
27 from django.template import loader |
27 from django.template import loader |
28 from django.utils.translation import ugettext |
28 from django.utils.translation import ugettext |
29 |
29 |
|
30 from soc.tasks.helper import error_handler |
30 from soc.views.helper import responses |
31 from soc.views.helper import responses |
31 |
32 |
32 |
33 |
33 def getDjangoURLPatterns(): |
34 def getDjangoURLPatterns(): |
34 """Returns the URL patterns for the views in this module. |
35 """Returns the URL patterns for the views in this module. |
35 """ |
36 """ |
36 |
37 |
37 patterns = [ |
38 patterns = [ |
38 (r'tasks/update/start$', 'soc.tasks.updates.start_update.startTasks'), |
39 (r'tasks/update/start$', 'soc.tasks.updates.start_update.startTasks'), |
39 (r'tasks/update/([a-z]+)$', 'soc.tasks.updates.start_update.runner')] |
40 (r'tasks/update/start/([a-z]+)$', |
|
41 'soc.tasks.updates.start_update.start_task'), |
|
42 (r'tasks/update/run/([a-z]+)$', |
|
43 'soc.tasks.updates.start_update.run_task')] |
40 |
44 |
41 return patterns |
45 return patterns |
42 |
46 |
43 |
47 |
44 def startTasks(request): |
48 def startTasks(request): |
75 content = loader.render_to_string(template, dictionary=context) |
79 content = loader.render_to_string(template, dictionary=context) |
76 return http.HttpResponse(content) |
80 return http.HttpResponse(content) |
77 |
81 |
78 |
82 |
79 class TaskRunner(object): |
83 class TaskRunner(object): |
80 """Runs one of the supported task starters. |
84 """Runs one of the supported tasks. |
81 """ |
85 """ |
|
86 |
|
87 ORG_CONVERSION = { |
|
88 'from_version': 'V-2', |
|
89 'in_version_order': 1, |
|
90 'description': ugettext('This converts the Organization models to contain X,Y,Z.'), |
|
91 'starter': lambda x:False, |
|
92 'runner': lambda x,**kwargs:http.HttpResponse('TEST OK'), |
|
93 } |
|
94 |
82 |
95 |
83 def __init__(self): |
96 def __init__(self): |
84 """Initializes the TaskRunner. |
97 """Initializes the TaskRunner. |
85 """ |
98 """ |
86 |
99 |
87 self.options = { |
100 self.options = { |
88 'program': self.programConversion(), |
101 'organization': self.ORG_CONVERSION, |
89 'student': self.studentConversion(), |
|
90 'organization': self.orgConversion(), |
|
91 } |
102 } |
92 |
103 |
93 def getOptions(self): |
104 def getOptions(self): |
94 """Returns the supported options. |
105 """Returns the supported options. |
95 """ |
106 """ |
96 |
107 |
97 return self.options |
108 return self.options |
98 |
109 |
99 def __call__(self, request, option): |
110 def startTask(self, request, option): |
100 """Starts the specified task. |
111 """Starts the specified Task for the given option. |
101 """ |
112 """ |
102 |
113 |
103 context = responses.getUniversalContext(request) |
114 context = responses.getUniversalContext(request) |
104 context['page_name'] = 'Start Update Task' |
115 context['page_name'] = 'Start Update Task' |
105 |
116 |
108 template = 'soc/error.html' |
119 template = 'soc/error.html' |
109 context['message'] = 'Uknown option "%s".' % option |
120 context['message'] = 'Uknown option "%s".' % option |
110 else: |
121 else: |
111 template = 'soc/tasks/run_update.html' |
122 template = 'soc/tasks/run_update.html' |
112 context['option'] = option |
123 context['option'] = option |
113 context['success'] = option['updater'](request) |
124 context['success'] = option['starter'](request) |
114 |
125 |
115 content = loader.render_to_string(template, dictionary=context) |
126 content = loader.render_to_string(template, dictionary=context) |
116 return http.HttpResponse(content) |
127 return http.HttpResponse(content) |
117 |
128 |
118 def programConversion(self): |
129 def runTask(self, request, option, **kwargs): |
119 """ |
130 """Runs the specified Task for the given option. |
120 """ |
131 """ |
121 |
132 |
122 description = ugettext('This converts the Program models to contain X,Y,Z. ' |
133 option = self.options.get(option) |
123 'Note that this conversion will only work after Q') |
|
124 |
134 |
125 # TODO(ljvderijk): implement this |
135 if not option: |
126 updater = lambda x:False |
136 error_handler('Uknown Updater option "%s".' % option) |
|
137 else: |
|
138 return option['runner'](request, **kwargs) |
127 |
139 |
128 conversion_information = {'from_version': 'V-1', |
|
129 'in_version_order': 2, |
|
130 'description': description, |
|
131 'updater': updater} |
|
132 |
140 |
133 return conversion_information |
141 task_runner = TaskRunner() |
134 |
142 start_task = task_runner.startTask |
135 def studentConversion(self): |
143 run_task = task_runner.runTask |
136 """ |
|
137 """ |
|
138 |
|
139 description = ugettext('This converts the Student models to contain X,Y,Z.') |
|
140 |
|
141 # TODO(ljvderijk): implement this |
|
142 updater = lambda x:False |
|
143 |
|
144 conversion_information = {'from_version': 'V-1', |
|
145 'in_version_order': 1, |
|
146 'description': description, |
|
147 'updater': updater} |
|
148 |
|
149 return conversion_information |
|
150 |
|
151 def orgConversion(self): |
|
152 """ |
|
153 """ |
|
154 |
|
155 description = ugettext('This converts the Organization models to contain X,Y,Z.') |
|
156 |
|
157 # TODO(ljvderijk): implement this |
|
158 updater = lambda x:False |
|
159 |
|
160 conversion_information = {'from_version': 'V-2', |
|
161 'in_version_order': 1, |
|
162 'description': description, |
|
163 'updater': updater} |
|
164 |
|
165 return conversion_information |
|
166 |
|
167 runner = TaskRunner() |
|