Skip to content

Commit

Permalink
dev: update recent pages endpoint and return blocks on page listing (#…
Browse files Browse the repository at this point in the history
…517)

* dev: update recent pages endpoint and return blocks on page listing

* fix: yesterday's pages
  • Loading branch information
pablohashescobar authored Mar 25, 2023
1 parent 82b9275 commit 28b3c99
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions apiserver/plane/api/serializers/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PageSerializer(BaseSerializer):
write_only=True,
required=False,
)
blocks = PageBlockSerializer(read_only=True, many=True)

class Meta:
model = Page
Expand Down
50 changes: 44 additions & 6 deletions apiserver/plane/api/views/page.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Python imports
from datetime import timedelta
from datetime import timedelta, datetime
from django.utils import timezone

# Django imports
from django.db import IntegrityError
from django.db.models import Exists, OuterRef, Q
from django.db.models import Exists, OuterRef, Q, Prefetch
from django.utils import timezone

# Third party imports
Expand Down Expand Up @@ -59,6 +60,11 @@ def get_queryset(self):
.annotate(is_favorite=Exists(subquery))
.order_by(self.request.GET.get("order_by", "-created_at"))
.prefetch_related("labels")
.prefetch_related(
Prefetch(
"blocks", queryset=PageBlock.objects.select_related("page", "issue")
)
)
.distinct()
)

Expand Down Expand Up @@ -234,10 +240,13 @@ def get(self, request, slug, project_id):
project_id=project_id,
workspace__slug=slug,
)
pages = (
current_time = timezone.now()
day_before = current_time - timedelta(days=1)

yesterday_pages = (
(
Page.objects.filter(
updated_at__gte=(timezone.now() - timedelta(days=7)),
updated_at__date=day_before.date(),
workspace__slug=slug,
project_id=project_id,
)
Expand All @@ -251,8 +260,37 @@ def get(self, request, slug, project_id):
.prefetch_related("labels")
)

serializer = PageSerializer(pages, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
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")
)
.select_related("project")
.select_related("workspace")
.select_related("owned_by")
.prefetch_related("labels")
)

yesterday_pages_serializer = PageSerializer(yesterday_pages, many=True)
earlier_this_week_serializer = PageBlockSerializer(
earlier_this_week, many=True
)
return Response(
{
"yesterday": yesterday_pages_serializer.data,
"earlier_this_week": earlier_this_week_serializer.data,
},
status=status.HTTP_200_OK,
)
except Exception as e:
print(e)
return Response(
Expand Down

0 comments on commit 28b3c99

Please sign in to comment.