models.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. from django import forms
  2. from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
  3. from django.db import models
  4. from modelcluster.fields import ParentalManyToManyField
  5. from wagtail.admin.panels import FieldPanel, MultiFieldPanel
  6. from wagtail.fields import StreamField
  7. from wagtail.models import DraftStateMixin, Page, RevisionMixin
  8. from wagtail.search import index
  9. from bakerydemo.base.blocks import BaseStreamBlock
  10. class Country(models.Model):
  11. """
  12. A Django model to store set of countries of origin.
  13. It is made accessible in the Wagtail admin interface through the CountrySnippetViewSet
  14. class in wagtail_hooks.py. This allows us to customize the admin interface for this snippet.
  15. In the BreadPage model you'll see we use a ForeignKey to create the relationship between
  16. Country and BreadPage. This allows a single relationship (e.g only one
  17. Country can be added) that is one-way (e.g. Country will have no way to
  18. access related BreadPage objects).
  19. """
  20. title = models.CharField(max_length=100)
  21. def __str__(self):
  22. return self.title
  23. class Meta:
  24. verbose_name_plural = "Countries of Origin"
  25. class BreadIngredient(DraftStateMixin, RevisionMixin, models.Model):
  26. """
  27. A Django model to store a single ingredient.
  28. It is made accessible in the Wagtail admin interface through the BreadIngredientSnippetViewSet
  29. class in wagtail_hooks.py. This allows us to customize the admin interface for this snippet.
  30. We use a piece of functionality available to Wagtail called the ParentalManyToManyField on the BreadPage
  31. model to display this. The Wagtail Docs give a slightly more detailed example
  32. https://docs.wagtail.org/en/stable/getting_started/tutorial.html#categories
  33. """
  34. name = models.CharField(max_length=255)
  35. panels = [
  36. FieldPanel("name"),
  37. ]
  38. def __str__(self):
  39. return self.name
  40. class Meta:
  41. verbose_name_plural = "Bread ingredients"
  42. class BreadType(RevisionMixin, models.Model):
  43. """
  44. A Django model to define the bread type
  45. It is made accessible in the Wagtail admin interface through the BreadTypeSnippetViewSet
  46. class in wagtail_hooks.py. This allows us to customize the admin interface for this snippet.
  47. In the BreadPage model you'll see we use a ForeignKey
  48. to create the relationship between BreadType and BreadPage. This allows a
  49. single relationship (e.g only one BreadType can be added) that is one-way
  50. (e.g. BreadType will have no way to access related BreadPage objects)
  51. """
  52. title = models.CharField(max_length=255)
  53. panels = [
  54. FieldPanel("title"),
  55. ]
  56. def __str__(self):
  57. return self.title
  58. class Meta:
  59. verbose_name_plural = "Bread types"
  60. class BreadPage(Page):
  61. """
  62. Detail view for a specific bread
  63. """
  64. introduction = models.TextField(help_text="Text to describe the page", blank=True)
  65. image = models.ForeignKey(
  66. "wagtailimages.Image",
  67. null=True,
  68. blank=True,
  69. on_delete=models.SET_NULL,
  70. related_name="+",
  71. help_text="Landscape mode only; horizontal width between 1000px and 3000px.",
  72. )
  73. body = StreamField(
  74. BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
  75. )
  76. origin = models.ForeignKey(
  77. Country,
  78. on_delete=models.SET_NULL,
  79. null=True,
  80. blank=True,
  81. )
  82. # We include related_name='+' to avoid name collisions on relationships.
  83. # e.g. there are two FooPage models in two different apps,
  84. # and they both have a FK to bread_type, they'll both try to create a
  85. # relationship called `foopage_objects` that will throw a valueError on
  86. # collision.
  87. bread_type = models.ForeignKey(
  88. "breads.BreadType",
  89. null=True,
  90. blank=True,
  91. on_delete=models.SET_NULL,
  92. related_name="+",
  93. )
  94. ingredients = ParentalManyToManyField("BreadIngredient", blank=True)
  95. content_panels = Page.content_panels + [
  96. FieldPanel("introduction"),
  97. FieldPanel("image"),
  98. FieldPanel("body"),
  99. FieldPanel("origin"),
  100. FieldPanel("bread_type"),
  101. MultiFieldPanel(
  102. [
  103. FieldPanel(
  104. "ingredients",
  105. widget=forms.CheckboxSelectMultiple,
  106. ),
  107. ],
  108. heading="Additional Metadata",
  109. classname="collapsed",
  110. ),
  111. ]
  112. search_fields = Page.search_fields + [
  113. index.SearchField("body"),
  114. ]
  115. parent_page_types = ["BreadsIndexPage"]
  116. class BreadsIndexPage(Page):
  117. """
  118. Index page for breads.
  119. This is more complex than other index pages on the bakery demo site as we've
  120. included pagination. We've separated the different aspects of the index page
  121. to be discrete functions to make it easier to follow
  122. """
  123. introduction = models.TextField(help_text="Text to describe the page", blank=True)
  124. image = models.ForeignKey(
  125. "wagtailimages.Image",
  126. null=True,
  127. blank=True,
  128. on_delete=models.SET_NULL,
  129. related_name="+",
  130. help_text="Landscape mode only; horizontal width between 1000px and " "3000px.",
  131. )
  132. content_panels = Page.content_panels + [
  133. FieldPanel("introduction"),
  134. FieldPanel("image"),
  135. ]
  136. # Can only have BreadPage children
  137. subpage_types = ["BreadPage"]
  138. # Returns a queryset of BreadPage objects that are live, that are direct
  139. # descendants of this index page with most recent first
  140. def get_breads(self):
  141. return (
  142. BreadPage.objects.live().descendant_of(self).order_by("-first_published_at")
  143. )
  144. # Allows child objects (e.g. BreadPage objects) to be accessible via the
  145. # template. We use this on the HomePage to display child items of featured
  146. # content
  147. def children(self):
  148. return self.get_children().specific().live()
  149. # Pagination for the index page. We use the `django.core.paginator` as any
  150. # standard Django app would, but the difference here being we have it as a
  151. # method on the model rather than within a view function
  152. def paginate(self, request, *args):
  153. page = request.GET.get("page")
  154. paginator = Paginator(self.get_breads(), 12)
  155. try:
  156. pages = paginator.page(page)
  157. except PageNotAnInteger:
  158. pages = paginator.page(1)
  159. except EmptyPage:
  160. pages = paginator.page(paginator.num_pages)
  161. return pages
  162. # Returns the above to the get_context method that is used to populate the
  163. # template
  164. def get_context(self, request):
  165. context = super(BreadsIndexPage, self).get_context(request)
  166. # BreadPage objects (get_breads) are passed through pagination
  167. breads = self.paginate(request, self.get_breads())
  168. context["breads"] = breads
  169. return context