1 | #----------------------------------------------------------------------------- |
---|
2 | # preamble {{{ |
---|
3 | #----------------------------------------------------------------------------- |
---|
4 | # imports {{{ |
---|
5 | |
---|
6 | from django.http import HttpResponse |
---|
7 | from django.template import Context, loader |
---|
8 | from wiki.models import WikiPage, Macro, MacroResultType, MacroProtocol |
---|
9 | |
---|
10 | # TODO: enable CSRF protection |
---|
11 | from django.views.decorators.csrf import csrf_exempt |
---|
12 | |
---|
13 | import debug |
---|
14 | |
---|
15 | # }}} |
---|
16 | #----------------------------------------------------------------------------- |
---|
17 | # tooling {{{ |
---|
18 | |
---|
19 | def load_wiki_or_null(page_name): |
---|
20 | from django.core.exceptions import ObjectDoesNotExist |
---|
21 | |
---|
22 | try: |
---|
23 | page = WikiPage.objects.get(name = page_name) |
---|
24 | except ObjectDoesNotExist, e: |
---|
25 | page = None |
---|
26 | |
---|
27 | return page |
---|
28 | |
---|
29 | # }}} |
---|
30 | #----------------------------------------------------------------------------- |
---|
31 | # }}} |
---|
32 | #----------------------------------------------------------------------------- |
---|
33 | # wiki pages {{{ |
---|
34 | #----------------------------------------------------------------------------- |
---|
35 | |
---|
36 | @csrf_exempt |
---|
37 | def handle_wiki(request, page_name): |
---|
38 | if 'cancel' in request.POST: |
---|
39 | return redirect_to_display_wiki(request, page_name) |
---|
40 | elif 'save' in request.POST: |
---|
41 | return save_wiki(request, page_name) |
---|
42 | elif 'edit' in request.POST or 'edit' in request.GET: |
---|
43 | return edit_wiki(request, page_name) |
---|
44 | elif 'remove' in request.POST: |
---|
45 | return remove_wiki(request, page_name) |
---|
46 | else: |
---|
47 | return display_wiki(request, page_name) |
---|
48 | |
---|
49 | #----------------------------------------------------------------------------- |
---|
50 | # display_wiki() {{{ |
---|
51 | |
---|
52 | @csrf_exempt |
---|
53 | def display_wiki(request, page_name): |
---|
54 | template = loader.get_template('display_wiki.html') |
---|
55 | |
---|
56 | page = load_wiki_or_null(page_name) |
---|
57 | context = Context({ |
---|
58 | 'name': page_name, |
---|
59 | 'body': page.content if page else None, |
---|
60 | 'editable': True, |
---|
61 | }) |
---|
62 | |
---|
63 | return HttpResponse(template.render(context)) |
---|
64 | |
---|
65 | # }}} |
---|
66 | #----------------------------------------------------------------------------- |
---|
67 | # edit_wiki() {{{ |
---|
68 | |
---|
69 | @csrf_exempt |
---|
70 | def edit_wiki(request, page_name): |
---|
71 | template = loader.get_template('edit_wiki.html') |
---|
72 | |
---|
73 | page = load_wiki_or_null(page_name) |
---|
74 | context = Context({ |
---|
75 | 'name': page_name, |
---|
76 | 'body': page.content if page else None, |
---|
77 | 'form_target': request.path, |
---|
78 | }) |
---|
79 | |
---|
80 | return HttpResponse(template.render(context)) |
---|
81 | |
---|
82 | # }}} |
---|
83 | #----------------------------------------------------------------------------- |
---|
84 | # save_wiki() {{{ |
---|
85 | |
---|
86 | @csrf_exempt |
---|
87 | def save_wiki(request, page_name): |
---|
88 | template = loader.get_template('display_wiki.html') |
---|
89 | page = load_wiki_or_null(page_name) |
---|
90 | if page == None: |
---|
91 | page = WikiPage(name=page_name) |
---|
92 | page.content = request.POST['body'] |
---|
93 | page.save() |
---|
94 | context = Context({ |
---|
95 | 'name': page_name, |
---|
96 | 'body': page.content if page else None, |
---|
97 | 'editable': True, |
---|
98 | 'message': 'Wiki page saved' |
---|
99 | }) |
---|
100 | |
---|
101 | return HttpResponse(template.render(context)) |
---|
102 | |
---|
103 | # }}} |
---|
104 | #----------------------------------------------------------------------------- |
---|
105 | # redirect_to_display_wiki() {{{ |
---|
106 | |
---|
107 | @csrf_exempt |
---|
108 | def redirect_to_display_wiki(request, page_name): |
---|
109 | response = HttpResponse(status = 302) |
---|
110 | response['location'] = request.path |
---|
111 | return response |
---|
112 | |
---|
113 | # }}} |
---|
114 | #----------------------------------------------------------------------------- |
---|
115 | # create_wiki() {{{ |
---|
116 | |
---|
117 | @csrf_exempt |
---|
118 | def create_wiki(request): |
---|
119 | if 'cancel' in request.POST: |
---|
120 | from django.core.urlresolvers import reverse |
---|
121 | response = HttpResponse(status = 302) |
---|
122 | response['location'] = reverse(handle_wiki, args = ['WikiStart']) |
---|
123 | return response |
---|
124 | |
---|
125 | template = loader.get_template('create_wiki.html') |
---|
126 | |
---|
127 | proposed_name = request.POST.get('page_name', None) |
---|
128 | if proposed_name: |
---|
129 | import re |
---|
130 | if not re.match('^[A-Z][A-Za-z0-9_/]+$', proposed_name): |
---|
131 | context = Context({ |
---|
132 | 'error': 'invalid name', |
---|
133 | 'page_name': proposed_name, |
---|
134 | }) |
---|
135 | return HttpResponse(template.render(context)) |
---|
136 | |
---|
137 | if WikiPage.objects.filter(name = proposed_name).exists(): |
---|
138 | context = Context({ |
---|
139 | 'error': 'page already exists', |
---|
140 | 'page_name': proposed_name, |
---|
141 | }) |
---|
142 | return HttpResponse(template.render(context)) |
---|
143 | |
---|
144 | context = Context({ |
---|
145 | 'message': 'Page %s created' % proposed_name, |
---|
146 | 'page_name': proposed_name, |
---|
147 | 'created': True, |
---|
148 | }) |
---|
149 | return HttpResponse(template.render(context)) |
---|
150 | |
---|
151 | context = Context({}) |
---|
152 | return HttpResponse(template.render(context)) |
---|
153 | |
---|
154 | # }}} |
---|
155 | #----------------------------------------------------------------------------- |
---|
156 | # remove_wiki() {{{ |
---|
157 | |
---|
158 | @csrf_exempt |
---|
159 | def remove_wiki(request, page_name): |
---|
160 | template = loader.get_template('remove_wiki.html') |
---|
161 | page = load_wiki_or_null(page_name) |
---|
162 | if 'confirm' in request.POST: |
---|
163 | # FIXME: How about page = None? |
---|
164 | page.delete() |
---|
165 | page = None |
---|
166 | context = Context({ |
---|
167 | 'name': page_name, |
---|
168 | 'form_target': request.path, |
---|
169 | 'confirmed': ('confirm' in request.POST), |
---|
170 | }) |
---|
171 | |
---|
172 | return HttpResponse(template.render(context)) |
---|
173 | |
---|
174 | # }}} |
---|
175 | #----------------------------------------------------------------------------- |
---|
176 | # }}} |
---|
177 | #----------------------------------------------------------------------------- |
---|
178 | # navigation {{{ |
---|
179 | #----------------------------------------------------------------------------- |
---|
180 | # list_history() {{{ |
---|
181 | |
---|
182 | def list_history(request): |
---|
183 | template = loader.get_template('list_history.html') |
---|
184 | context = Context({}) |
---|
185 | return HttpResponse(template.render(context)) |
---|
186 | |
---|
187 | # }}} |
---|
188 | #----------------------------------------------------------------------------- |
---|
189 | # list_pages() {{{ |
---|
190 | |
---|
191 | def list_pages(request): |
---|
192 | template = loader.get_template('list_pages.html') |
---|
193 | |
---|
194 | pages = [page.name for page in WikiPage.objects.all()] |
---|
195 | pages.sort() |
---|
196 | |
---|
197 | context = Context({ |
---|
198 | 'pages': pages, |
---|
199 | }) |
---|
200 | return HttpResponse(template.render(context)) |
---|
201 | |
---|
202 | # }}} |
---|
203 | #----------------------------------------------------------------------------- |
---|
204 | # }}} |
---|
205 | #----------------------------------------------------------------------------- |
---|
206 | # macros {{{ |
---|
207 | #----------------------------------------------------------------------------- |
---|
208 | # utilities {{{ |
---|
209 | |
---|
210 | def save_macro_to_database(name, url, in_args, out_args, result_type_name, protocol_name): |
---|
211 | result_type = MacroResultType.objects.get(name = result_type_name) |
---|
212 | protocol = MacroProtocol.objects.get(name = protocol_name) |
---|
213 | new_macro = Macro.objects.get_or_create( |
---|
214 | name = name, |
---|
215 | defaults = {'result_type' : result_type, 'protocol' : protocol} |
---|
216 | ) |
---|
217 | new_macro = new_macro[0] |
---|
218 | new_macro.url = url |
---|
219 | new_macro.in_args = in_args |
---|
220 | new_macro.out_args = out_args |
---|
221 | new_macro.result_type = result_type |
---|
222 | new_macro.protocol = protocol |
---|
223 | new_macro.save() |
---|
224 | return new_macro |
---|
225 | |
---|
226 | def macro_supported_result_types(): |
---|
227 | return [t.name for t in MacroResultType.objects.all()] |
---|
228 | |
---|
229 | def macro_supported_protocols(): |
---|
230 | return [p.name for p in MacroProtocol.objects.all()] |
---|
231 | |
---|
232 | # }}} |
---|
233 | #----------------------------------------------------------------------------- |
---|
234 | # list_macros() {{{ |
---|
235 | |
---|
236 | def list_macros(request): |
---|
237 | template = loader.get_template('list_macros.html') |
---|
238 | all_macros = Macro.objects.all() |
---|
239 | macros = [] |
---|
240 | for macro in all_macros: |
---|
241 | tmp = { |
---|
242 | 'name' : macro.name, |
---|
243 | 'in_args' : macro.in_args, |
---|
244 | 'out_args' : macro.out_args, |
---|
245 | 'destination' : macro.url, |
---|
246 | 'result' : macro.result_type.name, |
---|
247 | 'protocol' : macro.protocol.name, |
---|
248 | } |
---|
249 | macros.append(tmp) |
---|
250 | context = Context({ |
---|
251 | 'macros': macros, |
---|
252 | }) |
---|
253 | return HttpResponse(template.render(context)) |
---|
254 | |
---|
255 | # }}} |
---|
256 | #----------------------------------------------------------------------------- |
---|
257 | # create_macro() {{{ |
---|
258 | |
---|
259 | @csrf_exempt |
---|
260 | def create_macro(request): |
---|
261 | if 'cancel' in request.POST: |
---|
262 | from django.core.urlresolvers import reverse |
---|
263 | response = HttpResponse(status = 302) |
---|
264 | response['location'] = reverse(list_macros) |
---|
265 | return response |
---|
266 | |
---|
267 | template = loader.get_template('create_macro.html') |
---|
268 | macro = None |
---|
269 | |
---|
270 | if 'save' in request.POST: |
---|
271 | post = request.POST |
---|
272 | macro = { |
---|
273 | 'name' : post.get('name'), |
---|
274 | 'in_args' : post.get('in_args'), |
---|
275 | 'out_args' : post.get('out_args'), |
---|
276 | 'destination' : post.get('destination'), |
---|
277 | 'result' : post.get('result'), |
---|
278 | 'protocol' : post.get('protocol'), |
---|
279 | } |
---|
280 | save_macro_to_database( |
---|
281 | name = post['name'], |
---|
282 | url = post['destination'], |
---|
283 | in_args = post['in_args'], |
---|
284 | out_args = post['out_args'], |
---|
285 | result_type_name = post['result'], |
---|
286 | protocol_name = post['protocol'] |
---|
287 | ) |
---|
288 | context = Context({ |
---|
289 | 'macro': macro, |
---|
290 | 'message': 'macro created', |
---|
291 | 'created': True, |
---|
292 | 'supported_protocols': macro_supported_protocols(), |
---|
293 | 'supported_result_types': macro_supported_result_types(), |
---|
294 | }) |
---|
295 | else: |
---|
296 | context = Context({ |
---|
297 | 'macro': macro, |
---|
298 | 'supported_protocols': macro_supported_protocols(), |
---|
299 | 'supported_result_types': macro_supported_result_types(), |
---|
300 | }) |
---|
301 | |
---|
302 | return HttpResponse(template.render(context)) |
---|
303 | |
---|
304 | # }}} |
---|
305 | #----------------------------------------------------------------------------- |
---|
306 | # edit_macro() {{{ |
---|
307 | |
---|
308 | @csrf_exempt |
---|
309 | def edit_macro(request, macro_name): |
---|
310 | if 'cancel' in request.POST: |
---|
311 | from django.core.urlresolvers import reverse |
---|
312 | response = HttpResponse(status = 302) |
---|
313 | response['location'] = reverse(list_macros) |
---|
314 | return response |
---|
315 | |
---|
316 | template = loader.get_template('edit_macro.html') |
---|
317 | macro_to_edit = Macro.objects.get(name = macro_name) |
---|
318 | macro = { |
---|
319 | 'name' : macro_to_edit.name, |
---|
320 | 'in_args' : macro_to_edit.in_args, |
---|
321 | 'out_args' : macro_to_edit.out_args, |
---|
322 | 'destination' : macro_to_edit.url, |
---|
323 | 'result' : macro_to_edit.result_type.name, |
---|
324 | 'protocol' : macro_to_edit.protocol.name, |
---|
325 | } |
---|
326 | if 'save' in request.POST: |
---|
327 | post = request.POST |
---|
328 | macro = { |
---|
329 | 'name' : post.get('name'), |
---|
330 | 'in_args' : post.get('in_args'), |
---|
331 | 'out_args' : post.get('out_args'), |
---|
332 | 'destination' : post.get('destination'), |
---|
333 | 'result' : post.get('result'), |
---|
334 | 'protocol' : post.get('protocol'), |
---|
335 | } |
---|
336 | save_macro_to_database( |
---|
337 | name = post['name'], |
---|
338 | url = post['destination'], |
---|
339 | in_args = post['in_args'], |
---|
340 | out_args = post['out_args'], |
---|
341 | result_type_name = post['result'], |
---|
342 | protocol_name = post['protocol'] |
---|
343 | ) |
---|
344 | context = Context({ |
---|
345 | 'macro': macro, |
---|
346 | 'message': 'macro saved', |
---|
347 | 'created': True, |
---|
348 | 'supported_protocols': macro_supported_protocols(), |
---|
349 | 'supported_result_types': macro_supported_result_types(), |
---|
350 | }) |
---|
351 | else: |
---|
352 | context = Context({ |
---|
353 | 'macro': macro, |
---|
354 | 'supported_protocols': macro_supported_protocols(), |
---|
355 | 'supported_result_types': macro_supported_result_types(), |
---|
356 | }) |
---|
357 | |
---|
358 | return HttpResponse(template.render(context)) |
---|
359 | |
---|
360 | # }}} |
---|
361 | #----------------------------------------------------------------------------- |
---|
362 | # remove_macro() {{{ |
---|
363 | |
---|
364 | @csrf_exempt |
---|
365 | def remove_macro(request, macro_name): |
---|
366 | if 'cancel' in request.POST: |
---|
367 | from django.core.urlresolvers import reverse |
---|
368 | response = HttpResponse(status = 302) |
---|
369 | response['location'] = reverse(list_macros) |
---|
370 | return response |
---|
371 | |
---|
372 | template = loader.get_template('remove_macro.html') |
---|
373 | |
---|
374 | if 'confirm' in request.POST: |
---|
375 | Macro.objects.filter(name = macro_name).delete() |
---|
376 | |
---|
377 | context = Context({ |
---|
378 | 'name': macro_name, |
---|
379 | 'confirmed': ('confirm' in request.POST), |
---|
380 | }) |
---|
381 | |
---|
382 | return HttpResponse(template.render(context)) |
---|
383 | |
---|
384 | # }}} |
---|
385 | #----------------------------------------------------------------------------- |
---|
386 | # }}} |
---|
387 | #----------------------------------------------------------------------------- |
---|
388 | # sessions {{{ |
---|
389 | #----------------------------------------------------------------------------- |
---|
390 | # login() {{{ |
---|
391 | |
---|
392 | @csrf_exempt |
---|
393 | def login(request): |
---|
394 | if 'cancel' in request.POST: |
---|
395 | from django.core.urlresolvers import reverse |
---|
396 | response = HttpResponse(status = 302) |
---|
397 | response['location'] = reverse(handle_wiki, args = ['WikiStart']) |
---|
398 | return response |
---|
399 | |
---|
400 | if 'submit' in request.POST: |
---|
401 | if request.POST['user_name'] in \ |
---|
402 | ['tdrag', 'mbaczynska', 'sklekot', 'bzmudzinski']: |
---|
403 | context = Context({ |
---|
404 | 'success': True, |
---|
405 | 'tried': True, |
---|
406 | 'message': 'Logged in successfully.', |
---|
407 | }) |
---|
408 | else: |
---|
409 | context = Context({ |
---|
410 | 'success': False, |
---|
411 | 'tried': True, |
---|
412 | 'error': 'Login failure. Incorrect user name or password.', |
---|
413 | }) |
---|
414 | else: |
---|
415 | context = Context({}) |
---|
416 | |
---|
417 | template = loader.get_template('login.html') |
---|
418 | return HttpResponse(template.render(context)) |
---|
419 | |
---|
420 | # }}} |
---|
421 | #----------------------------------------------------------------------------- |
---|
422 | # logout() {{{ |
---|
423 | |
---|
424 | def logout(request): |
---|
425 | context = Context({ |
---|
426 | 'message': 'User logged out.', |
---|
427 | }) |
---|
428 | template = loader.get_template('logout.html') |
---|
429 | return HttpResponse(template.render(context)) |
---|
430 | |
---|
431 | # }}} |
---|
432 | #----------------------------------------------------------------------------- |
---|
433 | # }}} |
---|
434 | #----------------------------------------------------------------------------- |
---|
435 | # vim:ft=python:foldmethod=marker |
---|