models.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. subpage_types = ['LocationPage']
  74. def get_context(self, request):
  75. context = super(LocationsIndexPage, self).get_context(request)
  76. context['locations'] = LocationPage.objects.descendant_of(
  77. self).live().order_by(
  78. '-first_published_at')
  79. return context
  80. class LocationPage(Page):
  81. """
  82. Detail for a specific bakery location.
  83. """
  84. introduction = models.TextField(
  85. help_text='Text to describe the index page',
  86. blank=True)
  87. address = models.TextField()
  88. image = models.ForeignKey(
  89. 'wagtailimages.Image',
  90. null=True,
  91. blank=True,
  92. on_delete=models.SET_NULL,
  93. related_name='+'
  94. )
  95. lat_long = models.CharField(
  96. max_length=36,
  97. help_text="Comma separated lat/long. (Ex. 64.144367, -21.939182) \
  98. Right click Google Maps and select 'What\'s Here'",
  99. validators=[
  100. RegexValidator(
  101. regex='^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$',
  102. message='Lat Long must be a comma-separated numeric lat and long',
  103. code='invalid_lat_long'
  104. ),
  105. ]
  106. )
  107. body = StreamField(
  108. BaseStreamBlock(), verbose_name="About page detail", blank=True
  109. )
  110. # We've defined the StreamBlock() within blocks.py that we've imported on
  111. # line 12. Defining it in a different file gives us consistency across the
  112. # site, though StreamFields _can_ be created on a per model basis if you
  113. # have a use case for it
  114. # Search index configuration
  115. search_fields = Page.search_fields + [
  116. index.SearchField('address'),
  117. ]
  118. # Editor panels configuration
  119. content_panels = Page.content_panels + [
  120. FieldPanel('introduction', classname="full"),
  121. FieldPanel('address', classname="full"),
  122. FieldPanel('lat_long'),
  123. ImageChooserPanel('image'),
  124. InlinePanel('hours_of_operation', label="Hours of Operation"),
  125. StreamFieldPanel('body')
  126. ]
  127. def __str__(self):
  128. return self.title
  129. def opening_hours(self):
  130. hours = self.hours_of_operation.all()
  131. return hours
  132. def get_context(self, request):
  133. context = super(LocationPage, self).get_context(request)
  134. context['lat'] = self.lat_long.split(",")[0]
  135. context['long'] = self.lat_long.split(",")[1]
  136. return context
  137. parent_page_types = ['LocationsIndexPage']