.
This commit is contained in:
+109
-3
@@ -7,6 +7,7 @@ from django.shortcuts import render, get_object_or_404, redirect
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django import forms
|
||||
from django.utils import timezone
|
||||
from django.forms.models import model_to_dict
|
||||
|
||||
# from django.contrib.auth.models import User
|
||||
from django.contrib.auth.decorators import login_required, user_passes_test
|
||||
@@ -50,11 +51,16 @@ from django.core.exceptions import PermissionDenied
|
||||
from .tables import QuestionTable, UserAnswerTable
|
||||
from .filters import QuestionFilter, UserAnswerFilter
|
||||
|
||||
from .forms import (
|
||||
QuestionForm,
|
||||
ExamForm,
|
||||
)
|
||||
|
||||
|
||||
class AuthorOrCheckerRequiredMixin(object):
|
||||
def get_object(self, *args, **kwargs):
|
||||
obj = super().get_object(*args, **kwargs)
|
||||
if self.request.user.groups.filter(name="sbas_checker").exists():
|
||||
if self.request.user.groups.filter(name="sba_checker").exists():
|
||||
return obj
|
||||
if self.request.user not in obj.author.all():
|
||||
raise PermissionDenied() # or Http404
|
||||
@@ -299,9 +305,109 @@ class QuestionView(LoginRequiredMixin, SingleTableMixin, FilterView):
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["app_name"] = "anatomy"
|
||||
context["app_name"] = "sbas"
|
||||
return context
|
||||
|
||||
class QuestionCreateBase(RevisionMixin, LoginRequiredMixin, CreateView):
|
||||
model = Question
|
||||
form_class = QuestionForm
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(QuestionCreateBase, self).get_form_kwargs()
|
||||
kwargs.update({"user": self.request.user})
|
||||
return kwargs
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(QuestionCreateBase, self).get_context_data(**kwargs)
|
||||
#if self.request.POST:
|
||||
# context["answer_formset"] = AnswerFormSet(
|
||||
# self.request.POST, self.request.FILES
|
||||
# )
|
||||
#else:
|
||||
# context["answer_formset"] = AnswerFormSet()
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
|
||||
self.object = form.save(commit=False)
|
||||
self.object.save()
|
||||
|
||||
form.instance.author.add(self.request.user.id)
|
||||
|
||||
context = self.get_context_data(form=form)
|
||||
formset = context["answer_formset"]
|
||||
|
||||
if formset.is_valid():
|
||||
response = super().form_valid(form)
|
||||
formset.instance = self.object
|
||||
formset.save()
|
||||
# If the normal submit button is pressed we save as normal
|
||||
if "submit" in self.request.POST:
|
||||
return response
|
||||
# else we redirect to the clone url
|
||||
else:
|
||||
return redirect("sbas:question_clone", pk=self.object.pk)
|
||||
|
||||
else:
|
||||
return super().form_invalid(form)
|
||||
|
||||
class QuestionCreate(QuestionCreateBase):
|
||||
|
||||
# initial = {'laterality': AnatomyQuestion.NONE}
|
||||
|
||||
def get_initial(self):
|
||||
if "pk" in self.kwargs:
|
||||
initial = super().get_initial()
|
||||
exam = get_object_or_404(Exam, pk=self.kwargs["pk"])
|
||||
|
||||
initial["exams"] = [exam.id]
|
||||
|
||||
return initial
|
||||
|
||||
class QuestionUpdate(RevisionMixin, AuthorOrCheckerRequiredMixin, UpdateView):
|
||||
model = Question
|
||||
form_class = QuestionForm
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(QuestionUpdate, self).get_form_kwargs()
|
||||
kwargs.update({"user": self.request.user})
|
||||
return kwargs
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(QuestionUpdate, self).get_context_data(**kwargs)
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
|
||||
# save exam orders (there must be a better way to do this)
|
||||
#exam_orders = {}
|
||||
#for exam in self.object.exams.all():
|
||||
# exam_orders[exam] = list(exam.exam_questions.all())
|
||||
# print(exam_orders[exam])
|
||||
|
||||
self.object = form.save(commit=False)
|
||||
self.object.save()
|
||||
|
||||
form.instance.author.add(self.request.user.id)
|
||||
|
||||
context = self.get_context_data(form=form)
|
||||
response = super().form_valid(form)
|
||||
return response
|
||||
|
||||
class QuestionClone(QuestionCreateBase):
|
||||
"""Clones a existing question"""
|
||||
|
||||
def get_initial(self):
|
||||
# print(self.request)
|
||||
old_object = get_object_or_404(Question, pk=self.kwargs["pk"])
|
||||
initial_data = model_to_dict(old_object, exclude=["id"])
|
||||
|
||||
# We don't want to clone the exam details
|
||||
# exams = old_object.exams.all().values_list("id", flat=True)
|
||||
|
||||
# initial_data["exams"] = list(exams)
|
||||
|
||||
return initial_data
|
||||
|
||||
class UserAnswerView(LoginRequiredMixin, DetailView):
|
||||
model = CidUserAnswer
|
||||
@@ -364,7 +470,7 @@ class ExamDelete(AuthorOrCheckerRequiredMixin, ExamDeleteBase):
|
||||
# This view should return a list exams available to a user.
|
||||
# """
|
||||
# user = self.request.user
|
||||
# if user.groups.filter(name="sbas_checker").exists():
|
||||
# if user.groups.filter(name="sba_checker").exists():
|
||||
# return Exam.objects.all()
|
||||
#
|
||||
# return Exam.objects.filter(author__id=user.id)
|
||||
|
||||
Reference in New Issue
Block a user