start migrating from DRF to ninja
This commit is contained in:
+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 %}
|
||||
Reference in New Issue
Block a user