models.py 5.4 KB

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