from django.db import models from wagtail.admin.panels import FieldPanel from wagtail.fields import RichTextField from wagtail.models import Page class BlogPage(Page): introduction = models.TextField(help_text="Introduction de l'article", blank=True) image = models.ForeignKey( "wagtailimages.Image", null=True, blank=True, on_delete=models.SET_NULL, related_name="+", help_text="Image de l'article", ) body = RichTextField(blank=True) date_published = models.DateField("Date de publication de l'article") content_panels = Page.content_panels + [ FieldPanel("introduction"), FieldPanel("image"), FieldPanel("body"), FieldPanel("date_published"), ] class BlogIndexPage(Page): introduction = models.TextField(help_text="Description de la page", blank=True) image = models.ForeignKey( "wagtailimages.Image", null=True, blank=True, on_delete=models.SET_NULL, related_name="+", help_text="Image du blog", ) content_panels = Page.content_panels + [ FieldPanel("introduction"), FieldPanel("image"), ] subpage_types = ["BlogPage"] def children(self): return self.get_children().specific().live() def get_context(self, request): context = super().get_context(request) context["posts"] = ( BlogPage.objects.descendant_of(self).live().order_by("-date_published") ) return context