1 # -*- coding: utf-8 -*-
3 from copy import deepcopy
5 from django.conf import settings
6 from django.contrib import messages
7 from django.core.urlresolvers import reverse
8 from django.http import HttpResponseForbidden
9 from django.http.response import HttpResponseRedirect
10 from django.shortcuts import render, get_object_or_404
11 from django.views.decorators.cache import never_cache
12 from django.views.decorators.csrf import csrf_exempt
14 from wtem.models import Confirmation
15 from .forms import WTEMForm, WTEMSingleForm
16 from .models import Submission, DEBUG_KEY, exercises, CompetitionState
20 def form(request, submission_id, key):
21 state = CompetitionState.get_state()
22 if state == CompetitionState.DURING:
24 return globals()['form_' + state](request, submission_id, key)
27 def form_before(request, submission_id, key):
28 submission = Submission.objects.get(id=submission_id)
29 if submission.key != key:
30 return render(request, 'wtem/key_not_found_before.html')
32 return render(request, 'wtem/main_before.html')
35 def form_after(request, submission_id, key):
36 return render(request, 'wtem/main_after.html')
41 def form_during(request, key):
43 if CompetitionState.get_state() != CompetitionState.DURING:
44 if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
45 return HttpResponseForbidden('Not allowed')
48 submission = Submission.objects.get(key=key)
49 except Submission.DoesNotExist:
50 if settings.DEBUG and key == DEBUG_KEY:
51 submission = Submission.create(
52 first_name='Debug', last_name='Debug', email='debug@debug.com', key=DEBUG_KEY)
54 return render(request, 'wtem/key_not_found.html')
55 exercises_with_answers = deepcopy(exercises)
56 if submission.answers:
57 answers = json.loads(submission.answers)
60 for exercise in exercises_with_answers:
61 exercise['saved_answer'] = answers.get(str(exercise['id']), '')
62 if exercise['type'] == 'open' and exercise.get('fields'):
63 field_answers = {field['id']: field['text'] for field in exercise['saved_answer']}
64 for field in exercise['fields']:
65 field['saved_answer'] = field_answers.get(field['id'], '')
66 if request.method == 'GET':
67 return render(request, 'wtem/main.html', {'exercises': exercises_with_answers, 'end_time': submission.end_time})
68 elif request.method == 'POST':
69 form = WTEMForm(request.POST, request.FILES, instance=submission)
72 return render(request, 'wtem/thanks.html', dict(end_time=submission.end_time))
79 def form_single(request, submission_id, key):
80 if CompetitionState.get_state() != CompetitionState.DURING:
81 if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
82 return HttpResponseForbidden('Not allowed')
84 submission = Submission.objects.get(id=submission_id)
85 if submission.key != key:
86 return render(request, 'wtem/key_not_found.html')
88 i, exercise = submission.current_exercise()
90 exercise_count = len(exercises)
93 return render(request, 'wtem/thanks_single.html')
95 if request.method == 'GET':
96 return render(request, 'wtem/single.html', {'exercise': exercise, 'no': i, 'exercise_count': exercise_count})
97 elif request.method == 'POST':
98 form = WTEMSingleForm(request.POST, request.FILES, instance=submission)
102 except ValueError as e:
103 if e.message == 'wrong exercise id':
104 messages.error(request, u'Próba wysłania odpowiedzi ponownie lub poza kolejnością')
105 print 'wysyłam redirect', i
106 return HttpResponseRedirect(reverse('wtem_form', kwargs={'submission_id': submission_id, 'key': key}))
113 def start(request, submission_id, key):
114 state = CompetitionState.get_state()
115 if state in (CompetitionState.BEFORE, CompetitionState.AFTER):
116 return globals()['form_' + state](request, submission_id, key)
118 submission = Submission.objects.get(id=submission_id)
119 if submission.key != key:
120 return render(request, 'wtem/key_not_found.html')
122 i, exercise = submission.current_exercise()
124 return render(request, 'wtem/thanks_single.html')
126 return render(request, 'wtem/start.html', {'exercise_count': len(exercises), 'submission': submission})
129 def confirmation(request, id, key):
130 conf = get_object_or_404(Confirmation, id=id, key=key)
131 was_confirmed = conf.confirmed
132 if not was_confirmed:
133 conf.confirmed = True
135 return render(request, 'wtem/confirmed.html', {'confirmation': conf, 'was_confirmed': was_confirmed})