models.py 4.0 KB

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