from django.shortcuts import render, get_object_or_404, redirect from django.views.decorators.csrf import csrf_exempt 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.contrib.contenttypes.models import ContentType from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic import ListView from django.db.models.functions import Lower from django.core.cache import cache from django.urls import reverse_lazy, reverse from django.http import Http404, JsonResponse from django.http import HttpResponseRedirect, HttpResponse from physics.models import CidUserAnswer as PhysicsCidUserAnswer from physics.models import Exam as PhysicsExam from anatomy.models import CidUserAnswer as AnatomyCidUserAnswer from anatomy.models import Exam as AnatomyExam from anatomy.models import AnatomyQuestion from rapids.models import CidUserAnswer as RapidsCidUserAnswer from rapids.models import Exam as RapidsExam from rapids.models import Rapid from longs.models import CidUserAnswer as LongsCidUserAnswer from longs.models import Exam as LongsExam from anatomy.views import AnatomyExamViews from rapids.views import RapidExamViews from longs.views import LongExamViews from generic.forms import QuestionNoteForm from generic.models import QuestionNote @login_required def profile(request): user = request.user return render(request, "profile.html", {"user": user}) def cid_selector(request): return render( request, "cid_selector.html", ) def cid_scores(request, pk): # exam = get_object_or_404(Exam, pk=pk) # TODO:Need some kind of test for cid cid = pk # questions = exam.exam_questions.all() physics_answers = PhysicsCidUserAnswer.objects.filter(cid=cid) anatomy_answers = AnatomyCidUserAnswer.objects.filter(cid=cid) rapids_answers = RapidsCidUserAnswer.objects.filter(cid=cid) longs_answers = LongsCidUserAnswer.objects.filter(cid=cid) if ( not physics_answers and not anatomy_answers and not rapids_answers and not longs_answers ): raise Http404("cid not found") physics_exam_ids = physics_answers.values_list("exam").distinct() physics_exams = PhysicsExam.objects.filter(id__in=physics_exam_ids) anatomy_exam_ids = anatomy_answers.values_list("exam").distinct() anatomy_exams = AnatomyExam.objects.filter(id__in=anatomy_exam_ids) rapids_exam_ids = rapids_answers.values_list("exam").distinct() rapids_exams = RapidsExam.objects.filter(id__in=rapids_exam_ids) longs_exam_ids = longs_answers.values_list("exam").distinct() longs_exams = LongsExam.objects.filter(id__in=longs_exam_ids) return render( request, "cid_scores.html", { "physics_exams": physics_exams, "anatomy_exams": anatomy_exams, "rapids_exams": rapids_exams, "longs_exams": longs_exams, "cid": cid, }, ) def active_exams(request): anatomy_exams = AnatomyExamViews.active_exams(request, False) rapid_exams = RapidExamViews.active_exams(request, False) long_exams = LongExamViews.active_exams(request, False) exams = [] exams.extend(anatomy_exams) exams.extend(rapid_exams) exams.extend(long_exams) active_exams = {"exams": exams} return JsonResponse(active_exams) def active_exams_unbased(request): anatomy_exams = AnatomyExamViews.active_exams(request, False, False) rapid_exams = RapidExamViews.active_exams(request, False, False) long_exams = LongExamViews.active_exams(request, False, False) exams = [] exams.extend(anatomy_exams) exams.extend(rapid_exams) exams.extend(long_exams) active_exams = {"exams": exams} return JsonResponse(active_exams) @csrf_exempt def exam_submit(request): if request.is_ajax and request.method == "POST": exam_type, exam_id = request.POST.get("eid").split("/") if exam_type == "anatomy": return AnatomyExamViews.postExamAnswers(request) elif exam_type == "rapid": return RapidExamViews.postExamAnswers(request) elif exam_type == "long": return LongExamViews.postExamAnswers(request) return JsonResponse({"success": False, "error": "Invalid exam type"}) # postExamAnswers return JsonResponse({"success": False, "error": "Invalid data"}) def feedback_create(request, question_type, pk): if question_type == "anatomy": question = AnatomyQuestion elif question_type == "rapid": question = Rapid elif question_type == "long": question = Long return JsonResponse({"success": False, "error": "Invalid exam type"}) class AddQuestionNote(LoginRequiredMixin, CreateView): model = QuestionNote form_class = QuestionNoteForm