Skip to content

Instantly share code, notes, and snippets.

@sbaechler
Last active December 19, 2015 02:28
Show Gist options
  • Save sbaechler/5883053 to your computer and use it in GitHub Desktop.
Save sbaechler/5883053 to your computer and use it in GitHub Desktop.
Opening Hours Display Tag
WEEKDAYS = (('mon', _('Montag')),
('tue', _('Dienstag')),
('wed', _('Mittwoch')),
('thu', _('Donnerstag')),
('fri', _('Freitag')),
('sat', _('Samstag')),
('sun', _('Sonntag')),
)
class OpeningHoursNode(template.Node):
def __init__(self, hours):
self.hours = template.Variable(hours)
def get_hours_list(self, hours):
"""
This tag prettifies the opening hours field for facebook places
@param hours: the field of the facebook page.
"""
from_day = WEEKDAYS[0][1]
to_day = WEEKDAYS[0][1]
o1, o2, c1, c2 = None, None, None, None # stored hours
result = []
for i in range(len(WEEKDAYS)):
no1 = hours.get('%s_1_open' % WEEKDAYS[i][0], None)
nc1 = hours.get('%s_1_close' % WEEKDAYS[i][0], None)
no2 = hours.get('%s_2_open' % WEEKDAYS[i][0], None)
nc2 = hours.get('%s_2_close' % WEEKDAYS[i][0], None)
# first run and open mon
if not (o1 or o2 or c1 or c2) and (no1 or no2):
o1, o2, c1, c2 = no1, no2, nc1, nc2
from_day = WEEKDAYS[i][1]
elif not (o1 or o2 or c1 or c2):
# beginning of the week and closed.
continue
# opening hours have not changed for the current day
if (no1 == o1 and no2 == o2 and nc1 == c1 and nc2 == c2 and no1):
to_day = WEEKDAYS[i][1]
if i < len(WEEKDAYS)-1:
continue
# opening hours have changed. Store and write out the previous days.
if from_day == to_day:
if o2:
result.append(((from_day, None),(o1,c1),(o2,c2)))
else:
result.append(((from_day, None),(o1,c1), None))
else:
if o2:
result.append(((from_day, to_day),(o1,c1),(o2,c2)))
else:
result.append(((from_day, to_day),(o1,c1), None))
if i == len(WEEKDAYS)-1: # last day, write it out
if from_day == to_day or to_day == WEEKDAYS[i-1][1]:
if no2:
result.append(((WEEKDAYS[i][1], None),(no1,nc1),(no2,nc2)))
elif no1:
result.append(((WEEKDAYS[i][1], None),(no1,nc1), None))
from_day = WEEKDAYS[i][1]
to_day = WEEKDAYS[i][1]
o1, o2, c1, c2 = no1, no2, nc1, nc2
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment