|
5 | 5 | # Python imports |
6 | 6 | import json |
7 | 7 |
|
| 8 | + |
8 | 9 | # Third party imports |
9 | 10 | from celery import shared_task |
10 | 11 |
|
| 12 | +# Django imports |
| 13 | +from django.utils import timezone |
| 14 | + |
11 | 15 | # Module imports |
12 | 16 | from plane.db.models import Page, PageVersion |
13 | 17 | from plane.utils.exception_logger import log_exception |
14 | 18 |
|
| 19 | +PAGE_VERSION_TASK_TIMEOUT = 600 |
15 | 20 |
|
16 | 21 | @shared_task |
17 | | -def page_version(page_id, existing_instance, user_id): |
| 22 | +def track_page_version(page_id, existing_instance, user_id): |
18 | 23 | try: |
19 | 24 | # Get the page |
20 | 25 | page = Page.objects.get(id=page_id) |
21 | 26 |
|
22 | 27 | # Get the current instance |
23 | 28 | current_instance = json.loads(existing_instance) if existing_instance is not None else {} |
| 29 | + sub_pages = {} |
| 30 | + |
24 | 31 |
|
25 | 32 | # Create a version if description_html is updated |
26 | 33 | 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() |
38 | 36 |
|
| 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 | + ) |
39 | 71 | # If page versions are greater than 20 delete the oldest one |
40 | 72 | if PageVersion.objects.filter(page_id=page_id).count() > 20: |
41 | 73 | # Delete the old page version |
|
0 commit comments