Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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