dev: fix pages responses

This commit is contained in:
pablohashescobar 2024-06-14 15:45:48 +05:30
parent c9147e7a57
commit 8123ee8d3a
2 changed files with 46 additions and 14 deletions

View File

@ -20,7 +20,15 @@ class PageSerializer(BaseSerializer):
write_only=True, write_only=True,
required=False, required=False,
) )
project = serializers.UUIDField(read_only=True) # Many to many
label_ids = serializers.ListField(
child=serializers.UUIDField(),
required=False,
)
project_ids = serializers.ListField(
child=serializers.UUIDField(),
required=False,
)
class Meta: class Meta:
model = Page model = Page
@ -42,18 +50,14 @@ class PageSerializer(BaseSerializer):
"updated_by", "updated_by",
"view_props", "view_props",
"logo_props", "logo_props",
"project", "label_ids",
"project_ids",
] ]
read_only_fields = [ read_only_fields = [
"workspace", "workspace",
"owned_by", "owned_by",
] ]
def to_representation(self, instance):
data = super().to_representation(instance)
data["labels"] = [str(label.id) for label in instance.labels.all()]
return data
def create(self, validated_data): def create(self, validated_data):
labels = validated_data.pop("labels", None) labels = validated_data.pop("labels", None)
project_id = self.context["project_id"] project_id = self.context["project_id"]
@ -63,6 +67,7 @@ class PageSerializer(BaseSerializer):
# Get the workspace id from the project # Get the workspace id from the project
project = Project.objects.get(pk=project_id) project = Project.objects.get(pk=project_id)
# Create the page
page = Page.objects.create( page = Page.objects.create(
**validated_data, **validated_data,
description_html=description_html, description_html=description_html,
@ -70,6 +75,7 @@ class PageSerializer(BaseSerializer):
workspace_id=project.workspace_id, workspace_id=project.workspace_id,
) )
# Create the project page
ProjectPage.objects.create( ProjectPage.objects.create(
workspace_id=page.workspace_id, workspace_id=page.workspace_id,
project_id=project_id, project_id=project_id,
@ -78,6 +84,7 @@ class PageSerializer(BaseSerializer):
updated_by_id=page.updated_by_id, updated_by_id=page.updated_by_id,
) )
# Create page labels
if labels is not None: if labels is not None:
PageLabel.objects.bulk_create( PageLabel.objects.bulk_create(
[ [

View File

@ -6,10 +6,13 @@ from django.core.serializers.json import DjangoJSONEncoder
# Django imports # Django imports
from django.db import connection from django.db import connection
from django.db.models import Exists, OuterRef, Q, Subquery from django.db.models import Exists, OuterRef, Q, Value, UUIDField
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views.decorators.gzip import gzip_page from django.views.decorators.gzip import gzip_page
from django.http import StreamingHttpResponse from django.http import StreamingHttpResponse
from django.contrib.postgres.aggregates import ArrayAgg
from django.contrib.postgres.fields import ArrayField
from django.db.models.functions import Coalesce
# Third party imports # Third party imports
from rest_framework import status from rest_framework import status
@ -70,9 +73,6 @@ class PageViewSet(BaseViewSet):
entity_identifier=OuterRef("pk"), entity_identifier=OuterRef("pk"),
workspace__slug=self.kwargs.get("slug"), workspace__slug=self.kwargs.get("slug"),
) )
project_subquery = ProjectPage.objects.filter(
page_id=OuterRef("id"), project_id=self.kwargs.get("project_id")
).values_list("project_id", flat=True)[:1]
return self.filter_queryset( return self.filter_queryset(
super() super()
.get_queryset() .get_queryset()
@ -91,8 +91,33 @@ class PageViewSet(BaseViewSet):
.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")
.annotate(project=Subquery(project_subquery)) .annotate(
.filter(project=self.kwargs.get("project_id")) project=Exists(
ProjectPage.objects.filter(
page_id=OuterRef("id"),
project_id=self.kwargs.get("project_id"),
)
)
)
.annotate(
label_ids=Coalesce(
ArrayAgg(
"page_labels__label_id",
distinct=True,
filter=~Q(page_labels__label_id__isnull=True),
),
Value([], output_field=ArrayField(UUIDField())),
),
project_ids=Coalesce(
ArrayAgg(
"projects__id",
distinct=True,
filter=~Q(projects__id=True),
),
Value([], output_field=ArrayField(UUIDField())),
),
)
.filter(project=True)
.distinct() .distinct()
) )
@ -112,7 +137,7 @@ class PageViewSet(BaseViewSet):
serializer.save() serializer.save()
# capture the page transaction # capture the page transaction
page_transaction.delay(request.data, None, serializer.data["id"]) page_transaction.delay(request.data, None, serializer.data["id"])
page = Page.objects.get(pk=serializer.data["id"]) page = self.get_queryset().get(pk=serializer.data["id"])
serializer = PageDetailSerializer(page) serializer = PageDetailSerializer(page)
return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)