Skip to content

Commit a58642e

Browse files
[WIKI-852] chore: update page version save logic (#8440)
* chore: updated the logic for page version task * chore: updated the html variable * chore: handled the exception * chore: changed the function name * chore: added a custom variable
1 parent a9d688f commit a58642e

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

apps/api/plane/app/views/page/base.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
# Local imports
5151
from ..base import BaseAPIView, BaseViewSet
5252
from plane.bgtasks.page_transaction_task import page_transaction
53-
from plane.bgtasks.page_version_task import page_version
53+
from plane.bgtasks.page_version_task import track_page_version
5454
from plane.bgtasks.recent_visited_task import recent_visited_task
5555
from plane.bgtasks.copy_s3_object import copy_s3_objects_of_description_and_assets
5656
from plane.app.permissions import ProjectPagePermission
@@ -545,26 +545,28 @@ def partial_update(self, request, slug, project_id, page_id):
545545
status=status.HTTP_400_BAD_REQUEST,
546546
)
547547

548+
# Store the old description_html before saving (needed for both tasks)
549+
old_description_html = page.description_html
550+
548551
# Serialize the existing instance
549-
existing_instance = json.dumps({"description_html": page.description_html}, cls=DjangoJSONEncoder)
552+
existing_instance = json.dumps({"description_html": old_description_html}, cls=DjangoJSONEncoder)
550553

551554
# Use serializer for validation and update
552555
serializer = PageBinaryUpdateSerializer(page, data=request.data, partial=True)
553556
if serializer.is_valid():
557+
serializer.save()
558+
554559
# Capture the page transaction
555560
if request.data.get("description_html"):
556561
page_transaction.delay(
557562
new_description_html=request.data.get("description_html", "<p></p>"),
558-
old_description_html=page.description_html,
563+
old_description_html=old_description_html,
559564
page_id=page_id,
560565
)
561566

562-
# Update the page using serializer
563-
updated_page = serializer.save()
564-
565567
# Run background tasks
566-
page_version.delay(
567-
page_id=updated_page.id,
568+
track_page_version.delay(
569+
page_id=page_id,
568570
existing_instance=existing_instance,
569571
user_id=request.user.id,
570572
)

apps/api/plane/bgtasks/page_version_task.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,69 @@
55
# Python imports
66
import json
77

8+
89
# Third party imports
910
from celery import shared_task
1011

12+
# Django imports
13+
from django.utils import timezone
14+
1115
# Module imports
1216
from plane.db.models import Page, PageVersion
1317
from plane.utils.exception_logger import log_exception
1418

19+
PAGE_VERSION_TASK_TIMEOUT = 600
1520

1621
@shared_task
17-
def page_version(page_id, existing_instance, user_id):
22+
def track_page_version(page_id, existing_instance, user_id):
1823
try:
1924
# Get the page
2025
page = Page.objects.get(id=page_id)
2126

2227
# Get the current instance
2328
current_instance = json.loads(existing_instance) if existing_instance is not None else {}
29+
sub_pages = {}
30+
2431

2532
# Create a version if description_html is updated
2633
if current_instance.get("description_html") != page.description_html:
27-
# Create a new page version
28-
PageVersion.objects.create(
29-
page_id=page_id,
30-
workspace_id=page.workspace_id,
31-
description_html=page.description_html,
32-
description_binary=page.description_binary,
33-
owned_by_id=user_id,
34-
last_saved_at=page.updated_at,
35-
description_json=page.description_json,
36-
description_stripped=page.description_stripped,
37-
)
34+
# Fetch the latest page version
35+
page_version = PageVersion.objects.filter(page_id=page_id).order_by("-last_saved_at").first()
3836

37+
# Get the latest page version if it exists and is owned by the user
38+
if (
39+
page_version
40+
and str(page_version.owned_by_id) == str(user_id)
41+
and (timezone.now() - page_version.last_saved_at).total_seconds() <= PAGE_VERSION_TASK_TIMEOUT
42+
):
43+
page_version.description_html = page.description_html
44+
page_version.description_binary = page.description_binary
45+
page_version.description_json = page.description
46+
page_version.description_stripped = page.description_stripped
47+
page_version.sub_pages_data = sub_pages
48+
page_version.save(
49+
update_fields=[
50+
"description_html",
51+
"description_binary",
52+
"description_json",
53+
"description_stripped",
54+
"sub_pages_data",
55+
"updated_at"
56+
]
57+
)
58+
else:
59+
# Create a new page version
60+
PageVersion.objects.create(
61+
page_id=page_id,
62+
workspace_id=page.workspace_id,
63+
description_json=page.description,
64+
description_html=page.description_html,
65+
description_binary=page.description_binary,
66+
description_stripped=page.description_stripped,
67+
owned_by_id=user_id,
68+
last_saved_at=timezone.now(),
69+
sub_pages_data=sub_pages,
70+
)
3971
# If page versions are greater than 20 delete the oldest one
4072
if PageVersion.objects.filter(page_id=page_id).count() > 20:
4173
# Delete the old page version

0 commit comments

Comments
 (0)