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 submission.opened_link = True
52 return render(request, 'wtem/main_before.html')
55 def form_after(request, submission_id, key):
56 return render(request, 'wtem/main_after.html')
61 def form_during(request, key):
63 if CompetitionState.get_state() != CompetitionState.DURING:
64 if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
65 return HttpResponseForbidden('Not allowed')
68 submission = Submission.objects.get(key=key)
69 except Submission.DoesNotExist:
70 if settings.DEBUG and key == DEBUG_KEY:
71 submission = Submission.create(
72 first_name='Debug', last_name='Debug', email='debug@debug.com', key=DEBUG_KEY)
74 return render(request, 'wtem/key_not_found.html')
75 exercises_with_answers = deepcopy(exercises)
76 if submission.answers:
77 answers = json.loads(submission.answers)
80 for exercise in exercises_with_answers:
81 exercise['saved_answer'] = answers.get(str(exercise['id']), '')
82 if exercise['type'] == 'open' and exercise.get('fields'):
83 field_answers = {field['id']: field['text'] for field in exercise['saved_answer']}
84 for field in exercise['fields']:
85 field['saved_answer'] = field_answers.get(field['id'], '')
86 if request.method == 'GET':
87 return render(request, 'wtem/main.html', {'exercises': exercises_with_answers, 'end_time': submission.end_time})
88 elif request.method == 'POST':
89 form = WTEMForm(request.POST, request.FILES, instance=submission)
92 return render(request, 'wtem/thanks.html', dict(end_time=submission.end_time))
99 def form_single(request, submission_id, key):
100 if CompetitionState.get_state() != CompetitionState.DURING:
101 if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
102 return HttpResponseForbidden('Not allowed')
104 submission = Submission.objects.get(id=submission_id)
105 if submission.key != key:
106 return render(request, 'wtem/key_not_found.html')
108 i, exercise = submission.current_exercise()
110 exercise_count = len(exercises)
113 return render(request, 'wtem/thanks_single.html')
115 if request.method == 'GET':
116 return render(request, 'wtem/single.html', {'exercise': exercise, 'no': i, 'exercise_count': exercise_count})
117 elif request.method == 'POST':
118 form = WTEMSingleForm(request.POST, request.FILES, instance=submission)
122 except ValueError as e:
123 if e.message == 'wrong exercise id':
124 messages.error(request, u'Próba wysłania odpowiedzi ponownie lub poza kolejnością')
125 print 'wysyłam redirect', i
126 return HttpResponseRedirect(reverse('wtem_form', kwargs={'submission_id': submission_id, 'key': key}))
133 def start(request, submission_id, key):
134 state = CompetitionState.get_state()
135 if state in (CompetitionState.BEFORE, CompetitionState.AFTER):
136 return globals()['form_' + state](request, submission_id, key)
138 submission = Submission.objects.get(id=submission_id)
139 if submission.key != key:
140 return render(request, 'wtem/key_not_found.html')
142 submission.opened_link = True
145 i, exercise = submission.current_exercise()
147 return render(request, 'wtem/thanks_single.html')
149 return render(request, 'wtem/start.html', {'exercise_count': len(exercises), 'submission': submission})
152 def confirmation(request, id, key):
153 conf = get_object_or_404(Confirmation, id=id, key=key)
154 was_confirmed = conf.confirmed
155 if not was_confirmed:
156 conf.confirmed = True
158 return render(request, 'wtem/confirmed.html', {'confirmation': conf, 'was_confirmed': was_confirmed})