Skip to content

Commit

Permalink
add permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
urmatovnaa committed Nov 27, 2022
1 parent 2599bbe commit e581121
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 119 deletions.
29 changes: 0 additions & 29 deletions question_app/migrations/0001_initial.py

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions question_app/migrations/0003_alter_question_profile.py

This file was deleted.

19 changes: 0 additions & 19 deletions question_app/migrations/0004_alter_answer_profile.py

This file was deleted.

15 changes: 15 additions & 0 deletions question_app/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from rest_framework import permissions


class IsAuthorPermission(permissions.BasePermission):

def has_permission(self, request, view):
if request.method == 'GET':
return True
elif request.user.is_authenticated:
return True

def has_object_permission(self, request, view, obj):
if request.user.is_authenticated:
return True

5 changes: 3 additions & 2 deletions question_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

from question_app.serializers import QuestionSerializer, AnswerSerializer, QuestionDetailSerializer
from question_app.models import Question, Answer
from question_app.permissions import IsAuthorPermission


class QuestionViewSet(viewsets.ModelViewSet):
""" Добавление вопроса"""
serializer_class = QuestionSerializer
permission_classes = (IsAuthenticated,)
permission_classes = (IsAuthorPermission,)
serializer_classes = {
'retrieve': QuestionDetailSerializer,
}
Expand All @@ -27,7 +28,7 @@ def get_queryset(self):
class AnswerViewSet(viewsets.ModelViewSet):
""" Вывод вопрос и ответа"""
serializer_class = AnswerSerializer
permission_classes = (IsAuthenticated,)
permission_classes = (IsAuthorPermission,)
lookup_field = 'question_pk'
queryset = Answer.objects.all()

Expand Down

0 comments on commit e581121

Please sign in to comment.