models.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. from django.db import models
  2. from modelcluster.fields import ParentalKey
  3. from wagtail.admin.panels import (
  4. FieldPanel,
  5. HelpPanel,
  6. MultiFieldPanel,
  7. MultipleChooserPanel,
  8. )
  9. from wagtail.api import APIField
  10. from wagtail.fields import RichTextField, StreamField
  11. from wagtail.models import Orderable, Page
  12. from wagtail.search import index
  13. from bakerydemo.base.blocks import BaseStreamBlock
  14. from bakerydemo.headless import CustomHeadlessMixin
  15. from .blocks import RecipeStreamBlock
  16. class RecipePersonRelationship(Orderable, models.Model):
  17. """
  18. This defines the relationship between the `Person` within the `base`
  19. app and the RecipePage below. This allows people to be added to a RecipePage.
  20. We have created a two way relationship between RecipePage and Person using
  21. the ParentalKey and ForeignKey
  22. """
  23. page = ParentalKey(
  24. "RecipePage",
  25. related_name="recipe_person_relationship",
  26. on_delete=models.CASCADE,
  27. )
  28. person = models.ForeignKey(
  29. "base.Person",
  30. related_name="person_recipe_relationship",
  31. on_delete=models.CASCADE,
  32. )
  33. panels = [FieldPanel("person")]
  34. api_fields = [
  35. APIField("page"),
  36. APIField("person"),
  37. ]
  38. class RecipePage(CustomHeadlessMixin, Page):
  39. """
  40. Recipe pages are more complex than blog pages, demonstrating more advanced StreamField patterns.
  41. """
  42. date_published = models.DateField("Date article published", blank=True, null=True)
  43. subtitle = models.CharField(blank=True, max_length=255)
  44. introduction = models.TextField(blank=True, max_length=500)
  45. backstory = StreamField(
  46. BaseStreamBlock(),
  47. # Demonstrate block_counts to keep the backstory concise.
  48. block_counts={
  49. "heading_block": {"max_num": 1},
  50. "image_block": {"max_num": 1},
  51. "embed_block": {"max_num": 1},
  52. },
  53. blank=True,
  54. use_json_field=True,
  55. help_text="Use only a minimum number of headings and large blocks.",
  56. )
  57. # An example of using rich text for single-line content.
  58. recipe_headline = RichTextField(
  59. blank=True,
  60. max_length=120,
  61. features=["bold", "italic", "link"],
  62. help_text="Keep to a single line",
  63. )
  64. body = StreamField(
  65. RecipeStreamBlock(),
  66. blank=True,
  67. use_json_field=True,
  68. help_text="The recipe's step-by-step instructions and any other relevant information.",
  69. )
  70. content_panels = Page.content_panels + [
  71. FieldPanel("date_published"),
  72. # Using `title` to make a field larger.
  73. FieldPanel("subtitle", classname="title"),
  74. MultiFieldPanel(
  75. [
  76. # Example use case for HelpPanel.
  77. HelpPanel(
  78. "Refer to keywords analysis and correct international ingredients names to craft the best introduction backstory, and headline."
  79. ),
  80. FieldPanel("introduction"),
  81. # StreamField inside a MultiFieldPanel.
  82. FieldPanel("backstory"),
  83. FieldPanel("recipe_headline"),
  84. ],
  85. heading="Preface",
  86. ),
  87. FieldPanel("body"),
  88. MultipleChooserPanel(
  89. "recipe_person_relationship",
  90. chooser_field_name="person",
  91. heading="Authors",
  92. label="Author",
  93. help_text="Select between one and three authors",
  94. panels=None,
  95. min_num=1,
  96. max_num=3,
  97. ),
  98. ]
  99. search_fields = Page.search_fields + [
  100. index.SearchField("backstory"),
  101. index.SearchField("body"),
  102. ]
  103. api_fields = [
  104. APIField("date_published"),
  105. APIField("subtitle"),
  106. APIField("introduction"),
  107. APIField("backstory"),
  108. APIField("recipe_headline"),
  109. APIField("body"),
  110. APIField("recipe_person_relationship"),
  111. ]
  112. def authors(self):
  113. """
  114. Returns the RecipePage's related people. Again note that we are using
  115. the ParentalKey's related_name from the RecipePersonRelationship model
  116. to access these objects. This allows us to access the Person objects
  117. with a loop on the template. If we tried to access the recipe_person_
  118. relationship directly we'd print `recipe.RecipePersonRelationship.None`
  119. """
  120. # Only return authors that are not in draft
  121. return [
  122. n.person
  123. for n in self.recipe_person_relationship.filter(
  124. person__live=True
  125. ).select_related("person")
  126. ]
  127. # Specifies parent to Recipe as being RecipeIndexPages
  128. parent_page_types = ["RecipeIndexPage"]
  129. # Specifies what content types can exist as children of RecipePage.
  130. # Empty list means that no child content types are allowed.
  131. subpage_types = []
  132. class RecipeIndexPage(CustomHeadlessMixin, Page):
  133. """
  134. Index page for recipe.
  135. We need to alter the page model's context to return the child page objects,
  136. the RecipePage objects, so that it works as an index page
  137. """
  138. introduction = models.TextField(help_text="Text to describe the page", blank=True)
  139. content_panels = Page.content_panels + [
  140. FieldPanel("introduction"),
  141. ]
  142. api_fields = [
  143. APIField("introduction"),
  144. ]
  145. # Specifies that only RecipePage objects can live under this index page
  146. subpage_types = ["RecipePage"]
  147. # Defines a method to access the children of the page (e.g. RecipePage
  148. # objects).
  149. def children(self):
  150. return self.get_children().specific().live()
  151. # Overrides the context to list all child items, that are live, by the
  152. # date that they were published
  153. # https://docs.wagtail.org/en/stable/getting_started/tutorial.html#overriding-context
  154. def get_context(self, request):
  155. context = super(RecipeIndexPage, self).get_context(request)
  156. context["recipes"] = (
  157. RecipePage.objects.descendant_of(self).live().order_by("-date_published")
  158. )
  159. return context