Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tzinfo added to bakery location #491

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bd24
30 changes: 30 additions & 0 deletions bakerydemo/locations/migrations/0007_locationpage_timezone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.13 on 2024-07-21 00:57

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("locations", "0006_alter_locationoperatinghours_day"),
]

operations = [
migrations.AddField(
model_name="locationpage",
name="timezone",
field=models.TextField(
default="Etc/UTC",
help_text="Examples: US/Pacific, America/Los_Angeles, Etc/UTC",
max_length=25,
validators=[
django.core.validators.RegexValidator(
code="invalid_tz",
message="Examples: US/Pacific, America/Los_Angeles, Etc/UTC",
regex="^(\\w+\\/?\\w+)$",
)
],
),
),
]
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 %}
Loading