models.py 5.8 KB

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