"""产品栏目与产品详情页。详见 documents/设计方案分析与完善版.md §2.5 页面树结构。""" from django.db import models from wagtail.admin.panels import FieldPanel from wagtail.fields import StreamField from wagtail.search import index from apps.core.blocks import COMMON_BLOCKS from apps.core.models import SEOablePage class ProductIndexPage(SEOablePage): """产品栏目列表页,子页面为 ProductPage。""" intro = models.TextField(blank=True, verbose_name="栏目简介") content_panels = SEOablePage.content_panels + [ FieldPanel("intro"), ] subpage_types = ["products.ProductPage"] parent_page_types = ["home.HomePage"] class Meta: verbose_name = "产品栏目页" def get_context(self, request, *args, **kwargs): context = super().get_context(request, *args, **kwargs) context["products"] = ( ProductPage.objects.live().descendant_of(self).order_by("path") ) return context class ProductPage(SEOablePage): """单个产品详情页。""" summary = models.CharField(max_length=250, blank=True, verbose_name="产品简介") cover_image = models.ForeignKey( "wagtailimages.Image", null=True, blank=True, on_delete=models.SET_NULL, related_name="+", verbose_name="产品图片", ) body = StreamField(COMMON_BLOCKS, use_json_field=True, blank=True) search_fields = SEOablePage.search_fields + [ index.SearchField("summary"), index.SearchField("body"), ] content_panels = SEOablePage.content_panels + [ FieldPanel("summary"), FieldPanel("cover_image"), FieldPanel("body"), ] parent_page_types = ["products.ProductIndexPage"] subpage_types = [] class Meta: verbose_name = "产品页"