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
8 from .models import Submission, DEBUG_KEY
9 from .forms import WTEMForm
11 WTEM_CONTEST_STAGE = getattr(settings, 'WTEM_CONTEST_STAGE', 'before')
14 def form(request, key):
15 return globals()['form_' + WTEM_CONTEST_STAGE](request, key)
17 def form_before(request, key):
18 return render(request, 'wtem/main_before.html')
20 def form_after(request, key):
21 return render(request, 'wtem/main_after.html')
23 def form_during(request, key):
25 if WTEM_CONTEST_STAGE != 'during':
26 if request.META['REMOTE_ADDR'] != getattr(settings, 'WTEM_CONTEST_IP_ALLOW', 'xxx'):
27 return HttpResponseForbidden('Not allowed')
30 submission = Submission.objects.get(key = key)
31 except Submission.DoesNotExist:
32 if settings.DEBUG and key == DEBUG_KEY:
33 submission = Submission.create(first_name = 'Debug', last_name = 'Debug', email = 'debug@debug.com', key = DEBUG_KEY)
37 ## @@ move this out of the view
38 f = file(os.path.dirname(__file__) + '/fixtures/exercises.json')
39 exercises = simplejson.loads(f.read())
42 if request.method == 'GET':
43 return render(request, 'wtem/main.html', dict(exercises = exercises))
44 elif request.method == 'POST':
45 form = WTEMForm(request.POST, request.FILES, instance = submission)
48 return render(request, 'wtem/thanks.html')