furher shorts improvements
This commit is contained in:
+128
-7
@@ -37,11 +37,13 @@ from generic.views import (
|
||||
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,
|
||||
@@ -53,11 +55,13 @@ from .forms import (
|
||||
ExamAuthorForm,
|
||||
ExamGroupsForm,
|
||||
ExamMarkerForm,
|
||||
QuestionFindingForm,
|
||||
QuestionForm,
|
||||
QuestionAnswerForm,
|
||||
QuestionSampleAnswerForm,
|
||||
MarkQuestionForm,
|
||||
ExamForm,
|
||||
ImageFormSet,
|
||||
SampleAnswerUpdateFormSet,
|
||||
|
||||
)
|
||||
|
||||
@@ -224,18 +228,41 @@ class QuestionCreate(QuestionCreateBase):
|
||||
# return super().form_valid(form)
|
||||
|
||||
|
||||
class QuestionAnswerUpdate(RevisionMixin, AuthorOrCheckerRequiredMixin, UpdateView):
|
||||
class QuestionSampleAnswerUpdate(AuthorOrCheckerRequiredMixin, UpdateView):
|
||||
model = Question
|
||||
form_class = QuestionAnswerForm
|
||||
template_name = "shorts/shortsquestionanswer_form.html"
|
||||
form_class = QuestionSampleAnswerForm
|
||||
template_name = "shorts/sample_answer_form.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(QuestionAnswerUpdate, self).get_context_data(**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):
|
||||
return super().form_valid(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(
|
||||
@@ -728,4 +755,98 @@ def question_add_exam(request, question_id: int):
|
||||
return HttpResponse(mark_safe(html))
|
||||
|
||||
|
||||
raise PermissionDenied()
|
||||
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,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user