models.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from datetime import datetime
  2. from django.conf import settings
  3. from django.core.validators import RegexValidator
  4. from django.db import models
  5. from modelcluster.fields import ParentalKey
  6. from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
  7. from wagtail.wagtailcore.models import Orderable, Page
  8. from wagtail.wagtailsearch import index
  9. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  10. from bakerydemo.base.models import BasePageFieldsMixin
  11. class OperatingHours(models.Model):
  12. """
  13. Django model to capture operating hours for a Location
  14. """
  15. MONDAY = 'Mon'
  16. TUESDAY = 'Tue'
  17. WEDNESDAY = 'Wed'
  18. THURSDAY = 'Thu'
  19. FRIDAY = 'Fri'
  20. SATURDAY = 'Sat'
  21. SUNDAY = 'Sun'
  22. DAY_CHOICES = (
  23. (MONDAY, 'Mon'),
  24. (TUESDAY, 'Tue'),
  25. (WEDNESDAY, 'Weds'),
  26. (THURSDAY, 'Thu'),
  27. (FRIDAY, 'Fri'),
  28. (SATURDAY, 'Sat'),
  29. (SUNDAY, 'Sun'),
  30. )
  31. day = models.CharField(
  32. max_length=4,
  33. choices=DAY_CHOICES,
  34. default=MONDAY,
  35. )
  36. opening_time = models.TimeField(
  37. blank=True,
  38. null=True)
  39. closing_time = models.TimeField(
  40. blank=True,
  41. null=True)
  42. closed = models.BooleanField(
  43. "Closed?",
  44. blank=True,
  45. help_text='Tick if location is closed on this day'
  46. )
  47. panels = [
  48. FieldPanel('day'),
  49. FieldPanel('opening_time'),
  50. FieldPanel('closing_time'),
  51. FieldPanel('closed'),
  52. ]
  53. class Meta:
  54. abstract = True
  55. def __str__(self):
  56. if self.opening_time:
  57. opening = self.opening_time.strftime('%H:%M')
  58. else:
  59. opening = '--'
  60. if self.closing_time:
  61. closed = self.opening_time.strftime('%H:%M')
  62. else:
  63. closed = '--'
  64. return '{}: {} - {} {}'.format(
  65. self.day,
  66. opening,
  67. closed,
  68. settings.TIME_ZONE
  69. )
  70. class LocationOperatingHours(Orderable, OperatingHours):
  71. """
  72. Operating Hours entry for a Location
  73. """
  74. location = ParentalKey(
  75. 'LocationPage',
  76. related_name='hours_of_operation'
  77. )
  78. class LocationsIndexPage(BasePageFieldsMixin, Page):
  79. """
  80. Index page for locations
  81. """
  82. subpage_types = ['LocationPage']
  83. def get_context(self, request):
  84. context = super(LocationsIndexPage, self).get_context(request)
  85. context['locations'] = LocationPage.objects.descendant_of(
  86. self).live().order_by(
  87. 'title')
  88. return context
  89. content_panels = Page.content_panels + [
  90. FieldPanel('introduction', classname="full"),
  91. ImageChooserPanel('image'),
  92. ]
  93. class LocationPage(BasePageFieldsMixin, Page):
  94. """
  95. Detail for a specific bakery location.
  96. """
  97. address = models.TextField()
  98. lat_long = models.CharField(
  99. max_length=36,
  100. help_text="Comma separated lat/long. (Ex. 64.144367, -21.939182) \
  101. Right click Google Maps and select 'What\'s Here'",
  102. validators=[
  103. RegexValidator(
  104. regex='^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$',
  105. message='Lat Long must be a comma-separated numeric lat and long',
  106. code='invalid_lat_long'
  107. ),
  108. ]
  109. )
  110. # Search index configuration
  111. search_fields = Page.search_fields + [
  112. index.SearchField('address'),
  113. index.SearchField('body'),
  114. ]
  115. # Editor panels configuration
  116. content_panels = BasePageFieldsMixin.content_panels + [
  117. FieldPanel('address', classname="full"),
  118. FieldPanel('lat_long'),
  119. InlinePanel('hours_of_operation', label="Hours of Operation"),
  120. ]
  121. def __str__(self):
  122. return self.title
  123. @property
  124. def operating_hours(self):
  125. hours = self.hours_of_operation.all()
  126. return hours
  127. def is_open(self):
  128. # Determines if the location is currently open
  129. now = datetime.now()
  130. current_time = now.time()
  131. current_day = now.strftime('%a').upper()
  132. try:
  133. self.operating_hours.get(
  134. day=current_day,
  135. opening_time__lte=current_time,
  136. closing_time__gte=current_time
  137. )
  138. return True
  139. except LocationOperatingHours.DoesNotExist:
  140. return False
  141. def get_context(self, request):
  142. context = super(LocationPage, self).get_context(request)
  143. context['lat'] = self.lat_long.split(",")[0]
  144. context['long'] = self.lat_long.split(",")[1]
  145. return context
  146. parent_page_types = ['LocationsIndexPage']