6366e535db309b2dae410858b1e5ab371b0c4d0c
[edumed.git] / wtem / views.py
1 # -*- coding: utf-8 -*-
2 import json
3 from copy import deepcopy
4
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
12
13 from wtem.models import Confirmation
14 from .forms import WTEMForm, WTEMSingleForm
15 from .models import Submission, DEBUG_KEY, exercises, CompetitionState
16
17 WTEM_CONTEST_STAGE = getattr(settings, 'WTEM_CONTEST_STAGE', 'before')
18
19
20 @csrf_exempt
21 def form(request, key):
22     return globals()['form_' + WTEM_CONTEST_STAGE](request, key)
23
24
25 def form_before(request, key):
26     try:
27         Submission.objects.get(key=key)
28     except Submission.DoesNotExist:
29         return render(request, 'wtem/key_not_found_before.html')
30     else:
31         return render(request, 'wtem/main_before.html')
32
33
34 def form_after(request, key):
35     return render(request, 'wtem/main_after.html')
36
37
38 @never_cache
39 @csrf_exempt
40 def form_during(request, key):
41
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')
45
46     try:
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)
52         else:
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)
57     else:
58         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)
69         if form.is_valid():
70             form.save()
71             return render(request, 'wtem/thanks.html', dict(end_time=submission.end_time))
72         else:
73             raise Exception
74
75
76 @never_cache
77 @csrf_exempt
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')
82
83     submission = Submission.objects.get(id=submission_id)
84     if submission.key != key:
85         return render(request, 'wtem/key_not_found.html')
86
87     i, exercise = submission.current_exercise()
88
89     if not exercise:
90         return render(request, 'wtem/thanks_single.html')
91
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)
96         if form.is_valid():
97             form.save()
98             return HttpResponseRedirect(reverse('form_single', kwargs={'submission_id': submission_id, 'key': key}))
99         else:
100             raise Exception
101
102
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
108         conf.save()
109     return render(request, 'wtem/confirmed.html', {'confirmation': conf, 'was_confirmed': was_confirmed})