2
0

models.py 7.0 KB

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