og properties for olympiad
[edumed.git] / wtem / views.py
1 # -*- coding: utf-8 -*-
2
3 from django.conf import settings
4 from django.http import HttpResponseForbidden
5 from django.shortcuts import render
6 from django.views.decorators.cache import never_cache
7 from django.views.decorators.csrf import csrf_exempt
8
9 from .forms import WTEMForm
10 from .models import Submission, DEBUG_KEY, exercises
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
20 def form_before(request, key):
21     try:
22         Submission.objects.get(key=key)
23     except Submission.DoesNotExist:
24         return render(request, 'wtem/key_not_found_before.html')
25     else:
26         return render(request, 'wtem/main_before.html')
27
28
29 def form_after(request, key):
30     return render(request, 'wtem/main_after.html')
31
32
33 @never_cache
34 @csrf_exempt
35 def form_during(request, key):
36
37     if WTEM_CONTEST_STAGE != 'during':
38         if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
39             return HttpResponseForbidden('Not allowed')
40
41     try:
42         submission = Submission.objects.get(key=key)
43     except Submission.DoesNotExist:
44         if settings.DEBUG and key == DEBUG_KEY:
45             submission = Submission.create(
46                 first_name='Debug', last_name='Debug', email='debug@debug.com', key=DEBUG_KEY)
47         else:
48             return render(request, 'wtem/key_not_found.html')
49     if request.method == 'GET':
50         return render(request, 'wtem/main.html', dict(exercises=exercises, end_time=submission.end_time))
51     elif request.method == 'POST':
52         form = WTEMForm(request.POST, request.FILES, instance=submission)
53         if form.is_valid():
54             form.save()
55             return render(request, 'wtem/thanks.html', dict(end_time=submission.end_time))
56         else:
57             raise Exception