瀏覽代碼

Work on #28; decided a template tag was not necessary to determine if a store was open or not; added model methods and updated template

David Ray 8 年之前
父節點
當前提交
af6bfc2e30
共有 2 個文件被更改,包括 33 次插入3 次删除
  1. 26 2
      bakerydemo/locations/models.py
  2. 7 1
      bakerydemo/templates/locations/location_page.html

+ 26 - 2
bakerydemo/locations/models.py

@@ -1,3 +1,6 @@
+from datetime import datetime
+
+from django.conf import settings
 from django.core.validators import RegexValidator
 from django.db import models
 
@@ -49,7 +52,12 @@ class OperatingHours(models.Model):
         abstract = True
 
     def __str__(self):
-        return '{}: {} - {}'.format(self.day, self.opening_time, self.closing_time)
+        return '{}: {} - {} {}'.format(
+            self.day,
+            self.opening_time.strftime('%H:%M'),
+            self.closing_time.strftime('%H:%M'),
+            settings.TIME_ZONE
+        )
 
 
 class LocationOperatingHours(Orderable, OperatingHours):
@@ -135,10 +143,26 @@ class LocationPage(Page):
     def __str__(self):
         return self.title
 
-    def opening_hours(self):
+    @property
+    def operating_hours(self):
         hours = self.hours_of_operation.all()
         return hours
 
+    def is_open(self):
+        # Determines if the location is currently open
+        now = datetime.now()
+        current_time = now.time()
+        current_day = now.strftime('%a').upper()
+        try:
+            self.operating_hours.get(
+                day=current_day,
+                opening_time__lte=current_time,
+                closing_time__gte=current_time
+            )
+            return True
+        except LocationOperatingHours.DoesNotExist:
+            return False
+
     def get_context(self, request):
         context = super(LocationPage, self).get_context(request)
         context['lat'] = self.lat_long.split(",")[0]

+ 7 - 1
bakerydemo/templates/locations/location_page.html

@@ -29,7 +29,13 @@
     <p>{{ page.address|linebreaks }}</p>
     <div id="map" class="maps embed-container"></div>
 
-    {% for hours in page.opening_hours %}
+    {% if page.is_open %}
+      Open Now!
+    {% else %}
+      Closed
+    {% endif %}
+
+    {% for hours in page.operating_hours %}
         <li>{{ hours }}</li>
     {% endfor %}