54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
from typing import List
|
|
from django.shortcuts import get_object_or_404
|
|
from ninja import ModelSchema, Router
|
|
from .models import Rapid, Exam
|
|
|
|
from .decorators import user_is_author_or_rapid_checker
|
|
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 RapidSchema(ModelSchema):
|
|
class Config:
|
|
model = Rapid
|
|
|
|
#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_rapids(request):
|
|
return [
|
|
{"id": e.id, "normal": e.normal}
|
|
for e in Rapid.objects.all()
|
|
]
|
|
|
|
@router.get('/question/{question_id}', response=RapidSchema)
|
|
@check_user_in_group(Group.cid_user_manager)
|
|
def get_rapid_details(request, question_id: int):
|
|
|
|
rapid = get_object_or_404(Rapid, id=question_id)
|
|
return rapid
|
|
|
|
|
|
@router.get('/user_exams', response=List[ExamSchema], url_name="rapids_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="rapid_checker").exists():
|
|
return Exam.objects.filter(archive=False).order_by('name')
|
|
|
|
return Exam.objects.filter(author__id=user.id, archive=False).order_by('name') |