fix: pages endpoint for dates and access (#531)

* fix: pages endpoint for dates and access

* fix: recent pages endpoint

* fix: recent pages endpoint

* fix: date object in recent pages endpoint

* dev: update nomenclature
This commit is contained in:
pablohashescobar 2023-03-25 23:57:16 +05:30 committed by GitHub
parent 69387ffd8c
commit c5baa6183c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -60,7 +60,6 @@ class PageViewSet(BaseViewSet):
.annotate(is_favorite=Exists(subquery))
.order_by(self.request.GET.get("order_by", "-created_at"))
.prefetch_related("labels")
.order_by("name", "-is_favorite")
.prefetch_related(
Prefetch(
@ -88,7 +87,7 @@ class PageViewSet(BaseViewSet):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
print(e)
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
@ -246,17 +245,31 @@ class RecentPagesEndpoint(BaseAPIView):
current_time = timezone.now()
day_before = current_time - timedelta(days=1)
yesterday_pages = (
(
Page.objects.filter(
updated_at__date=day_before.date(),
workspace__slug=slug,
project_id=project_id,
)
.filter(project__project_projectmember__member=request.user)
.annotate(is_favorite=Exists(subquery))
.order_by("-updated_by")
todays_pages = (
Page.objects.filter(
updated_at__date=timezone.now().date(),
workspace__slug=slug,
project_id=project_id,
)
.filter(project__project_projectmember__member=request.user)
.annotate(is_favorite=Exists(subquery))
.filter(Q(owned_by=self.request.user) | Q(access=0))
.select_related("project")
.select_related("workspace")
.select_related("owned_by")
.prefetch_related("labels")
.order_by("-updated_by")
)
yesterdays_pages = (
Page.objects.filter(
updated_at__date=day_before.date(),
workspace__slug=slug,
project_id=project_id,
)
.filter(project__project_projectmember__member=request.user)
.annotate(is_favorite=Exists(subquery))
.filter(Q(owned_by=self.request.user) | Q(access=0))
.select_related("project")
.select_related("workspace")
.select_related("owned_by")
@ -265,38 +278,37 @@ class RecentPagesEndpoint(BaseAPIView):
)
earlier_this_week = (
(
Page.objects.filter(
updated_at__range=(
(timezone.now() - timedelta(days=1)),
(timezone.now() - timedelta(days=7)),
),
workspace__slug=slug,
project_id=project_id,
)
.filter(project__project_projectmember__member=request.user)
.annotate(is_favorite=Exists(subquery))
.order_by("-updated_by")
Page.objects.filter(
updated_at__date__range=(
(timezone.now() - timedelta(days=7)).date(),
(timezone.now() - timedelta(days=1)).date(),
),
workspace__slug=slug,
project_id=project_id,
)
.annotate(is_favorite=Exists(subquery))
.filter(Q(owned_by=self.request.user) | Q(access=0))
.filter(project__project_projectmember__member=request.user)
.annotate(is_favorite=Exists(subquery))
.select_related("project")
.select_related("workspace")
.select_related("owned_by")
.prefetch_related("labels")
.order_by("-updated_by")
)
yesterday_pages_serializer = PageSerializer(yesterday_pages, many=True)
earlier_this_week_serializer = PageBlockSerializer(
earlier_this_week, many=True
)
todays_pages_serializer = PageSerializer(todays_pages, many=True)
yesterday_pages_serializer = PageSerializer(yesterdays_pages, many=True)
earlier_this_week_serializer = PageSerializer(earlier_this_week, many=True)
return Response(
{
"today": todays_pages_serializer.data,
"yesterday": yesterday_pages_serializer.data,
"earlier_this_week": earlier_this_week_serializer.data,
},
status=status.HTTP_200_OK,
)
except Exception as e:
print(e)
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
@ -322,6 +334,8 @@ class FavoritePagesEndpoint(BaseAPIView):
project_id=project_id,
)
.annotate(is_favorite=Exists(subquery))
.filter(Q(owned_by=self.request.user) | Q(access=0))
.filter(project__project_projectmember__member=request.user)
.filter(is_favorite=True)
.select_related("project")
.select_related("workspace")
@ -362,6 +376,8 @@ class MyPagesEndpoint(BaseAPIView):
.select_related("owned_by")
.prefetch_related("labels")
.annotate(is_favorite=Exists(subquery))
.filter(Q(owned_by=self.request.user) | Q(access=0))
.filter(project__project_projectmember__member=request.user)
.order_by("name", "-is_favorite")
)
serializer = PageSerializer(pages, many=True)
@ -392,6 +408,7 @@ class CreatedbyOtherPagesEndpoint(BaseAPIView):
~Q(owned_by=request.user),
workspace__slug=slug,
project_id=project_id,
access=0,
)
.select_related("project")
.select_related("workspace")