models.py 8.2 KB

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