start migrating from DRF to ninja
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
from typing import List
|
||||
from django.shortcuts import get_object_or_404
|
||||
from ninja import ModelSchema, Router
|
||||
from .models import AnatomyQuestion as Question, Exam
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
from generic.decorators import check_user_in_group
|
||||
from generic.constants import Group
|
||||
|
||||
from ninja.security import django_auth
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
class QuestionSchema(ModelSchema):
|
||||
class Config:
|
||||
model = Question
|
||||
|
||||
#model_fields = ["question", "history", "feedback", "normal", "laterality"]
|
||||
model_fields = "__all__"
|
||||
|
||||
#model_exclude = ["answers"]
|
||||
|
||||
class ExamSchema(ModelSchema):
|
||||
class Config:
|
||||
model = Exam
|
||||
|
||||
model_fields = ["id", "name", "active", "publish_results"]
|
||||
|
||||
@router.get('/')
|
||||
def list_questions(request):
|
||||
return [
|
||||
{"id": e.id, "normal": e.normal}
|
||||
for e in Question.objects.all()
|
||||
]
|
||||
|
||||
@router.get('/question/{question_id}', response=QuestionSchema)
|
||||
@check_user_in_group(Group.cid_user_manager)
|
||||
def get_rapid_details(request, question_id: int):
|
||||
|
||||
rapid = get_object_or_404(Question, id=question_id)
|
||||
return rapid
|
||||
|
||||
|
||||
@router.get('/user_exams', response=List[ExamSchema], url_name="anatomy_user_exams")
|
||||
def user_exams(request):
|
||||
"""Returns a list of exams that the user has access to"""
|
||||
user = request.user
|
||||
if user.groups.filter(name="anatomy_checker").exists():
|
||||
return Exam.objects.filter(archive=False).order_by('name')
|
||||
|
||||
return Exam.objects.filter(author__id=user.id, archive=False).order_by('name')
|
||||
@@ -15,7 +15,11 @@
|
||||
</div>
|
||||
{% render_table table %}
|
||||
|
||||
<button id="button-select-add-exam" data-exam_json_edit_url="{% url 'generic:generic_exam_json_edit' %}" data-exam_list_url="{% url 'anatomy-exam-list' %}" data-type="anatomy" data-csrf="{{ csrf_token}}">Add selected questions to
|
||||
<button id="button-select-add-exam"
|
||||
data-exam_json_edit_url="{% url 'generic:generic_exam_json_edit' %}"
|
||||
data-exam_list_url="{% url 'api-1:anatomy_user_exams' %}"
|
||||
data-type="anatomy"
|
||||
data-csrf="{{ csrf_token}}">Add selected questions to
|
||||
exam</button>
|
||||
<div id="exam-options"></div>
|
||||
|
||||
|
||||
+14
-14
@@ -775,20 +775,20 @@ class ExamDelete(AuthorOrCheckerRequiredMixin, ExamDeleteBase):
|
||||
|
||||
|
||||
|
||||
class ExamViewSet(RevisionMixin, viewsets.ModelViewSet):
|
||||
# queryset = Exam.objects.all().order_by('name')
|
||||
serializer_class = ExamSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
"""
|
||||
This view should return a list exams available to a user.
|
||||
"""
|
||||
user = self.request.user
|
||||
if user.groups.filter(name="anatomy_checker").exists():
|
||||
return Exam.objects.all()
|
||||
|
||||
return Exam.objects.filter(author__id=user.id)
|
||||
#class ExamViewSet(RevisionMixin, viewsets.ModelViewSet):
|
||||
# # queryset = Exam.objects.all().order_by('name')
|
||||
# serializer_class = ExamSerializer
|
||||
# permission_classes = [permissions.IsAuthenticated]
|
||||
#
|
||||
# def get_queryset(self):
|
||||
# """
|
||||
# This view should return a list exams available to a user.
|
||||
# """
|
||||
# user = self.request.user
|
||||
# if user.groups.filter(name="anatomy_checker").exists():
|
||||
# return Exam.objects.all()
|
||||
#
|
||||
# return Exam.objects.filter(author__id=user.id)
|
||||
|
||||
|
||||
class ExamAuthorUpdate(RevisionMixin, LoginRequiredMixin, AuthorRequiredMixin, UpdateView):
|
||||
|
||||
Reference in New Issue
Block a user