1 # -*- coding: utf-8 -*-
3 from copy import deepcopy
5 from django.conf import settings
6 from django.core.urlresolvers import reverse
7 from django.http import HttpResponseForbidden
8 from django.http.response import HttpResponseRedirect
9 from django.shortcuts import render, get_object_or_404
10 from django.views.decorators.cache import never_cache
11 from django.views.decorators.csrf import csrf_exempt
13 from wtem.models import Confirmation
14 from .forms import WTEMForm, WTEMSingleForm
15 from .models import Submission, DEBUG_KEY, exercises, CompetitionState
17 WTEM_CONTEST_STAGE = getattr(settings, 'WTEM_CONTEST_STAGE', 'before')
21 def form(request, key):
22 return globals()['form_' + WTEM_CONTEST_STAGE](request, key)
25 def form_before(request, key):
27 Submission.objects.get(key=key)
28 except Submission.DoesNotExist:
29 return render(request, 'wtem/key_not_found_before.html')
31 return render(request, 'wtem/main_before.html')
34 def form_after(request, key):
35 return render(request, 'wtem/main_after.html')
40 def form_during(request, key):
42 if WTEM_CONTEST_STAGE != 'during':
43 if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
44 return HttpResponseForbidden('Not allowed')
47 submission = Submission.objects.get(key=key)
48 except Submission.DoesNotExist:
49 if settings.DEBUG and key == DEBUG_KEY:
50 submission = Submission.create(
51 first_name='Debug', last_name='Debug', email='debug@debug.com', key=DEBUG_KEY)
53 return render(request, 'wtem/key_not_found.html')
54 exercises_with_answers = deepcopy(exercises)
55 if submission.answers:
56 answers = json.loads(submission.answers)
59 for exercise in exercises_with_answers:
60 exercise['saved_answer'] = answers.get(str(exercise['id']), '')
61 if exercise['type'] == 'open' and exercise.get('fields'):
62 field_answers = {field['id']: field['text'] for field in exercise['saved_answer']}
63 for field in exercise['fields']:
64 field['saved_answer'] = field_answers.get(field['id'], '')
65 if request.method == 'GET':
66 return render(request, 'wtem/main.html', {'exercises': exercises_with_answers, 'end_time': submission.end_time})
67 elif request.method == 'POST':
68 form = WTEMForm(request.POST, request.FILES, instance=submission)
71 return render(request, 'wtem/thanks.html', dict(end_time=submission.end_time))
78 def form_single(request, submission_id, key):
79 if CompetitionState.get_state() != CompetitionState.DURING:
80 if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
81 return HttpResponseForbidden('Not allowed')
83 submission = Submission.objects.get(id=submission_id)
84 if submission.key != key:
85 return render(request, 'wtem/key_not_found.html')
87 i, exercise = submission.current_exercise()
90 return render(request, 'wtem/thanks_single.html')
92 if request.method == 'GET':
93 return render(request, 'wtem/single.html', {'exercise': exercise, 'no': i})
94 elif request.method == 'POST':
95 form = WTEMSingleForm(request.POST, request.FILES, instance=submission)
98 return HttpResponseRedirect(reverse('form_single', kwargs={'submission_id': submission_id, 'key': key}))
103 def confirmation(request, id, key):
104 conf = get_object_or_404(Confirmation, id=id, key=key)
105 was_confirmed = conf.confirmed
106 if not was_confirmed:
107 conf.confirmed = True
109 return render(request, 'wtem/confirmed.html', {'confirmation': conf, 'was_confirmed': was_confirmed})