46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
from django.contrib import admin
|
|
from wagtail.admin.panels import CommentPanel
|
|
from wagtail.admin.panels import FieldPanel, ObjectList
|
|
from wagtail.models import Page
|
|
from wagtail.admin.widgets.slug import SlugInput
|
|
|
|
|
|
class StrictHierarchyPage(Page):
|
|
class Meta:
|
|
abstract = True
|
|
|
|
def get_child_ids(self):
|
|
return self.get_children().values_list("id", flat=True)
|
|
|
|
@classmethod
|
|
def get_by_parent(cls, parent):
|
|
return cls.objects.filter(id__in=parent.get_child_ids()).live()
|
|
|
|
def get_content_blocks(self):
|
|
from books.models.contentblock import ContentBlock
|
|
|
|
return ContentBlock.objects.all().descendant_of(self)
|
|
|
|
|
|
def wagtail_parent_filter(parent_cls, child_cls):
|
|
class ParentValueFilter(admin.SimpleListFilter):
|
|
title = "parent"
|
|
parameter_name = "parent"
|
|
|
|
def lookups(self, request, model_admin):
|
|
return list(
|
|
(parent.slug, parent.title) for parent in parent_cls.objects.all()
|
|
)
|
|
|
|
def queryset(self, request, queryset):
|
|
filter_value = self.value()
|
|
parent = parent_cls.objects.filter(slug=filter_value).first()
|
|
if parent:
|
|
return child_cls.objects.filter(id__in=parent.get_child_ids()).live()
|
|
|
|
return ParentValueFilter
|
|
|
|
|
|
def get_default_settings():
|
|
return ObjectList([FieldPanel("slug", widget=SlugInput), CommentPanel()], heading="Settings")
|