Skip to content

Instantly share code, notes, and snippets.

@robrocker7
Created March 28, 2012 19:10
Show Gist options
  • Save robrocker7/2229579 to your computer and use it in GitHub Desktop.
Save robrocker7/2229579 to your computer and use it in GitHub Desktop.
@property
def render_business_hours(self):
today = datetime.today()
day_table = [
'mon',
'tues',
'wed',
'thurs',
'fri',
'sat',
'sun'
]
timezone = pytz.timezone(self.timezone)
loc_dt = timezone.localize(today)
days = {}
groups = {}
prev_hours = None
index_holder = []
time_holder = []
html = ''
prev_time = None
for node in self.timenode_set.all():
for weekday in node.weekdays:
if int(weekday) not in days:
days[int(weekday)] = []
days[int(weekday)].append('%s:%s' % (
node.start.strftime('%H%M'), node.end.strftime('%H%M')))
for weekday, value in days.iteritems():
str_hours = '|'.join(value)
if prev_hours is not None and str_hours is prev_hours:
continue
else:
prev_hours = str_hours
if str_hours not in groups:
groups[str_hours] = []
groups[str_hours].append(weekday)
for iday in range(0, 7):
for time, weekdays in groups.iteritems():
if iday in weekdays:
if time is prev_time:
index_holder[len(index_holder) - 1].append(iday)
else:
prev_time = time
index_holder.append([iday])
time_holder.append(time)
for index in range(0, len(index_holder)):
day_set = index_holder[index]
time = time_holder[index]
hgroup = []
element = '<li>'
for hour_set in time.split('|'):
start, end = hour_set.split(':')
dt_start = datetime(
today.year,
today.month,
today.day,
int(start[:2]),
int(start[2:])).strftime('%I:%M%p')
dt_end = datetime(
today.year,
today.month,
today.day,
int(end[:2]),
int(end[2:])).strftime('%I:%M%p')
hgroup.append('%s - %s' % (dt_start, dt_end))
if len(day_set) > 1:
element = element + '<span>%s - %s</span>' % (
day_table[day_set[0]],
day_table[day_set[-1]],)
else:
element = element + '<span>%s</span>' % (
day_table[day_set[0]],)
element = element + ' %s %s </li>' % (' & '.join(hgroup),
loc_dt.strftime('%Z'))
html = html + element
return html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment