models.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from django.db import models
  2. from modelcluster.fields import ParentalKey
  3. from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
  4. from wagtail.wagtailcore.models import Orderable, Page
  5. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  6. from wagtail.wagtailsearch import index
  7. class OperatingHours(models.Model):
  8. '''
  9. Django model to capture operating hours for a Location
  10. '''
  11. MONDAY = 'MON'
  12. TUESDAY = 'TUE'
  13. WEDNESDAY = 'WED'
  14. THURSDAY = 'THU'
  15. FRIDAY = 'FRI'
  16. SATURDAY = 'SAT'
  17. SUNDAY = 'SUN'
  18. DAY_CHOICES = (
  19. (MONDAY, 'MON'),
  20. (TUESDAY, 'TUE'),
  21. (WEDNESDAY, 'WED'),
  22. (THURSDAY, 'THU'),
  23. (FRIDAY, 'FRI'),
  24. (SATURDAY, 'SAT'),
  25. (SUNDAY, 'SUN'),
  26. )
  27. day = models.CharField(
  28. max_length=3,
  29. choices=DAY_CHOICES,
  30. default=MONDAY,
  31. )
  32. opening_time = models.TimeField()
  33. closing_time = models.TimeField()
  34. panels = [
  35. FieldPanel('day'),
  36. FieldPanel('opening_time'),
  37. FieldPanel('closing_time'),
  38. ]
  39. class Meta:
  40. abstract = True
  41. def __str__(self):
  42. return '{}: {} - {}'.format(self.day, self.opening_time, self.closing_time)
  43. class LocationOperatingHours(Orderable, OperatingHours):
  44. '''
  45. Operating Hours entry for a Location
  46. '''
  47. location = ParentalKey(
  48. 'LocationPage',
  49. related_name='hours_of_operation'
  50. )
  51. class LocationsIndexPage(Page):
  52. '''
  53. Index page for locations
  54. '''
  55. subpage_types = ['LocationPage']
  56. def get_context(self, request):
  57. context = super(LocationsIndexPage, self).get_context(request)
  58. context['locations'] = LocationPage.objects.descendant_of(
  59. self).live().order_by(
  60. '-first_published_at')
  61. return context
  62. class LocationPage(Page):
  63. '''
  64. Detail for a specific location
  65. '''
  66. address = models.TextField()
  67. image = models.ForeignKey(
  68. 'wagtailimages.Image',
  69. null=True,
  70. blank=True,
  71. on_delete=models.SET_NULL,
  72. related_name='+'
  73. )
  74. lat_long = models.CharField(
  75. max_length=36,
  76. help_text="Comma separated lat/long. (Ex. 64.144367, -21.939182) \
  77. Right click Google Maps and click 'What\'s Here'"
  78. )
  79. # Search index configuration
  80. search_fields = Page.search_fields + [
  81. index.SearchField('address'),
  82. ]
  83. # Editor panels configuration
  84. content_panels = Page.content_panels + [
  85. FieldPanel('address', classname="full"),
  86. FieldPanel('lat_long'),
  87. ImageChooserPanel('image'),
  88. InlinePanel('hours_of_operation', label="Hours of Operation")
  89. ]
  90. def __str__(self):
  91. return self.title
  92. def opening_hours(self):
  93. hours = self.hours_of_operation.all()
  94. return hours
  95. def get_context(self, request):
  96. context = super(LocationPage, self).get_context(request)
  97. context['lat'] = self.lat_long.split(",")[0]
  98. context['long'] = self.lat_long.split(",")[1]
  99. return context
  100. parent_page_types = [
  101. 'LocationsIndexPage'
  102. ]