1 # -*- coding: utf-8 -*-
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
9 from .forms import WTEMForm
10 from .models import Submission, DEBUG_KEY, exercises
12 WTEM_CONTEST_STAGE = getattr(settings, 'WTEM_CONTEST_STAGE', 'before')
16 def form(request, key):
17 return globals()['form_' + WTEM_CONTEST_STAGE](request, key)
20 def form_before(request, key):
22 Submission.objects.get(key=key)
23 except Submission.DoesNotExist:
24 return render(request, 'wtem/key_not_found_before.html')
26 return render(request, 'wtem/main_before.html')
29 def form_after(request, key):
30 return render(request, 'wtem/main_after.html')
35 def form_during(request, key):
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')
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)
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)
55 return render(request, 'wtem/thanks.html', dict(end_time=submission.end_time))