1 # -*- coding: utf-8 -*-
3 from copy import deepcopy
4 from functools import wraps
6 from django.conf import settings
7 from django.contrib import messages
8 from django.core.urlresolvers import reverse
9 from django.http import HttpResponseForbidden
10 from django.http.response import HttpResponseRedirect
11 from django.shortcuts import render, get_object_or_404
12 from django.utils import timezone
13 from django.utils.cache import patch_cache_control, add_never_cache_headers
14 from django.views.decorators.cache import never_cache
15 from django.views.decorators.csrf import csrf_exempt
17 from wtem.models import Confirmation
18 from .forms import WTEMForm, WTEMSingleForm
19 from .models import Submission, DEBUG_KEY, exercises, CompetitionState
22 def cache_until_start(view_func):
24 def _wrapped_view_func(request, *args, **kwargs):
25 response = view_func(request, *args, **kwargs)
26 max_age = max(int((CompetitionState.start - timezone.now()).total_seconds()) + 1, 0)
28 patch_cache_control(response, max_age=max_age)
30 add_never_cache_headers(response)
33 return _wrapped_view_func
37 def form(request, submission_id, key):
38 state = CompetitionState.get_state()
39 if state == CompetitionState.DURING:
41 return globals()['form_' + state](request, submission_id, key)
45 def form_before(request, submission_id, key):
46 submission = Submission.objects.get(id=submission_id)
47 if submission.key != key:
48 return render(request, 'wtem/key_not_found_before.html')
50 return render(request, 'wtem/main_before.html')
53 def form_after(request, submission_id, key):
54 return render(request, 'wtem/main_after.html')
59 def form_during(request, key):
61 if CompetitionState.get_state() != CompetitionState.DURING:
62 if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
63 return HttpResponseForbidden('Not allowed')
66 submission = Submission.objects.get(key=key)
67 except Submission.DoesNotExist:
68 if settings.DEBUG and key == DEBUG_KEY:
69 submission = Submission.create(
70 first_name='Debug', last_name='Debug', email='debug@debug.com', key=DEBUG_KEY)
72 return render(request, 'wtem/key_not_found.html')
73 exercises_with_answers = deepcopy(exercises)
74 if submission.answers:
75 answers = json.loads(submission.answers)
78 for exercise in exercises_with_answers:
79 exercise['saved_answer'] = answers.get(str(exercise['id']), '')
80 if exercise['type'] == 'open' and exercise.get('fields'):
81 field_answers = {field['id']: field['text'] for field in exercise['saved_answer']}
82 for field in exercise['fields']:
83 field['saved_answer'] = field_answers.get(field['id'], '')
84 if request.method == 'GET':
85 return render(request, 'wtem/main.html', {'exercises': exercises_with_answers, 'end_time': submission.end_time})
86 elif request.method == 'POST':
87 form = WTEMForm(request.POST, request.FILES, instance=submission)
90 return render(request, 'wtem/thanks.html', dict(end_time=submission.end_time))
97 def form_single(request, submission_id, key):
98 if CompetitionState.get_state() != CompetitionState.DURING:
99 if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
100 return HttpResponseForbidden('Not allowed')
102 submission = Submission.objects.get(id=submission_id)
103 if submission.key != key:
104 return render(request, 'wtem/key_not_found.html')
106 i, exercise = submission.current_exercise()
108 exercise_count = len(exercises)
111 return render(request, 'wtem/thanks_single.html')
113 if request.method == 'GET':
114 return render(request, 'wtem/single.html', {'exercise': exercise, 'no': i, 'exercise_count': exercise_count})
115 elif request.method == 'POST':
116 form = WTEMSingleForm(request.POST, request.FILES, instance=submission)
120 except ValueError as e:
121 if e.message == 'wrong exercise id':
122 messages.error(request, u'Próba wysłania odpowiedzi ponownie lub poza kolejnością')
123 print 'wysyłam redirect', i
124 return HttpResponseRedirect(reverse('wtem_form', kwargs={'submission_id': submission_id, 'key': key}))
131 def start(request, submission_id, key):
132 state = CompetitionState.get_state()
133 if state in (CompetitionState.BEFORE, CompetitionState.AFTER):
134 return globals()['form_' + state](request, submission_id, key)
136 submission = Submission.objects.get(id=submission_id)
137 if submission.key != key:
138 return render(request, 'wtem/key_not_found.html')
140 i, exercise = submission.current_exercise()
142 return render(request, 'wtem/thanks_single.html')
144 return render(request, 'wtem/start.html', {'exercise_count': len(exercises), 'submission': submission})
147 def confirmation(request, id, key):
148 conf = get_object_or_404(Confirmation, id=id, key=key)
149 was_confirmed = conf.confirmed
150 if not was_confirmed:
151 conf.confirmed = True
153 return render(request, 'wtem/confirmed.html', {'confirmation': conf, 'was_confirmed': was_confirmed})