models.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 ParentalKey, ParentalManyToManyField
  5. from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
  6. from wagtail.wagtailcore.fields import StreamField
  7. from wagtail.wagtailcore.models import Page
  8. from wagtail.wagtailsearch import index
  9. from wagtail.wagtailsnippets.models import register_snippet
  10. from bakerydemo.base.blocks import BaseStreamBlock
  11. from bakerydemo.base.models import BasePageFieldsMixin
  12. @register_snippet
  13. class Country(models.Model):
  14. """
  15. Standard Django model to store set of countries of origin.
  16. Exposed in the Wagtail admin via Snippets.
  17. """
  18. title = models.CharField(max_length=100)
  19. def __str__(self):
  20. return self.title
  21. class Meta:
  22. verbose_name_plural = "Countries of Origin"
  23. @register_snippet
  24. class BreadIngredient(models.Model):
  25. """
  26. Standard Django model used as a Snippet in the BreadPage model.
  27. Demonstrates ManyToMany relationship.
  28. """
  29. name = models.CharField(max_length=255)
  30. panels = [
  31. FieldPanel('name'),
  32. ]
  33. def __str__(self):
  34. return self.name
  35. class Meta:
  36. verbose_name_plural = 'Bread ingredients'
  37. @register_snippet
  38. class BreadType(models.Model):
  39. """
  40. Standard Django model used as a Snippet in the BreadPage model.
  41. """
  42. title = models.CharField(max_length=255)
  43. panels = [
  44. FieldPanel('title'),
  45. ]
  46. def __str__(self):
  47. return self.title
  48. class Meta:
  49. verbose_name_plural = "Bread types"
  50. class BreadPage(BasePageFieldsMixin, Page):
  51. """
  52. Detail view for a specific bread
  53. """
  54. origin = models.ForeignKey(
  55. Country,
  56. on_delete=models.SET_NULL,
  57. null=True,
  58. blank=True,
  59. )
  60. body = StreamField(
  61. BaseStreamBlock(), verbose_name="Describe the bread", blank=True
  62. )
  63. bread_type = models.ForeignKey(
  64. 'breads.BreadType',
  65. null=True,
  66. blank=True,
  67. on_delete=models.SET_NULL,
  68. related_name='+'
  69. )
  70. ingredients = ParentalManyToManyField('BreadIngredient', blank=True)
  71. content_panels = BasePageFieldsMixin.content_panels + [
  72. StreamFieldPanel('body'),
  73. FieldPanel('origin'),
  74. FieldPanel('bread_type'),
  75. FieldPanel('ingredients', widget=forms.CheckboxSelectMultiple),
  76. ]
  77. search_fields = Page.search_fields + [
  78. index.SearchField('title'),
  79. index.SearchField('body'),
  80. ]
  81. parent_page_types = ['BreadsIndexPage']
  82. api_fields = ['title', 'bread_type', 'origin', 'image']
  83. class BreadsIndexPage(BasePageFieldsMixin, Page):
  84. """
  85. Index page for breads. We don't have any fields within our model but we need
  86. to alter the page model's context to return the child page objects - the
  87. BreadPage - so that it works as an index page
  88. """
  89. subpage_types = ['BreadPage']
  90. def get_context(self, request):
  91. context = super(BreadsIndexPage, self).get_context(request)
  92. # Get the full unpaginated listing of resource pages as a queryset -
  93. # replace this with your own query as appropriate
  94. all_resources = self.get_children().live()
  95. paginator = Paginator(all_resources, 5) # Show 5 resources per page
  96. page = request.GET.get('page')
  97. try:
  98. resources = paginator.page(page)
  99. except PageNotAnInteger:
  100. # If page is not an integer, deliver first page.
  101. resources = paginator.page(1)
  102. except EmptyPage:
  103. # If page is out of range (e.g. 9999), deliver last page of results.
  104. resources = paginator.page(paginator.num_pages)
  105. # make the variable 'resources' available on the template
  106. context['resources'] = resources
  107. context['paginator'] = paginator
  108. return context