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