127710ac8aaae62d8540c851fd9435fa02f6b471
[edumed.git] / wtem / views.py
1 import os
2
3 from django.shortcuts import render
4 from django.utils import simplejson
5 from django.conf import settings
6 from django.http import Http404
7
8 from .models import Submission
9 from .forms import WTEMForm
10
11
12 def main(request):
13     pass
14
15 def form(request, key):
16     try:
17         submission = Submission.objects.get(key = key)
18     except Submission.DoesNotExist:
19         if settings.DEBUG and key == '12345':
20             submission = Submission.create(first_name = 'Debug', last_name = 'Debug', email = 'debug@debug.com', key = '12345')[0]
21         else:
22             raise Http404
23
24     ## @@ move this out of the view
25     f = file(os.path.dirname(__file__) + '/fixtures/exercises.json')
26     exercises = simplejson.loads(f.read())
27     f.close()
28
29     if request.method == 'GET':
30         return render(request, 'wtem/main.html', dict(exercises = exercises))
31     elif request.method == 'POST':
32         form = WTEMForm(request.POST, request.FILES, instance = submission)
33         if form.is_valid():
34             form.save()
35             return render(request, 'wtem/thanks.html')
36         else:
37             raise Exception