models.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. from django.core.validators import RegexValidator
  2. from django.db import models
  3. from modelcluster.fields import ParentalKey
  4. from wagtail.wagtailadmin.edit_handlers import (
  5. FieldPanel,
  6. InlinePanel,
  7. StreamFieldPanel)
  8. from wagtail.wagtailcore.models import Orderable, Page
  9. from wagtail.wagtailimages.edit_handlers import (
  10. ImageChooserPanel,
  11. )
  12. from wagtail.wagtailcore.fields import StreamField
  13. from wagtail.wagtailsearch import index
  14. from bakerydemo.base.blocks import BaseStreamBlock
  15. class OperatingHours(models.Model):
  16. """
  17. Django model to capture operating hours for a Location
  18. """
  19. MONDAY = 'Mon'
  20. TUESDAY = 'Tue'
  21. WEDNESDAY = 'Wed'
  22. THURSDAY = 'Thu'
  23. FRIDAY = 'Fri'
  24. SATURDAY = 'Sat'
  25. SUNDAY = 'Sun'
  26. DAY_CHOICES = (
  27. (MONDAY, 'Mon'),
  28. (TUESDAY, 'Tue'),
  29. (WEDNESDAY, 'Weds'),
  30. (THURSDAY, 'Thu'),
  31. (FRIDAY, 'Fri'),
  32. (SATURDAY, 'Sat'),
  33. (SUNDAY, 'Sun'),
  34. )
  35. day = models.CharField(
  36. max_length=4,
  37. choices=DAY_CHOICES,
  38. default=MONDAY,
  39. )
  40. opening_time = models.TimeField(
  41. blank=True,
  42. null=True)
  43. closing_time = models.TimeField(
  44. blank=True,
  45. null=True)
  46. closed = models.BooleanField(
  47. "Closed?",
  48. blank=True,
  49. help_text='Tick if location is closed on this day'
  50. )
  51. panels = [
  52. FieldPanel('day'),
  53. FieldPanel('opening_time'),
  54. FieldPanel('closing_time'),
  55. FieldPanel('closed'),
  56. ]
  57. class Meta:
  58. abstract = True
  59. def __str__(self):
  60. return '{}: {} - {}'.format(self.day, self.opening_time, self.closing_time)
  61. class LocationOperatingHours(Orderable, OperatingHours):
  62. """
  63. Operating Hours entry for a Location
  64. """
  65. location = ParentalKey(
  66. 'LocationPage',
  67. related_name='hours_of_operation'
  68. )
  69. class LocationsIndexPage(Page):
  70. """
  71. Index page for locations
  72. """
  73. introduction = models.TextField(
  74. help_text='Text to describe the index page',
  75. blank=True)
  76. image = models.ForeignKey(
  77. 'wagtailimages.Image',
  78. null=True,
  79. blank=True,
  80. on_delete=models.SET_NULL,
  81. related_name='+',
  82. help_text='Location listing image'
  83. )
  84. subpage_types = ['LocationPage']
  85. content_panels = Page.content_panels + [
  86. FieldPanel('introduction'),
  87. ImageChooserPanel('image'),
  88. ]
  89. def get_context(self, request):
  90. context = super(LocationsIndexPage, self).get_context(request)
  91. context['locations'] = LocationPage.objects.descendant_of(
  92. self).live().order_by(
  93. 'title')
  94. return context
  95. class LocationPage(Page):
  96. """
  97. Detail for a specific bakery location.
  98. """
  99. introduction = models.TextField(
  100. help_text='Text to describe the index page',
  101. blank=True)
  102. address = models.TextField()
  103. image = models.ForeignKey(
  104. 'wagtailimages.Image',
  105. null=True,
  106. blank=True,
  107. on_delete=models.SET_NULL,
  108. related_name='+'
  109. )
  110. lat_long = models.CharField(
  111. max_length=36,
  112. help_text="Comma separated lat/long. (Ex. 64.144367, -21.939182) \
  113. Right click Google Maps and select 'What\'s Here'",
  114. validators=[
  115. RegexValidator(
  116. regex='^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$',
  117. message='Lat Long must be a comma-separated numeric lat and long',
  118. code='invalid_lat_long'
  119. ),
  120. ]
  121. )
  122. body = StreamField(
  123. BaseStreamBlock(), verbose_name="About page detail", blank=True
  124. )
  125. # We've defined the StreamBlock() within blocks.py that we've imported on
  126. # line 12. Defining it in a different file gives us consistency across the
  127. # site, though StreamFields _can_ be created on a per model basis if you
  128. # have a use case for it
  129. # Search index configuration
  130. search_fields = Page.search_fields + [
  131. index.SearchField('address'),
  132. ]
  133. # Editor panels configuration
  134. content_panels = Page.content_panels + [
  135. FieldPanel('introduction', classname="full"),
  136. FieldPanel('address', classname="full"),
  137. FieldPanel('lat_long'),
  138. ImageChooserPanel('image'),
  139. InlinePanel('hours_of_operation', label="Hours of Operation"),
  140. StreamFieldPanel('body')
  141. ]
  142. def __str__(self):
  143. return self.title
  144. def opening_hours(self):
  145. hours = self.hours_of_operation.all()
  146. return hours
  147. def get_context(self, request):
  148. context = super(LocationPage, self).get_context(request)
  149. context['lat'] = self.lat_long.split(",")[0]
  150. context['long'] = self.lat_long.split(",")[1]
  151. return context
  152. parent_page_types = ['LocationsIndexPage']