stage starting django ninja
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
from ninja import ModelSchema, Router
|
||||
from .models import Rapid
|
||||
|
||||
from .decorators import user_is_author_or_rapid_checker
|
||||
from django.core.exceptions import PermissionDenied
|
||||
|
||||
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"]
|
||||
|
||||
|
||||
@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)
|
||||
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
|
||||
@@ -3,7 +3,14 @@ from .models import Exam, Rapid
|
||||
|
||||
def user_is_author_or_rapid_checker(function):
|
||||
def wrap(request, *args, **kwargs):
|
||||
question = Rapid.objects.get(pk=kwargs['pk'])
|
||||
if "pk" in kwargs:
|
||||
question_id = kwargs["pk"]
|
||||
elif "question_id" in kwargs:
|
||||
question_id = kwargs["question_id"]
|
||||
else:
|
||||
raise PermissionDenied
|
||||
|
||||
question = Rapid.objects.get(pk=question_id)
|
||||
if request.user in question.author.all() or request.user.groups.filter(name='rapid_checker').exists():
|
||||
return function(request, *args, **kwargs)
|
||||
else:
|
||||
|
||||
@@ -789,19 +789,6 @@ class RapidViewSet(
|
||||
pagination_class = StandardResultsSetPagination
|
||||
|
||||
|
||||
class RapidLateralityViewSet(
|
||||
RevisionMixin,
|
||||
mixins.CreateModelMixin,
|
||||
mixins.RetrieveModelMixin,
|
||||
mixins.UpdateModelMixin,
|
||||
mixins.ListModelMixin,
|
||||
viewsets.GenericViewSet,
|
||||
):
|
||||
queryset = Rapid.objects.all() # .order_by('name')
|
||||
serializer_class = RapidLateralitySerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
pagination_class = StandardResultsSetPagination
|
||||
|
||||
|
||||
# class AddSuggestedAnswer(LoginRequiredMixin, CreateView):
|
||||
# model = QuestionNote
|
||||
|
||||
Reference in New Issue
Block a user