from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect

from tutor.models import *


def study(request):
    """Choose the next question for the user to study."""
    return render(request, 'tutor/study.html')


def list(request):
    """List the questions in the database"""
    
    questions = Question.objects.all()
    context = {"questions": questions}
    
    return render(request, 'tutor/list.html', context)

@login_required
def question_form(request, pk=0):
    
    if pk == 0:
        form = QuestionForm()
        history = []
        tests = []
    else:
        question = Question.objects.get(pk=pk)
        form = QuestionForm(instance=question)
        history = ArchiveQuestion.objects.all().filter(parent_id=pk)
        tests = Test.objects.all().filter(question=question)


    test_form = TestForm()

    context = { "question": form,
                "pk": pk,
                "history": history,
                "test_form": test_form,
                "tests": tests
              }

    return render(request, 'tutor/question_form.html', context)

@login_required
def save_question(request):

    pk = int(request.POST["pk"])
    if pk > 0:
        q = Question.objects.get(pk=pk)
        form = QuestionForm(request.POST, instance=q)
        aq = ArchiveQuestion()
        aq.archive(form.instance)
        aq.modifier = request.user
        aq.save()
        form.instance.version += 1
    else:
        form = QuestionForm(request.POST)
        form.instance.version = len(ArchiveQuestion.objects.all()) + 1
        form.instance.creator = request.user

    form.instance.modifier = request.user

    question = form.save()
    return HttpResponseRedirect("/tutor/list")



@@ -1356,126 +1356,8 @@
 =q)%0A
-        aq = ArchiveQuestion()%0A        aq.archive(form.instance)%0A        aq.modifier = request.user%0A        aq.save()%0A
     
@@ -1474,45 +1474,8 @@
 on =
- len(ArchiveQuestion.objects.all()) +
  1%0A 
@@ -1557,25 +1557,24 @@
 equest.user%0A
-%0A
     question
@@ -1588,16 +1588,43 @@
 .save()%0A
+    archive(question)%0A    %0A
     retu
@@ -1660,12 +1660,137 @@
 or/list%22)%0A%0A%0A
+def archive(question):%0A    aq = ArchiveQuestion()%0A    aq.archive(question)%0A    aq.modifier = question.modifier%0A    aq.save()%0A

create an archive every time a question is saved