-
Notifications
You must be signed in to change notification settings - Fork 11
Feature: Add sponser viewset #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from rest_framework import permissions | ||
|
||
from sponsor.models import Sponsor | ||
|
||
|
||
class IsOwnerOrReadOnly(permissions.BasePermission): | ||
# https://stackoverflow.com/questions/72691826/djnago-rest-framework-how-to-allow-only-update-user-own-content-only | ||
def has_object_permission(self, request, view, obj: Sponsor): | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
return obj.manager_id == request.user or obj.creator == request.user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 말씀 주신 커스텀 퍼미션 이군요! 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 값을 바꾸지 않는 methods 일 때, 또는 매니저 또는 작성자가 접근할 때에만 Permission이 부여되는 커스텀 퍼미션이군요. 확인했습니다. |
||
|
||
|
||
class OwnerOnly(permissions.BasePermission): | ||
def has_object_permission(self, request, view, obj: Sponsor): | ||
return obj.manager_id == request.user or obj.creator == request.user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 관리자가 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from rest_framework.routers import DefaultRouter | ||
|
||
from sponsor.viewsets import * | ||
|
||
|
||
def get_router(): | ||
router = DefaultRouter() | ||
router.register("", SponsorViewSet, basename="sponsor") | ||
|
||
return router |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from rest_framework.serializers import ModelSerializer | ||
|
||
from sponsor.models import Sponsor | ||
|
||
|
||
class SponsorSerializer(ModelSerializer): | ||
class Meta: | ||
model = Sponsor | ||
fields = "__all__" | ||
|
||
|
||
class SponsorListSerializer(ModelSerializer): | ||
class Meta: | ||
model = Sponsor | ||
fields = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
"name", | ||
"level", | ||
"desc", | ||
"eng_desc", | ||
"url", | ||
"logo_image", | ||
"id", | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework.permissions import IsAuthenticatedOrReadOnly | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import ModelViewSet | ||
|
||
from sponsor.models import Sponsor | ||
from sponsor.permissions import IsOwnerOrReadOnly, OwnerOnly | ||
from sponsor.serializers import SponsorListSerializer, SponsorSerializer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
|
||
class SponsorViewSet(ModelViewSet): | ||
serializer_class = SponsorSerializer | ||
permission_classes = [IsOwnerOrReadOnly] # 본인 소유만 수정가능 | ||
http_method_names = ["get", "post"] # 지금은 조회/등록만 가능 TODO: 추후 수정기능 추가 | ||
|
||
def get_queryset(self): | ||
return Sponsor.objects.all() | ||
|
||
def list(self, request, *args, **kwargs): | ||
queryset = Sponsor.objects.filter(accepted=True).order_by("name") | ||
serializer = SponsorListSerializer(queryset, many=True) | ||
return Response(serializer.data) | ||
|
||
def create(self, request, *args, **kwargs): | ||
serializer = self.get_serializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response(serializer.data) | ||
|
||
def retrieve(self, request, *args, **kwargs): | ||
pk = kwargs["pk"] | ||
sponsor_data = get_object_or_404(Sponsor, pk=pk) | ||
|
||
# 본인 소유인 경우는 모든 필드 | ||
# 그렇지 않은 경우는 공개 가능한 필드만 응답 | ||
serializer = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와! 이렇게 작성이 가능하군요. 👍 |
||
SponsorSerializer(sponsor_data) | ||
if self.check_owner_permission(request, sponsor_data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 단순한 호기심이 있다면 🤔 |
||
else SponsorListSerializer(sponsor_data) | ||
) | ||
|
||
return Response(serializer.data) | ||
|
||
def check_owner_permission(self, request, sponsor_data: Sponsor): | ||
return OwnerOnly.has_object_permission( | ||
self=OwnerOnly, request=request, view=self, obj=sponsor_data | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍