847 lines
26 KiB
Python
847 lines
26 KiB
Python
import json
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
|
from django import forms
|
|
|
|
# from django.contrib.auth.models import User
|
|
from django.contrib.auth.decorators import login_required, user_passes_test
|
|
from django.contrib.auth.models import User
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.forms.models import model_to_dict
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.views.generic.detail import DetailView
|
|
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
|
from django.views.generic import ListView
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.urls import reverse_lazy, reverse
|
|
|
|
from django.http import Http404, JsonResponse
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
from django.utils.safestring import mark_safe
|
|
# Create your views here.
|
|
|
|
from reversion.views import RevisionMixin
|
|
|
|
from generic.views import (
|
|
AuthorRequiredMixin,
|
|
ExamCloneMixin,
|
|
ExamCreateBase,
|
|
ExamDeleteBase,
|
|
ExamGroupsUpdateBase,
|
|
ExamUpdateBase,
|
|
ExamViews,
|
|
GenericViewBase,
|
|
UpdateQuestionMixin,
|
|
)
|
|
from generic.mixins import CheckCanEditMixin, SuperuserRequiredMixin
|
|
from atlas.models import Finding, Structure, Condition
|
|
from shorts.decorators import user_is_author_or_shorts_checker
|
|
|
|
from .models import (
|
|
Exam,
|
|
Question,
|
|
QuestionFinding,
|
|
QuestionImage,
|
|
UserAnswer,
|
|
SampleAnswer,
|
|
)
|
|
|
|
from .forms import (
|
|
#AnswerFormSet,
|
|
#AnswerUpdateFormSet,
|
|
ExamAuthorForm,
|
|
ExamGroupsForm,
|
|
ExamMarkerForm,
|
|
QuestionFindingForm,
|
|
QuestionForm,
|
|
QuestionSampleAnswerForm,
|
|
MarkQuestionForm,
|
|
ExamForm,
|
|
ImageFormSet,
|
|
SampleAnswerUpdateFormSet,
|
|
|
|
)
|
|
|
|
from .tables import QuestionTable, QuestionUserAnswerTable
|
|
from .filters import QuestionFilter, QuestionUserAnswerFilter
|
|
|
|
from django_tables2 import SingleTableView, SingleTableMixin
|
|
from django_filters.views import FilterView
|
|
|
|
from dal import autocomplete
|
|
|
|
def normaliseScore(score):
|
|
return score
|
|
#if score == 49:
|
|
# return 4.5
|
|
#elif score in [50, 51]:
|
|
# return 5
|
|
#elif score in [52, 53]:
|
|
# return 5.5
|
|
#elif score in [54]:
|
|
# return 6
|
|
#elif score in [55, 56]:
|
|
# return 6.5
|
|
#elif score in [57, 58]:
|
|
# return 7
|
|
#elif score in [59]:
|
|
# return 7.5
|
|
#elif score in [60]:
|
|
# return 8
|
|
|
|
#return 4
|
|
|
|
|
|
class AuthorOrCheckerRequiredMixin(object):
|
|
def get_object(self, *args, **kwargs):
|
|
obj = super().get_object(*args, **kwargs)
|
|
if self.request.user.groups.filter(name="shorts_checker").exists():
|
|
return obj
|
|
if self.request.user not in obj.author.all():
|
|
raise PermissionDenied() # or Http404
|
|
return obj
|
|
|
|
|
|
@login_required
|
|
def question_split(request, pk):
|
|
question = get_object_or_404(Question, pk=pk)
|
|
|
|
if (
|
|
not request.user.groups.filter(name="shorts_checker").exists()
|
|
and request.user not in question.author.all()
|
|
):
|
|
raise PermissionDenied()
|
|
|
|
images = question.images.all()
|
|
|
|
old_examination = question.examination.all()
|
|
# old_site = rapid.site.all()
|
|
old_author = question.author.all()
|
|
|
|
if not images:
|
|
raise Http404
|
|
|
|
for n in range(len(images) - 1):
|
|
question.pk = None
|
|
|
|
question.save()
|
|
|
|
question.examination.set(old_examination)
|
|
# question.site.set(old_site)
|
|
question.author.set(old_author)
|
|
images[n].question = question
|
|
images[n].save()
|
|
|
|
# images[-1].rapid
|
|
|
|
# if request.user not in rapid.author.all():
|
|
# raise PermissionDenied
|
|
|
|
# logging.debug(rapid.subspecialty.first().name.all())
|
|
return render(request, "shorts/question_detail.html", {"question": question})
|
|
|
|
|
|
|
|
class QuestionCreateBase(RevisionMixin, LoginRequiredMixin, CreateView):
|
|
model = Question
|
|
form_class = QuestionForm
|
|
|
|
# Sending user object to the form, to verify which fields to display/remove (depending on group)
|
|
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["image_formset"] = ImageFormSet(
|
|
self.request.POST, self.request.FILES
|
|
)
|
|
context["image_formset"].full_clean()
|
|
else:
|
|
context["image_formset"] = ImageFormSet()
|
|
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)
|
|
image_formset = context["image_formset"]
|
|
if image_formset.is_valid():
|
|
response = super().form_valid(form)
|
|
image_formset.instance = self.object
|
|
image_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("shorts:rapid_clone", pk=self.object.pk)
|
|
|
|
else:
|
|
return super().form_invalid(form)
|
|
|
|
|
|
# @login_required
|
|
class QuestionCreate(QuestionCreateBase):
|
|
|
|
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
|
|
|
|
## There has to be a better way...
|
|
# try:
|
|
# s = (i.pk for i in self.request.user.rapid_default.site.all())
|
|
# self.initial.update({"site": s})
|
|
# except AttributeError:
|
|
# pass
|
|
# return self.initial
|
|
|
|
# fields = '__all__'
|
|
# #fields = [ 'condition' ]
|
|
# #initial = {'date_of_death': '05/01/2018'}
|
|
# exclude = [ 'created_date', 'published_date' ]
|
|
|
|
# self.object = form.save(commit=False)
|
|
# self.object.save()
|
|
|
|
# form.instance.author.add(self.request.user.id)
|
|
# return super().form_valid(form)
|
|
|
|
|
|
class QuestionSampleAnswerUpdate(AuthorOrCheckerRequiredMixin, UpdateView):
|
|
model = Question
|
|
form_class = QuestionSampleAnswerForm
|
|
template_name = "shorts/sample_answer_form.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(QuestionSampleAnswerUpdate, self).get_context_data(**kwargs)
|
|
if self.request.POST:
|
|
context["answer_formset"] = SampleAnswerUpdateFormSet(
|
|
self.request.POST, self.request.FILES, instance=self.object
|
|
)
|
|
context["answer_formset"].full_clean()
|
|
else:
|
|
context["answer_formset"] = SampleAnswerUpdateFormSet(instance=self.object)
|
|
context["question"] = context["object"]
|
|
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"]
|
|
# logger.debug(formset.is_valid())
|
|
if formset.is_valid():
|
|
response = super().form_valid(form)
|
|
formset.instance = self.object
|
|
formset.save()
|
|
|
|
return response
|
|
else:
|
|
return super().form_invalid(form)
|
|
|
|
|
|
class QuestionUpdate(
|
|
UpdateQuestionMixin
|
|
):
|
|
model = Question
|
|
form_class = QuestionForm
|
|
|
|
# fields = '__all__'
|
|
# #fields = [ 'condition' ]
|
|
# #initial = {'date_of_death': '05/01/2018'}
|
|
# exclude = [ 'created_date', 'published_date' ]
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(QuestionUpdate, self).get_context_data(**kwargs)
|
|
if self.request.POST:
|
|
context["image_formset"] = ImageFormSet(
|
|
self.request.POST, self.request.FILES, instance=self.object
|
|
)
|
|
context["image_formset"].full_clean()
|
|
else:
|
|
context["image_formset"] = ImageFormSet(instance=self.object)
|
|
context["question"] = context["object"]
|
|
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)
|
|
image_formset = context["image_formset"]
|
|
# logger.debug(formset.is_valid())
|
|
if image_formset.is_valid():
|
|
response = super().form_valid(form)
|
|
image_formset.instance = self.object
|
|
image_formset.save()
|
|
|
|
# restore exam orders
|
|
# wanted_exams = form["exams"].data
|
|
# for exam in self.object.exams.all():
|
|
# if exam in exam_orders and self.object in exam_orders[exam]:
|
|
# print(exam_orders[exam])
|
|
# exam.exam_questions.set(exam_orders[exam])
|
|
# exam.save()
|
|
|
|
return response
|
|
else:
|
|
return super().form_invalid(form)
|
|
|
|
|
|
class QuestionClone(QuestionCreateBase):
|
|
"""Clones a existing shorts 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"])
|
|
|
|
return initial_data
|
|
|
|
|
|
class QuestionView(LoginRequiredMixin, SingleTableMixin, FilterView):
|
|
model = Question
|
|
table_class = QuestionTable
|
|
template_name = "question_table_view.html"
|
|
|
|
filterset_class = QuestionFilter
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["app_name"] = "shorts"
|
|
return context
|
|
|
|
|
|
@login_required
|
|
def mark_all(request, exam_pk, sk):
|
|
return mark(request, exam_pk, sk, unmarked_exam_answers_only=False)
|
|
|
|
|
|
@login_required
|
|
def mark_review(request, exam_pk, sk):
|
|
return mark(request, exam_pk, sk, unmarked_exam_answers_only=False, review=True)
|
|
|
|
|
|
# @user_passes_test(user_is_admin, login_url="/accounts/login")
|
|
@login_required
|
|
def mark(request, exam_pk, sk, unmarked_exam_answers_only=True, review=False):
|
|
return HttpResponse("Not implemented")
|
|
exam = get_object_or_404(Exam, pk=exam_pk)
|
|
|
|
if not GenericExamViews.check_user_marker_access(request.user, exam_pk):
|
|
raise PermissionDenied
|
|
|
|
if not exam.exam_mode:
|
|
raise Http404("Packet not in exam mode")
|
|
|
|
mark_url = "shorts:mark"
|
|
if review:
|
|
mark_url = "shorts:mark_review"
|
|
elif not unmarked_exam_answers_only:
|
|
mark_url = "shorts:mark_all"
|
|
|
|
questions = exam.get_questions()
|
|
|
|
n = sk
|
|
|
|
question_details = {
|
|
"total": len(questions),
|
|
"current": n + 1,
|
|
}
|
|
|
|
try:
|
|
question = questions[sk]
|
|
except IndexError:
|
|
raise Http404("Exam question does not exist")
|
|
|
|
if request.method == "POST":
|
|
|
|
form = MarkQuestionForm(request.POST)
|
|
# *******************************
|
|
# TODO: convert to JSON request
|
|
# *******************************
|
|
if form.is_valid():
|
|
# If skip button is pressed skip the question without marking
|
|
if "skip" in request.POST:
|
|
return redirect(mark_url, exam_pk=exam_pk, sk=n + 1)
|
|
|
|
cd = form.cleaned_data
|
|
|
|
marked_answers = json.loads(cd.get("marked_answers"))
|
|
|
|
# This should probably use json (or something else...)
|
|
# ajax.....
|
|
to_run = (
|
|
("correct", Answer.MarkOptions.CORRECT),
|
|
("half-correct", Answer.MarkOptions.HALF_MARK),
|
|
("incorrect", Answer.MarkOptions.INCORRECT),
|
|
)
|
|
|
|
for status_string, status in to_run:
|
|
for ans in marked_answers[status_string]:
|
|
ans = ans.strip()
|
|
if ans == "":
|
|
continue
|
|
|
|
a = Answer.objects.filter(
|
|
answer__iexact=ans, question_id=question.pk
|
|
).first()
|
|
|
|
if a is None:
|
|
a = Answer()
|
|
a.question_id = question.pk
|
|
a.answer = ans
|
|
|
|
a.status = status
|
|
a.save()
|
|
|
|
if "next" in request.POST:
|
|
return redirect(mark_url, exam_pk=exam_pk, sk=n + 1)
|
|
elif "previous" in request.POST:
|
|
return redirect(mark_url, exam_pk=exam_pk, sk=n - 1)
|
|
|
|
# Reset user answers (relies on the functional javascript)
|
|
unmarked_user_answers = set()
|
|
else:
|
|
form = MarkQuestionForm()
|
|
|
|
# answers_dict = {}
|
|
|
|
# for ans in question.answers.all():
|
|
# answers_dict[ans.answer_compare] = ans
|
|
|
|
correct_answers = []
|
|
half_mark_answers = []
|
|
incorrect_answers = []
|
|
review_user_answers = []
|
|
unmarked_answers_bool = False
|
|
unmarked_user_answers = []
|
|
# this could be improved
|
|
if question.normal:
|
|
incorrect_answers = question.get_user_answers(exam_pk=exam_pk)
|
|
if "" in incorrect_answers:
|
|
incorrect_answers.remove("")
|
|
if "normal" in incorrect_answers:
|
|
incorrect_answers.remove("normal")
|
|
else:
|
|
if review:
|
|
unmarked_user_answers = question.get_user_answers(
|
|
exam_pk=exam_pk, include_normal=False
|
|
)
|
|
elif unmarked_exam_answers_only:
|
|
unmarked_user_answers = question.get_unmarked_user_answers(exam_pk=exam_pk)
|
|
else:
|
|
unmarked_user_answers = question.get_unmarked_user_answers()
|
|
|
|
if not unmarked_exam_answers_only:
|
|
correct_answers = question.answers.filter(status=Answer.MarkOptions.CORRECT)
|
|
half_mark_answers = question.answers.filter(
|
|
status=Answer.MarkOptions.HALF_MARK
|
|
)
|
|
incorrect_answers = question.answers.filter(
|
|
status=Answer.MarkOptions.INCORRECT
|
|
)
|
|
|
|
if review:
|
|
for ans in unmarked_user_answers:
|
|
# duplicated from user answer model....
|
|
marked_ans = question.answers.filter(answer_compare__iexact=ans).first()
|
|
mark = "unmarked"
|
|
if marked_ans is not None:
|
|
if marked_ans.status == Answer.MarkOptions.CORRECT:
|
|
mark = "correct"
|
|
elif marked_ans.status == Answer.MarkOptions.HALF_MARK:
|
|
mark = "half-correct"
|
|
elif marked_ans.status == Answer.MarkOptions.INCORRECT:
|
|
mark = "incorrect"
|
|
if mark == "unmarked":
|
|
unmarked_answers_bool = True
|
|
review_user_answers.append((ans, mark))
|
|
|
|
return render(
|
|
request,
|
|
"shorts/mark.html",
|
|
{
|
|
"exam": exam,
|
|
"form": form,
|
|
"review": review,
|
|
"question": question,
|
|
"question_number": n,
|
|
"question_details": question_details,
|
|
"unmarked_answers_bool": unmarked_answers_bool,
|
|
"user_answers": unmarked_user_answers,
|
|
"review_user_answers": review_user_answers,
|
|
"correct_answers": correct_answers,
|
|
"half_mark_answers": half_mark_answers,
|
|
"incorrect_answers": incorrect_answers,
|
|
"unmarked_exam_answers_only": unmarked_exam_answers_only,
|
|
},
|
|
)
|
|
|
|
|
|
class QuestionDelete(AuthorOrCheckerRequiredMixin, DeleteView):
|
|
model = Question
|
|
success_url = reverse_lazy("shorts:question_view")
|
|
|
|
|
|
GenericExamViews = ExamViews(
|
|
Exam, Question, None, UserAnswer, "shorts", "shorts", normalise_score=normaliseScore
|
|
)
|
|
|
|
|
|
class ExamCreate(ExamCreateBase):
|
|
model = Exam
|
|
form_class = ExamForm
|
|
|
|
|
|
class ExamUpdate(ExamUpdateBase, AuthorOrCheckerRequiredMixin):
|
|
model = Exam
|
|
form_class = ExamForm
|
|
|
|
|
|
class ExamDelete(AuthorOrCheckerRequiredMixin, ExamDeleteBase):
|
|
model = Exam
|
|
|
|
|
|
class ExamAuthorUpdate(
|
|
RevisionMixin,
|
|
CheckCanEditMixin,
|
|
LoginRequiredMixin,
|
|
AuthorRequiredMixin,
|
|
UpdateView,
|
|
):
|
|
model = Exam
|
|
form_class = ExamAuthorForm
|
|
template_name = "author_form.html"
|
|
|
|
|
|
class ExamMarkersUpdate(
|
|
RevisionMixin, CheckCanEditMixin, LoginRequiredMixin, AuthorRequiredMixin, UpdateView
|
|
):
|
|
model = Exam
|
|
form_class = ExamMarkerForm
|
|
|
|
template_name = "markers_form.html"
|
|
|
|
class ExamGroupsUpdate(ExamGroupsUpdateBase):
|
|
model = Exam
|
|
form_class = ExamGroupsForm
|
|
|
|
class UserAnswerView(LoginRequiredMixin, DetailView):
|
|
model = UserAnswer
|
|
|
|
|
|
class UserAnswerTableView(LoginRequiredMixin, SingleTableMixin, FilterView):
|
|
model = UserAnswer
|
|
table_class = QuestionUserAnswerTable
|
|
template_name = "shorts/user_answer_question_view.html"
|
|
|
|
filterset_class = QuestionUserAnswerFilter
|
|
|
|
|
|
class UserAnswerDelete(SuperuserRequiredMixin, DeleteView):
|
|
model = UserAnswer
|
|
template_name = "user_answer_delete.html"
|
|
success_url = reverse_lazy("shorts:user_answer_table_view")
|
|
|
|
|
|
GenericViews = GenericViewBase("shorts", Question, UserAnswer, Exam)
|
|
|
|
|
|
@user_is_author_or_shorts_checker
|
|
def question_anonymise_dicom(request, pk):
|
|
question = get_object_or_404(Question, pk=pk)
|
|
|
|
question.anonymise_images()
|
|
|
|
return redirect("shorts:question_detail", pk=pk)
|
|
|
|
|
|
@user_is_author_or_shorts_checker
|
|
def question_save_annotation(request, pk):
|
|
if request.method == "POST":
|
|
question = get_object_or_404(Question, pk=pk)
|
|
|
|
image_annotations = json.loads(request.POST.get("annotation"))
|
|
|
|
question_images = question.images.filter(feedback_image=False)
|
|
|
|
for n, image in enumerate(question_images):
|
|
annotation = image_annotations[n]
|
|
if annotation != "undefined":
|
|
image.image_annotations = annotation
|
|
else:
|
|
image.image_annotations = ""
|
|
image.save()
|
|
|
|
data = {"status": "success"}
|
|
return JsonResponse(data, status=200)
|
|
else:
|
|
data = {"status": "error"}
|
|
return JsonResponse(data, status=400)
|
|
|
|
|
|
class ExamClone(ExamCloneMixin, ExamCreate):
|
|
"""Clone exam view"""
|
|
|
|
|
|
@login_required
|
|
def get_question_by_hash(request):
|
|
if request.method == "POST":
|
|
|
|
hash = request.POST.get("hash")
|
|
|
|
try:
|
|
question = QuestionImage.objects.get(image_md5_hash=hash)
|
|
data = {
|
|
"status": "success",
|
|
"id": question.pk,
|
|
"url": question.question.get_absolute_url(),
|
|
}
|
|
return JsonResponse(data, status=200)
|
|
except QuestionImage.DoesNotExist:
|
|
data = {"status": "success", "id": False}
|
|
return JsonResponse(data, status=200)
|
|
|
|
|
|
# TODO: better permissions
|
|
@login_required
|
|
def question_json_unbased(request, pk):
|
|
"""
|
|
No (file based) caching is enabled for unbased quesitons
|
|
"""
|
|
question = get_object_or_404(Question, pk=pk)
|
|
|
|
question_json = question.get_question_json(based=False)
|
|
return JsonResponse(question_json)
|
|
|
|
|
|
@login_required
|
|
def question_json(request, pk):
|
|
question = get_object_or_404(Question, pk=pk)
|
|
|
|
question_json = question.get_question_json(based=True)
|
|
return JsonResponse(question_json)
|
|
|
|
|
|
@login_required
|
|
def question_viewer(request):
|
|
# question = get_object_or_404(Question, pk=pk)
|
|
|
|
ids = request.GET.get("ids").split(",")
|
|
|
|
return render(
|
|
request,
|
|
"shorts/question_viewer.html",
|
|
{
|
|
"ids": ids,
|
|
},
|
|
)
|
|
|
|
|
|
#class AnswerAutocomplete(autocomplete.Select2QuerySetView):
|
|
# def get_queryset(self):
|
|
# # Don't forget to filter out results depending on the visitor !
|
|
# if not self.request.user.is_authenticated:
|
|
# return Answer.objects.none()
|
|
#
|
|
# qs = Answer.objects.all()
|
|
#
|
|
# if self.q:
|
|
# qs = qs.filter(answer__istartswith=self.q)
|
|
#
|
|
# return qs
|
|
|
|
|
|
#def confirm_answer(request, answer_id: int):
|
|
# if request.htmx:
|
|
# answer = get_object_or_404(Answer, pk=answer_id)
|
|
#
|
|
# # Check if the user has permission to confirm the answer
|
|
# if not answer.can_edit(request.user):
|
|
# return HttpResponse("Invalid permissions", status=403)
|
|
#
|
|
# answer.clean()
|
|
# answer.proposed = False
|
|
# answer.save()
|
|
# return HttpResponse("Answer accepted")
|
|
#
|
|
# raise PermissionDenied()
|
|
#
|
|
#def delete_answer(request, answer_id: int):
|
|
# if request.htmx:
|
|
# answer = get_object_or_404(Answer, pk=answer_id)
|
|
#
|
|
# # Check if the user has permission to confirm the answer
|
|
# if not answer.can_edit(request.user):
|
|
# return HttpResponse("Invalid permissions", status=403)
|
|
#
|
|
# answer.delete()
|
|
# return HttpResponse("Answer deleted")
|
|
# raise PermissionDenied()
|
|
|
|
def question_add_exam(request, question_id: int):
|
|
if request.htmx:
|
|
question = get_object_or_404(Question, pk=question_id)
|
|
|
|
if not question.can_edit(request.user):
|
|
return HttpResponse("Invalid permissions", status=403)
|
|
|
|
|
|
if request.POST:
|
|
exam_id = request.POST.get("exam_id", None)
|
|
|
|
if exam_id is None:
|
|
return HttpResponse("No exam id provided", status=400)
|
|
|
|
exam = get_object_or_404(Exam, pk=exam_id)
|
|
|
|
if request.POST.get("remove", False) == "true":
|
|
question.exams.remove(exam)
|
|
return HttpResponse("Question removed from exam.")
|
|
else:
|
|
question.exams.add(exam)
|
|
|
|
return HttpResponse("Question added to exam.")
|
|
else:
|
|
# Return a list of exams that we can add to the question
|
|
|
|
exams = Exam.objects.filter(author=request.user)
|
|
exams = exams.difference(question.exams.all())
|
|
|
|
if not exams:
|
|
return HttpResponse("No exams available to add")
|
|
|
|
html = "Exams to add to question: <br>"
|
|
for exam in exams:
|
|
html = html + (f"<span id='htmx-exam-list'><form><input name='exam_id' value='{exam.id}' type='hidden'><button hx-post=\"{request.path}\""
|
|
f">{exam.name}: {exam.id}</button></form>"
|
|
)
|
|
html = html + "<br>Exams to remove from question: <br>"
|
|
for exam in question.exams.all():
|
|
html = html + (f"<span id='htmx-exam-list'><form><input name='exam_id' value='{exam.id}' type='hidden'><input name='remove' value='true' type='hidden'><button hx-post=\"{request.path}\""
|
|
f"hx-confirm='Are you sure you want to remove this exam from the question?'"
|
|
f">{exam.name}: {exam.id}</button></form>"
|
|
)
|
|
html = html + "<button _='on click remove #htmx-exam-list'>Cancel</button>"
|
|
|
|
return HttpResponse(mark_safe(html))
|
|
|
|
|
|
raise PermissionDenied()
|
|
|
|
|
|
# At the moment only the series owner / author can delete findings but anyone can create
|
|
# TODO: duplicated from atlas "create_series_findings"
|
|
def create_findings(request):
|
|
# posts = Post.objects.all()
|
|
response_data = {}
|
|
|
|
if request.POST.get("action") == "post":
|
|
question_finding_id = int(request.POST.get("question_finding_id"))
|
|
question_id = request.POST.get("question")
|
|
findings_ids = json.loads(request.POST.get("findings"))
|
|
structure_ids = json.loads(request.POST.get("structures"))
|
|
condition_ids = json.loads(request.POST.get("conditions"))
|
|
description = request.POST.get("description")
|
|
annotation_json = request.POST.get("annotation_json")
|
|
viewport_json = request.POST.get("viewport_json")
|
|
current_image_id_index = request.POST.get("current_image_id_index")
|
|
|
|
question = Question.objects.get(pk=question_id)
|
|
findings = Finding.objects.filter(pk__in=findings_ids)
|
|
structures = Structure.objects.filter(pk__in=structure_ids)
|
|
conditions = Condition.objects.filter(pk__in=condition_ids)
|
|
|
|
if question_finding_id > 0:
|
|
sf = get_object_or_404(QuestionFinding, pk=question_finding_id)
|
|
sf.question = question
|
|
sf.description = description
|
|
sf.annotation_json = annotation_json
|
|
sf.viewport_json = viewport_json
|
|
else:
|
|
sf = QuestionFinding.objects.create(
|
|
question=question,
|
|
description=description,
|
|
annotation_json=annotation_json,
|
|
viewport_json=viewport_json,
|
|
)
|
|
|
|
sf.findings.set(findings)
|
|
sf.structures.set(structures)
|
|
sf.conditions.set(conditions)
|
|
sf.current_image_id_index = current_image_id_index
|
|
sf.save()
|
|
return JsonResponse({"success": True})
|
|
|
|
return JsonResponse({"success": False})
|
|
return render(request, "create_post.html", {"posts": posts})
|
|
|
|
# TODO fix permissions
|
|
class FindingDelete(RevisionMixin, DeleteView): # PermissionRequiredMixin,
|
|
#permission_required = "atlas.delete_seriesfinding"
|
|
model = QuestionFinding
|
|
template_name = "confirm_delete.html"
|
|
# success_url = reverse_lazy("atlas:case_view")
|
|
|
|
def get_object(self):
|
|
question_finding = super().get_object()
|
|
question = question_finding.question
|
|
if question.check_user_can_edit(self.request.user):
|
|
return question_finding
|
|
else:
|
|
raise PermissionDenied
|
|
|
|
def get_success_url(self):
|
|
pk = self.kwargs["pk"]
|
|
question_id = get_object_or_404(QuestionFinding, pk=pk).question.id
|
|
return reverse("shorts:question_detail", kwargs={"pk": question_id})
|
|
|
|
def question_findings(request, question_id, finding_pk=None):
|
|
question = get_object_or_404(Question, pk=question_id)
|
|
|
|
#can_edit = series.check_user_can_edit(request.user)
|
|
can_edit = True
|
|
|
|
editing_finding = -1
|
|
if finding_pk is not None:
|
|
finding = get_object_or_404(QuestionFinding, pk=finding_pk)
|
|
question_finding_form = QuestionFindingForm(None, instance=finding)
|
|
editing_finding = finding.pk
|
|
else:
|
|
question_finding_form = QuestionFindingForm(question_id=question.id)
|
|
|
|
# logging.debug(atlas.subspecialty.first().name.all())
|
|
return render(
|
|
request,
|
|
"shorts/question_findings.html",
|
|
{
|
|
"question": question,
|
|
"question_finding_form": question_finding_form,
|
|
"editing_finding": editing_finding,
|
|
"can_edit": can_edit,
|
|
},
|
|
)
|
|
|