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):
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# constants.py
|
||||
|
||||
from enum import Enum
|
||||
class Group(Enum):
|
||||
cid_user_manager = "cid_user_manager"
|
||||
marker = "marker"
|
||||
anatomy_marker = "anatomy_marker"
|
||||
@@ -1,5 +1,9 @@
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
from django.http import Http404
|
||||
from .constants import Group
|
||||
|
||||
from functools import wraps
|
||||
|
||||
def user_is_cid_user_manager(function):
|
||||
def wrap(request, *args, **kwargs):
|
||||
@@ -14,3 +18,21 @@ def user_is_cid_user_manager(function):
|
||||
wrap.__doc__ = function.__doc__
|
||||
wrap.__name__ = function.__name__
|
||||
return wrap
|
||||
|
||||
|
||||
def check_user_in_group(*groups: Group):
|
||||
print("DEC")
|
||||
print(groups)
|
||||
|
||||
def decorator(function):
|
||||
@wraps(function)
|
||||
def wrapper(request, *args, **kwargs):
|
||||
print(request.user)
|
||||
if request.user.is_superuser or request.user.groups.filter(
|
||||
name__in=[group.name for group in groups]
|
||||
).exists():
|
||||
return function(request, *args, **kwargs)
|
||||
raise PermissionDenied
|
||||
|
||||
return wrapper
|
||||
return decorator
|
||||
+2
-2
@@ -195,10 +195,10 @@ def generic_exam_json_edit(request):
|
||||
if request.method == "POST":
|
||||
question_type = request.POST.get("type")
|
||||
|
||||
if question_type == "rapid":
|
||||
if question_type == "rapids":
|
||||
Exam = RapidExam
|
||||
Question = RapidQuestion
|
||||
elif question_type == "long":
|
||||
elif question_type == "longs":
|
||||
Exam = LongExam
|
||||
Question = LongQuestion
|
||||
elif question_type == "anatomy":
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
from typing import List
|
||||
from django.shortcuts import get_object_or_404
|
||||
from ninja import ModelSchema, Router
|
||||
from .models import Exam, Long as Question
|
||||
|
||||
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="longs_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="long_checker").exists():
|
||||
return Exam.objects.filter(archive=False).order_by('name')
|
||||
|
||||
return Exam.objects.filter(author__id=user.id, archive=False).order_by('name')
|
||||
+3
-3
@@ -1135,9 +1135,9 @@ class ExamAuthorUpdate(RevisionMixin, LoginRequiredMixin, AuthorRequiredMixin, U
|
||||
form_class = ExamAuthorForm
|
||||
|
||||
|
||||
class ExamViewSet(RevisionMixin, viewsets.ModelViewSet):
|
||||
queryset = Exam.objects.all().order_by("name")
|
||||
serializer_class = ExamSerializer
|
||||
#class ExamViewSet(RevisionMixin, viewsets.ModelViewSet):
|
||||
# queryset = Exam.objects.all().order_by("name")
|
||||
# serializer_class = ExamSerializer
|
||||
|
||||
|
||||
def question_json_unbased(request, pk):
|
||||
|
||||
+6
-2
@@ -1,12 +1,16 @@
|
||||
from ninja import NinjaAPI
|
||||
|
||||
from rapids.api import router as rapids_router
|
||||
from longs.api import router as longs_router
|
||||
from anatomy.api import router as anatomy_router
|
||||
|
||||
api = NinjaAPI(csrf=True)
|
||||
api = NinjaAPI(csrf=True, version="1")
|
||||
|
||||
|
||||
|
||||
api.add_router("/rapid/", rapids_router)
|
||||
api.add_router("rapid/", rapids_router)
|
||||
api.add_router("anatomy/", anatomy_router)
|
||||
api.add_router("longs/", longs_router)
|
||||
|
||||
@api.get("/hello")
|
||||
def hello(request):
|
||||
|
||||
+3
-3
@@ -37,9 +37,9 @@ from .api import api
|
||||
|
||||
|
||||
router_drf = routers.DefaultRouter()
|
||||
router_drf.register(r"rapids/exams", rapid_views.ExamViewSet, basename="rapid-exam")
|
||||
router_drf.register(r"anatomy/exams", anatomy_views.ExamViewSet, basename="anatomy-exam")
|
||||
router_drf.register(r"longs/exams", long_views.ExamViewSet, basename="long-exam")
|
||||
#router_drf.register(r"rapids/exams", rapid_views.ExamViewSet, basename="rapid-exam")
|
||||
#router_drf.register(r"anatomy/exams", anatomy_views.ExamViewSet, basename="anatomy-exam")
|
||||
#router_drf.register(r"longs/exams", long_views.ExamViewSet, basename="long-exam")
|
||||
router_drf.register(
|
||||
r"rapids/answers",
|
||||
rapid_views.QuestionAnswerViewSet,
|
||||
|
||||
+22
-5
@@ -1,10 +1,14 @@
|
||||
from typing import List
|
||||
from django.shortcuts import get_object_or_404
|
||||
from ninja import ModelSchema, Router
|
||||
from .models import Rapid
|
||||
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()
|
||||
@@ -19,6 +23,11 @@ class RapidSchema(ModelSchema):
|
||||
|
||||
#model_exclude = ["answers"]
|
||||
|
||||
class ExamSchema(ModelSchema):
|
||||
class Config:
|
||||
model = Exam
|
||||
|
||||
model_fields = ["id", "name", "active", "publish_results"]
|
||||
|
||||
@router.get('/')
|
||||
def list_rapids(request):
|
||||
@@ -28,10 +37,18 @@ def list_rapids(request):
|
||||
]
|
||||
|
||||
@router.get('/question/{question_id}', response=RapidSchema)
|
||||
@check_user_in_group(Group.cid_user_manager)
|
||||
def get_rapid_details(request, question_id: int):
|
||||
print(request.user)
|
||||
#if not request.user.is_superuser:
|
||||
# raise PermissionDenied
|
||||
|
||||
rapid = get_object_or_404(Rapid, id=question_id)
|
||||
return rapid
|
||||
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')
|
||||
@@ -34,7 +34,7 @@
|
||||
{% endfor %}
|
||||
|
||||
<button id="add-to-exam" data-exam_json_edit_url="{% url 'generic:generic_exam_json_edit' %}"
|
||||
data-exam_list_url="{% url 'rapid-exam-list' %}" data-type="rapid" data-csrf="{{ csrf_token}}"
|
||||
data-exam_list_url="{% url 'api-1:rapid_user_exams' %}" data-type="rapids" data-csrf="{{ csrf_token}}"
|
||||
data-qid="{{question.pk}}">Add to exam</button>
|
||||
<button id="cancel-add-to-exam" style="display:none">Cancel</button>
|
||||
<span id="exam-options"></span>
|
||||
|
||||
@@ -6,23 +6,26 @@
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div id="view-filter-options">
|
||||
<h3>Filter Rapids</h3>
|
||||
<form action="" method="get">
|
||||
{{ filter.form }}
|
||||
<input class="btn btn-primary btn-sm mt-1 mb-1" type="submit" />
|
||||
</form>
|
||||
</div>
|
||||
{% render_table table %}
|
||||
<div id="view-filter-options">
|
||||
<h3>Filter Rapids</h3>
|
||||
<form action="" method="get">
|
||||
{{ filter.form }}
|
||||
<input class="btn btn-primary btn-sm mt-1 mb-1" type="submit" />
|
||||
</form>
|
||||
</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 'rapid-exam-list' %}" data-type="rapid" data-csrf="{{ csrf_token}}">Add selected questions to
|
||||
exam</button>
|
||||
<div id="exam-options"></div>
|
||||
<button id="button-select-add-exam"
|
||||
data-exam_json_edit_url="{% url 'generic:generic_exam_json_edit' %}"
|
||||
data-exam_list_url="{% url 'api-1:rapid_user_exams' %}"
|
||||
data-type="rapid" data-csrf="{{ csrf_token}}">Add selected questions to
|
||||
exam</button>
|
||||
<div id="exam-options"></div>
|
||||
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function () {
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function () {
|
||||
|
||||
|
||||
});
|
||||
</script>
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -20,8 +20,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 'rapid-exam-list' %}" data-type="rapid" data-csrf="{{ csrf_token}}">Add selected questions to
|
||||
exam</button>
|
||||
<div id="exam-options"></div>
|
||||
<button id="button-select-add-exam"
|
||||
data-exam_json_edit_url="{% url 'generic:generic_exam_json_edit' %}"
|
||||
data-exam_list_url="{% url 'api-1:'|add:app_name|add:'_user_exams' %}"
|
||||
data-type={{app_name}} data-csrf="{{ csrf_token}}">Add selected questions to
|
||||
exam</button>
|
||||
<div id="exam-options"></div>
|
||||
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user