fix results
[edumed.git] / wtem / views.py
index 504fa5b..465b11b 100644 (file)
@@ -1,34 +1,58 @@
 # -*- coding: utf-8 -*-
 import json
 from copy import deepcopy
+from functools import wraps
 
 from django.conf import settings
+from django.contrib import messages
+from django.core.urlresolvers import reverse
 from django.http import HttpResponseForbidden
-from django.shortcuts import render
+from django.http.response import HttpResponseRedirect
+from django.shortcuts import render, get_object_or_404
+from django.utils import timezone
+from django.utils.cache import patch_cache_control, add_never_cache_headers
 from django.views.decorators.cache import never_cache
 from django.views.decorators.csrf import csrf_exempt
 
-from .forms import WTEMForm
-from .models import Submission, DEBUG_KEY, exercises
+from wtem.models import Confirmation
+from .forms import WTEMForm, WTEMSingleForm
+from .models import Submission, DEBUG_KEY, exercises, CompetitionState
 
-WTEM_CONTEST_STAGE = getattr(settings, 'WTEM_CONTEST_STAGE', 'before')
+
+def cache_until_start(view_func):
+    @wraps(view_func)
+    def _wrapped_view_func(request, *args, **kwargs):
+        response = view_func(request, *args, **kwargs)
+        max_age = max(int((CompetitionState.start - timezone.now()).total_seconds()) + 1, 0)
+        if max_age:
+            patch_cache_control(response, max_age=max_age)
+        else:
+            add_never_cache_headers(response)
+        return response
+
+    return _wrapped_view_func
 
 
 @csrf_exempt
-def form(request, key):
-    return globals()['form_' + WTEM_CONTEST_STAGE](request, key)
+def form(request, submission_id, key):
+    state = CompetitionState.get_state()
+    if state == CompetitionState.DURING:
+        state = 'single'
+    return globals()['form_' + state](request, submission_id, key)
 
 
-def form_before(request, key):
-    try:
-        Submission.objects.get(key=key)
-    except Submission.DoesNotExist:
+@cache_until_start
+def form_before(request, submission_id, key):
+    submission = Submission.objects.get(id=submission_id)
+    if submission.key != key:
         return render(request, 'wtem/key_not_found_before.html')
     else:
+        submission.opened_link = True
+        submission.save()
         return render(request, 'wtem/main_before.html')
 
 
-def form_after(request, key):
+def form_after(request, submission_id, key):
     return render(request, 'wtem/main_after.html')
 
 
@@ -36,7 +60,7 @@ def form_after(request, key):
 @csrf_exempt
 def form_during(request, key):
 
-    if WTEM_CONTEST_STAGE != 'during':
+    if CompetitionState.get_state() != CompetitionState.DURING:
         if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
             return HttpResponseForbidden('Not allowed')
 
@@ -54,11 +78,11 @@ def form_during(request, key):
     else:
         answers = {}
     for exercise in exercises_with_answers:
-        exercise['saved_answer'] = answers.get(str(exercise['id']), {})
+        exercise['saved_answer'] = answers.get(str(exercise['id']), '')
         if exercise['type'] == 'open' and exercise.get('fields'):
             field_answers = {field['id']: field['text'] for field in exercise['saved_answer']}
             for field in exercise['fields']:
-                field['saved_answer'] = field_answers.get(field['id'])
+                field['saved_answer'] = field_answers.get(field['id'], '')
     if request.method == 'GET':
         return render(request, 'wtem/main.html', {'exercises': exercises_with_answers, 'end_time': submission.end_time})
     elif request.method == 'POST':
@@ -68,3 +92,67 @@ def form_during(request, key):
             return render(request, 'wtem/thanks.html', dict(end_time=submission.end_time))
         else:
             raise Exception
+
+
+@never_cache
+@csrf_exempt
+def form_single(request, submission_id, key):
+    if CompetitionState.get_state() != CompetitionState.DURING:
+        if request.META['REMOTE_ADDR'] not in getattr(settings, 'WTEM_CONTEST_IP_ALLOW', []):
+            return HttpResponseForbidden('Not allowed')
+
+    submission = Submission.objects.get(id=submission_id)
+    if submission.key != key:
+        return render(request, 'wtem/key_not_found.html')
+
+    i, exercise = submission.current_exercise()
+
+    exercise_count = len(exercises)
+
+    if not exercise:
+        return render(request, 'wtem/thanks_single.html')
+
+    if request.method == 'GET':
+        return render(request, 'wtem/single.html', {'exercise': exercise, 'no': i, 'exercise_count': exercise_count})
+    elif request.method == 'POST':
+        form = WTEMSingleForm(request.POST, request.FILES, instance=submission)
+        if form.is_valid():
+            try:
+                form.save()
+            except ValueError as e:
+                if e.message == 'wrong exercise id':
+                    messages.error(request, u'Próba wysłania odpowiedzi ponownie lub poza kolejnością')
+            print 'wysyłam redirect', i
+            return HttpResponseRedirect(reverse('wtem_form', kwargs={'submission_id': submission_id, 'key': key}))
+        else:
+            raise Exception
+
+
+@cache_until_start
+@csrf_exempt
+def start(request, submission_id, key):
+    state = CompetitionState.get_state()
+    if state in (CompetitionState.BEFORE, CompetitionState.AFTER):
+        return globals()['form_' + state](request, submission_id, key)
+
+    submission = Submission.objects.get(id=submission_id)
+    if submission.key != key:
+        return render(request, 'wtem/key_not_found.html')
+
+    submission.opened_link = True
+    submission.save()
+
+    i, exercise = submission.current_exercise()
+    if not exercise:
+        return render(request, 'wtem/thanks_single.html')
+
+    return render(request, 'wtem/start.html', {'exercise_count': len(exercises), 'submission': submission})
+
+
+def confirmation(request, id, key):
+    conf = get_object_or_404(Confirmation, id=id, key=key)
+    was_confirmed = conf.confirmed
+    if not was_confirmed:
+        conf.confirmed = True
+        conf.save()
+    return render(request, 'wtem/confirmed.html', {'confirmation': conf, 'was_confirmed': was_confirmed})