fix: lock and archive fixes

This commit is contained in:
NarayanBavisetti 2023-11-16 17:33:06 +05:30
parent 71de47496a
commit f113375a15

View File

@ -87,15 +87,10 @@ class PageViewSet(BaseViewSet):
.annotate(is_favorite=Exists(subquery)) .annotate(is_favorite=Exists(subquery))
.order_by(self.request.GET.get("order_by", "-created_at")) .order_by(self.request.GET.get("order_by", "-created_at"))
.prefetch_related("labels") .prefetch_related("labels")
.order_by("-is_favorite","-created_at") .order_by("-is_favorite", "-created_at")
.distinct() .distinct()
) )
def perform_create(self, serializer):
serializer.save(
project_id=self.kwargs.get("project_id"), owned_by=self.request.user
)
def create(self, request, slug, project_id): def create(self, request, slug, project_id):
serializer = PageSerializer( serializer = PageSerializer(
data=request.data, data=request.data,
@ -148,10 +143,8 @@ class PageViewSet(BaseViewSet):
status=status.HTTP_400_BAD_REQUEST, status=status.HTTP_400_BAD_REQUEST,
) )
def lock(self, request, slug, project_id, pk): def lock(self, request, slug, project_id, page_id):
page = Page.objects.filter( page = Page.objects.get(pk=page_id, workspace__slug=slug, project_id=project_id)
pk=pk, workspace__slug=slug, project_id=project_id
)
# only the owner can lock the page # only the owner can lock the page
if request.user.id != page.owned_by_id: if request.user.id != page.owned_by_id:
@ -163,8 +156,8 @@ class PageViewSet(BaseViewSet):
page.save() page.save()
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
def unlock(self, request, slug, project_id, pk): def unlock(self, request, slug, project_id, page_id):
page = Page.objects.get(pk=pk, workspace__slug=slug, project_id=project_id) page = Page.objects.get(pk=page_id, workspace__slug=slug, project_id=project_id)
# only the owner can unlock the page # only the owner can unlock the page
if request.user.id != page.owned_by_id: if request.user.id != page.owned_by_id:
@ -242,23 +235,25 @@ class PageViewSet(BaseViewSet):
) )
def archive(self, request, slug, project_id, page_id): def archive(self, request, slug, project_id, page_id):
_ = Page.objects.get( page = Page.objects.get(pk=page_id, workspace__slug=slug, project_id=project_id)
project_id=project_id,
owned_by_id=request.user.id, if page.owned_by_id != request.user.id:
workspace__slug=slug, return Response(
pk=page_id, {"error": "Only the owner of the page can archive a page"},
status=status.HTTP_204_NO_CONTENT,
) )
unarchive_archive_page_and_descendants(page_id, datetime.now()) unarchive_archive_page_and_descendants(page_id, datetime.now())
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_400_BAD_REQUEST)
def unarchive(self, request, slug, project_id, page_id): def unarchive(self, request, slug, project_id, page_id):
page = Page.objects.get( page = Page.objects.get(pk=page_id, workspace__slug=slug, project_id=project_id)
project_id=project_id,
owned_by_id=request.user.id, if page.owned_by_id != request.user.id:
workspace__slug=slug, return Response(
pk=page_id, {"error": "Only the owner of the page can unarchive a page"},
status=status.HTTP_400_BAD_REQUEST,
) )
page.parent = None page.parent = None
@ -278,17 +273,11 @@ class PageViewSet(BaseViewSet):
.filter(parent_id__isnull=True) .filter(parent_id__isnull=True)
) )
if not pages:
return Response(
{"error": "No pages found"}, status=status.HTTP_400_BAD_REQUEST
)
return Response( return Response(
PageSerializer(pages, many=True).data, status=status.HTTP_200_OK PageSerializer(pages, many=True).data, status=status.HTTP_200_OK
) )
class PageFavoriteViewSet(BaseViewSet): class PageFavoriteViewSet(BaseViewSet):
permission_classes = [ permission_classes = [
ProjectEntityPermission, ProjectEntityPermission,
@ -324,6 +313,7 @@ class PageFavoriteViewSet(BaseViewSet):
page_favorite.delete() page_favorite.delete()
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
class PageLogEndpoint(BaseAPIView): class PageLogEndpoint(BaseAPIView):
permission_classes = [ permission_classes = [
ProjectEntityPermission, ProjectEntityPermission,
@ -417,4 +407,3 @@ class SubPagesEndpoint(BaseAPIView):
return Response( return Response(
SubPageSerializer(pages, many=True).data, status=status.HTTP_200_OK SubPageSerializer(pages, many=True).data, status=status.HTTP_200_OK
) )