models.py 3.4 KB

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