models.py 5.9 KB

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