Skip to content

Commit

Permalink
fix(pages): nested pages
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 12, 2023
1 parent 55de866 commit 668db9c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
23 changes: 11 additions & 12 deletions fiesta/apps/pages/models/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Max
from django.db.models.signals import post_save, pre_save
from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django_editorjs_fields import EditorJsJSONField
from mptt.models import TreeForeignKey

from apps.plugins.middleware.plugin import HttpRequest
from apps.utils.models.base import BaseTreeModel


Expand Down Expand Up @@ -85,8 +84,16 @@ class MPTTMeta:
def __str__(self):
return f"{self.title}"

def page_url(self, request: HttpRequest) -> str:
return reverse("pages:single-page", kwargs=dict(slug=self.slug_path))
def get_absolute_url(self) -> str:
return self.section.section_base_url(None) + reverse("pages:single-page", kwargs=dict(slug=self.slug_path))

def save(self, *args, **kwargs):
super().save(*args, **kwargs)

current_path = Page.LEVEL_SLUG_DIVIDER.join(map(attrgetter("slug"), self.get_ancestors(include_self=True)))
if current_path != self.slug_path:
self.slug_path = current_path
super().save(update_fields=["slug_path"])


@receiver(pre_save, sender=Page)
Expand All @@ -95,11 +102,3 @@ def set_order(sender, instance: Page, **kwargs):
instance.order = (
Page.objects.filter(section=instance.section).aggregate(Max("order")).get("order__max") or 0 + 1
)


@receiver(post_save, sender=Page)
def save_slug(sender, instance: Page, **kwargs):
current_path = Page.LEVEL_SLUG_DIVIDER.join(map(attrgetter("slug"), instance.get_ancestors(include_self=True)))
if current_path != instance.slug_path:
instance.slug_path = current_path
instance.save(update_fields=["slug_path"])
4 changes: 2 additions & 2 deletions fiesta/apps/pages/templatetags/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def get_navigation_items_for_pages(context: dict):
request.path.startswith(sub_url),
)
for sub in p.get_descendants()
if (sub_url := sub.page_url(request))
if (sub_url := sub.get_absolute_url(request))
],
request.path.startswith(url),
)
for p in request.in_space_of_section.pages.filter(level=0).exclude(default=True)
if (url := p.page_url(request))
if (url := p.get_absolute_url(request))
]
2 changes: 1 addition & 1 deletion fiesta/apps/sections/models/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Meta:
def __str__(self):
return self.name

def section_base_url(self, request: HttpRequest):
def section_base_url(self, request: HttpRequest | None):
site = get_current_site(request)

return f"//{self.space_slug}.{site.domain}"
Expand Down

0 comments on commit 668db9c

Please sign in to comment.