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

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

* fix: yesterday's pages
This commit is contained in:
pablohashescobar 2023-03-25 11:12:54 +05:30 committed by GitHub
parent 82b9275609
commit 28b3c999ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 6 deletions

View File

@ -28,6 +28,7 @@ class PageSerializer(BaseSerializer):
write_only=True, write_only=True,
required=False, required=False,
) )
blocks = PageBlockSerializer(read_only=True, many=True)
class Meta: class Meta:
model = Page model = Page

View File

@ -1,9 +1,10 @@
# Python imports # Python imports
from datetime import timedelta from datetime import timedelta, datetime
from django.utils import timezone
# Django imports # Django imports
from django.db import IntegrityError 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 from django.utils import timezone
# Third party imports # Third party imports
@ -59,6 +60,11 @@ 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")
.prefetch_related(
Prefetch(
"blocks", queryset=PageBlock.objects.select_related("page", "issue")
)
)
.distinct() .distinct()
) )
@ -234,10 +240,13 @@ class RecentPagesEndpoint(BaseAPIView):
project_id=project_id, project_id=project_id,
workspace__slug=slug, workspace__slug=slug,
) )
pages = ( current_time = timezone.now()
day_before = current_time - timedelta(days=1)
yesterday_pages = (
( (
Page.objects.filter( Page.objects.filter(
updated_at__gte=(timezone.now() - timedelta(days=7)), updated_at__date=day_before.date(),
workspace__slug=slug, workspace__slug=slug,
project_id=project_id, project_id=project_id,
) )
@ -251,8 +260,37 @@ class RecentPagesEndpoint(BaseAPIView):
.prefetch_related("labels") .prefetch_related("labels")
) )
serializer = PageSerializer(pages, many=True) earlier_this_week = (
return Response(serializer.data, status=status.HTTP_200_OK) (
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: except Exception as e:
print(e) print(e)
return Response( return Response(