|
| 1 | +from rest_framework.response import Response |
| 2 | +from rest_framework import status |
| 3 | + |
| 4 | +from plane.api.views.base import BaseViewSet |
| 5 | +from plane.app.permissions import WorkspaceUserPermission |
| 6 | +from plane.db.models import Sticky, Workspace |
| 7 | +from plane.api.serializers import StickySerializer |
| 8 | + |
| 9 | +# OpenAPI imports |
| 10 | +from plane.utils.openapi.decorators import sticky_docs |
| 11 | + |
| 12 | +from drf_spectacular.utils import OpenApiRequest, OpenApiResponse |
| 13 | +from plane.utils.openapi import ( |
| 14 | + STICKY_EXAMPLE, |
| 15 | + create_paginated_response, |
| 16 | + DELETED_RESPONSE, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class StickyViewSet(BaseViewSet): |
| 21 | + serializer_class = StickySerializer |
| 22 | + model = Sticky |
| 23 | + use_read_replica = True |
| 24 | + permission_classes = [WorkspaceUserPermission] |
| 25 | + |
| 26 | + def get_queryset(self): |
| 27 | + return self.filter_queryset( |
| 28 | + super() |
| 29 | + .get_queryset() |
| 30 | + .filter(workspace__slug=self.kwargs.get("slug")) |
| 31 | + .filter(owner_id=self.request.user.id) |
| 32 | + .distinct() |
| 33 | + ) |
| 34 | + |
| 35 | + @sticky_docs( |
| 36 | + operation_id="create_sticky", |
| 37 | + summary="Create a new sticky", |
| 38 | + description="Create a new sticky in the workspace", |
| 39 | + request=OpenApiRequest(request=StickySerializer), |
| 40 | + responses={ |
| 41 | + 201: OpenApiResponse(description="Sticky created", response=StickySerializer, examples=[STICKY_EXAMPLE]) |
| 42 | + }, |
| 43 | + ) |
| 44 | + def create(self, request, slug): |
| 45 | + workspace = Workspace.objects.get(slug=slug) |
| 46 | + serializer = StickySerializer(data=request.data) |
| 47 | + if serializer.is_valid(): |
| 48 | + serializer.save(workspace_id=workspace.id, owner_id=request.user.id) |
| 49 | + return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 50 | + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 51 | + |
| 52 | + @sticky_docs( |
| 53 | + operation_id="list_stickies", |
| 54 | + summary="List stickies", |
| 55 | + description="List all stickies in the workspace", |
| 56 | + responses={ |
| 57 | + 200: create_paginated_response( |
| 58 | + StickySerializer, "Sticky", "List of stickies", example_name="List of stickies" |
| 59 | + ) |
| 60 | + }, |
| 61 | + ) |
| 62 | + def list(self, request, slug): |
| 63 | + query = request.query_params.get("query", False) |
| 64 | + stickies = self.get_queryset().order_by("-created_at") |
| 65 | + if query: |
| 66 | + stickies = stickies.filter(description_stripped__icontains=query) |
| 67 | + |
| 68 | + return self.paginate( |
| 69 | + request=request, |
| 70 | + queryset=(stickies), |
| 71 | + on_results=lambda stickies: StickySerializer(stickies, many=True).data, |
| 72 | + default_per_page=20, |
| 73 | + ) |
| 74 | + |
| 75 | + @sticky_docs( |
| 76 | + operation_id="retrieve_sticky", |
| 77 | + summary="Retrieve a sticky", |
| 78 | + description="Retrieve a sticky by its ID", |
| 79 | + responses={200: OpenApiResponse(description="Sticky", response=StickySerializer, examples=[STICKY_EXAMPLE])}, |
| 80 | + ) |
| 81 | + def retrieve(self, request, slug, pk): |
| 82 | + sticky = self.get_object() |
| 83 | + return Response(StickySerializer(sticky).data) |
| 84 | + |
| 85 | + @sticky_docs( |
| 86 | + operation_id="update_sticky", |
| 87 | + summary="Update a sticky", |
| 88 | + description="Update a sticky by its ID", |
| 89 | + request=OpenApiRequest(request=StickySerializer), |
| 90 | + responses={200: OpenApiResponse(description="Sticky", response=StickySerializer, examples=[STICKY_EXAMPLE])}, |
| 91 | + ) |
| 92 | + def partial_update(self, request, slug, pk): |
| 93 | + sticky = self.get_object() |
| 94 | + serializer = StickySerializer(sticky, data=request.data, partial=True) |
| 95 | + if serializer.is_valid(): |
| 96 | + serializer.save() |
| 97 | + return Response(serializer.data, status=status.HTTP_200_OK) |
| 98 | + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 99 | + |
| 100 | + @sticky_docs( |
| 101 | + operation_id="delete_sticky", |
| 102 | + summary="Delete a sticky", |
| 103 | + description="Delete a sticky by its ID", |
| 104 | + responses={204: DELETED_RESPONSE}, |
| 105 | + ) |
| 106 | + def destroy(self, request, slug, pk): |
| 107 | + sticky = self.get_object() |
| 108 | + sticky.delete() |
| 109 | + return Response(status=status.HTTP_204_NO_CONTENT) |
0 commit comments