Fixes #3315: Lessons ordering.
[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, HttpResponseForbidden
7 from django.views.decorators.csrf import csrf_exempt
8
9 from .models import Submission, DEBUG_KEY, exercises
10 from .forms import WTEMForm
11
12 WTEM_CONTEST_STAGE = getattr(settings, 'WTEM_CONTEST_STAGE', 'before')
13
14
15 @csrf_exempt
16 def form(request, key):
17     return globals()['form_' + WTEM_CONTEST_STAGE](request, key)
18     
19 def form_before(request, key):
20     try:
21         submission = Submission.objects.get(key = key)
22     except:
23         return render(request, 'wtem/key_not_found_before.html')
24     else:
25         return render(request, 'wtem/main_before.html')
26
27 def form_after(request, key):
28     return render(request, 'wtem/main_after.html')
29
30 @csrf_exempt
31 def form_during(request, key):
32
33     if WTEM_CONTEST_STAGE != 'during':
34         if request.META['REMOTE_ADDR'] != getattr(settings, 'WTEM_CONTEST_IP_ALLOW', 'xxx'):
35             return HttpResponseForbidden('Not allowed')
36
37     try:
38         submission = Submission.objects.get(key = key)
39     except Submission.DoesNotExist:
40         if settings.DEBUG and key == DEBUG_KEY:
41             submission = Submission.create(first_name = 'Debug', last_name = 'Debug', email = 'debug@debug.com', key = DEBUG_KEY)
42         else:
43             return render(request, 'wtem/key_not_found.html')
44     if request.method == 'GET':
45         return render(request, 'wtem/main.html', dict(exercises = exercises, end_time = submission.end_time))
46     elif request.method == 'POST':
47         form = WTEMForm(request.POST, request.FILES, instance = submission)
48         if form.is_valid():
49             form.save()
50             return render(request, 'wtem/thanks.html', dict(end_time = submission.end_time))
51         else:
52             raise Exception