models.py 5.2 KB

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