1647f099036d66a711c380a83680d99e7b7ec81e
[edumed.git] / wtem / views.py
1 import os
2
3 from django.shortcuts import render
4 from django.utils import simplejson
5 from django.conf import settings
6 from django.http import Http404
7
8 from .models import Submission
9 from .forms import WTEMForm
10
11 WTEM_CONTEST_STAGE = getattr(settings, 'WTEM_CONTEST_STAGE', 'before')
12
13
14 def main(request):
15     pass
16
17 def form(request, key):
18     return globals()['form_' + WTEM_CONTEST_STAGE](request, key)
19     
20 def form_before(request, key):
21     return render(request, 'wtem/main_before.html')
22
23 def form_after(request, key):
24     return render(request, 'wtem/main_after.html')
25
26 def form_during(request, key):
27     try:
28         submission = Submission.objects.get(key = key)
29     except Submission.DoesNotExist:
30         if settings.DEBUG and key == '12345':
31             submission = Submission.create(first_name = 'Debug', last_name = 'Debug', email = 'debug@debug.com', key = '12345')
32         else:
33             raise Http404
34
35     ## @@ move this out of the view
36     f = file(os.path.dirname(__file__) + '/fixtures/exercises.json')
37     exercises = simplejson.loads(f.read())
38     f.close()
39
40     if request.method == 'GET':
41         return render(request, 'wtem/main.html', dict(exercises = exercises))
42     elif request.method == 'POST':
43         form = WTEMForm(request.POST, request.FILES, instance = submission)
44         if form.is_valid():
45             form.save()
46             return render(request, 'wtem/thanks.html')
47         else:
48             raise Exception