Skip to content

Commit

Permalink
view mixins and api_view mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
KommuSoft committed Feb 17, 2025
1 parent aa2c5e5 commit 145a433
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions django_user_pinned/api_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django_user_pinned.views import PinnedViewMixin
from rest_framework.decorators import action
from rest_framework.serializers import Serializer
from rest_framework.response import Response


class PinAPIView(PinnedViewMixin):

@action(methods=['get', 'post', 'delete'], detail=True, url_path='pin', url_name='pin', serializer_class=Serializer)
def check_pin(self, request, *args, **kwargs):
instance = self.get_object()
method = request.method.casefold()
if method == 'get':
pin = instance.pinned
elif method == 'post':
instance.pin(self.request.user)
pin = True
else:
instance.unpin(self.request.user)
pin = False
return Response({'pinned': pin})
3 changes: 3 additions & 0 deletions django_user_pinned/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class PinnedViewMixin:
def get_queryset(self, *args, **kwargs):
return super().get_queryset(*args, **kwargs).with_pinned(self.request.user)

0 comments on commit 145a433

Please sign in to comment.