Skip to content

Commit

Permalink
Will it work?
Browse files Browse the repository at this point in the history
  • Loading branch information
Amondale committed Jul 19, 2024
1 parent 676f211 commit d3030d5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
43 changes: 41 additions & 2 deletions bakerydemo/locations/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

import pytz
from pytz import timezone
from django.conf import settings
from django.core.validators import RegexValidator
from django.db import models
Expand Down Expand Up @@ -120,6 +121,20 @@ class LocationPage(Page):
BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
)
address = models.TextField()

timezone = models.TextField(
max_length=25,
help_text="Examples: US/Pacific, America/Los_Angeles, Etc/UTC",
default='Etc/UTC',
validators=[
RegexValidator(
regex = r'^(\w+\/?\w+)$',
message ='Examples: US/Pacific, America/Los_Angeles, Etc/UTC',
code='invalid_tz'
)
]
)

lat_long = models.CharField(
max_length=36,
help_text="Comma separated lat/long. (Ex. 64.144367, -21.939182) \
Expand All @@ -146,6 +161,7 @@ class LocationPage(Page):
FieldPanel("image"),
FieldPanel("body"),
FieldPanel("address"),
FieldPanel('timezone', classname="full"),
FieldPanel("lat_long"),
InlinePanel("hours_of_operation", heading="Hours of Operation", label="Slot"),
]
Expand All @@ -159,6 +175,8 @@ def operating_hours(self):
return hours

# Determines if the location is currently open. It is timezone naive
'''
# original is_open
def is_open(self):
now = datetime.now()
current_time = now.time()
Expand All @@ -172,15 +190,36 @@ def is_open(self):
return True
except LocationOperatingHours.DoesNotExist:
return False
'''
def loc_datetime(self):
cur_zone = pytz.timezone(self.timezone)
now = datetime.now().astimezone(cur_zone)
return now.strftime('%a %b %d %Y %H:%M %Z')

def is_open(self):
cur_zone = pytz.timezone(self.timezone)
now = datetime.now().astimezone(cur_zone)
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

# Makes additional context available to the template so that we can access
# the latitude, longitude and map API key to render the map
def get_context(self, request):
context = super(LocationPage, self).get_context(request)
context['timezone'] = self.timezone
context["lat"] = self.lat_long.split(",")[0]
context["long"] = self.lat_long.split(",")[1]
context["google_map_api_key"] = settings.GOOGLE_MAP_API_KEY
return context

# Can only be placed under a LocationsIndexPage object
parent_page_types = ["LocationsIndexPage"]
parent_page_types = ["LocationsIndexPage"]
12 changes: 6 additions & 6 deletions bakerydemo/templates/locations/location_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

{% block content %}
{% include "base/include/header-hero.html" %}

<div class="container bread-detail">
<div class="row">
<div class="col-md-12">
Expand All @@ -23,14 +22,15 @@

<div class="col-md-4 col-md-offset-1">
<div class="row">
<div class="bread-detail__meta">
<div class="bread-detail__meta">
<p class="location__meta-title">Operating Status</p>
As of {{ page.loc_datetime }},<br>
This location is
{% if page.is_open %}
This location is currently open.
<font color=green>open</font>.
{% else %}
Sorry, this location is currently closed.
<font color=red>closed</font>.
{% endif %}

<p class="location__meta-title">Address</p>
<address>{{ page.address|linebreaks }}</address>

Expand Down Expand Up @@ -66,4 +66,4 @@
</div>
</div>

{% endblock content %}
{% endblock content %}

0 comments on commit d3030d5

Please sign in to comment.