Skip to content

Commit b1e9d91

Browse files
authored
Adding day view
and fortnight view, if you're into that.
1 parent eddd31f commit b1e9d91

1 file changed

Lines changed: 72 additions & 29 deletions

File tree

Calendar/Calendar.py

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ def get_events(dt):
1414
FROM Meetings m
1515
LEFT JOIN Organizations o ON m.OrganizationId = o.OrganizationId
1616
17-
WHERE m.MeetingDate < DATEADD(day, 1, '{0}') AND COALESCE(m.MeetingEnd, m.MeetingDate) > '{0}'
17+
WHERE m.MeetingDate < DATEADD(day, 1, '{0}')
18+
AND COALESCE(m.MeetingEnd, m.MeetingDate) > '{0}'
19+
AND m.Canceled = 0
1820
ORDER BY MeetingDate, o.DivisionId
1921
'''.format(dt))
2022

@@ -39,7 +41,8 @@ def generate_calendar_html(date):
3941

4042
prev_link = "?d=%d-%d" % (prev_year, prev_month)
4143
next_link = "?d=%d-%d" % (next_year, next_month)
42-
week_link = "?v=w&d=%d-%d-1" % (date.year, date.month)
44+
week_link = "?v=w&d=%d-%d-%d" % (date.year, date.month, date.day)
45+
day_link = "?v=d&d=%d-%d-%d" % (date.year, date.month, date.day)
4346

4447
html = []
4548
html.append("<style>")
@@ -49,17 +52,21 @@ def generate_calendar_html(date):
4952
html.append("th { background: #eee; }")
5053
html.append(".daynum { font-weight: bold; }")
5154
html.append(".event { margin: 2px 0; padding: 2px; background: #def; border-radius: 3px; }")
52-
html.append(".nav { display: flex; gap: 1em; }")
53-
html.append(".nav-l, .nav-r { flex:1; }")
55+
html.append(".nav.cal { display: flex; gap: 1em; }")
56+
html.append(".nav.cal > * { flex:1; }")
57+
html.append(".nav-c { text-align:center; }")
5458
html.append(".nav-r { text-align:right; }")
5559
html.append("</style>")
5660
# html.append("<h2>%s %d</h2>" % (calendar.month_name[month], year))
5761

5862
model.Title = "Calendar: %s %d" % (calendar.month_name[month], year)
5963
model.Header = "%s %d" % (calendar.month_name[month], year)
6064

61-
html.append("<div class='nav'>")
62-
html.append("<div class='nav-l'>")
65+
html.append("<div class='nav cal'>")
66+
67+
html.append("<div class='nav-l'></div>")
68+
69+
html.append("<div class='nav-c'>")
6370
html.append("<a href='%s'>&laquo; %s %d</a> | " % (prev_link, calendar.month_name[prev_month], prev_year))
6471
html.append("<strong>%s %d</strong>" % (calendar.month_name[month], year))
6572
html.append(" | <a href='%s'>%s %d &raquo;</a>" % (next_link, calendar.month_name[next_month], next_year))
@@ -68,6 +75,7 @@ def generate_calendar_html(date):
6875
html.append("<div class='nav-r'>")
6976
html.append("<strong>Month</strong>")
7077
html.append(" | <a href='%s'>Week</a>" % (week_link))
78+
html.append(" | <a href='%s'>Day</a>" % (day_link))
7179
html.append("</div>")
7280
html.append("</div>")
7381

@@ -85,8 +93,9 @@ def generate_calendar_html(date):
8593

8694
else:
8795
html.append("<td style='background:#f9f9f9; opacity:.5'>")
88-
89-
html.append("<div class='daynum'>%d</div>" % day.day)
96+
97+
day_link = "?v=d&d=%d-%d-%d" % (day.year, day.month, day.day)
98+
html.append("<a class='daynum' href='%s'>%d</div>" % (day_link, day.day))
9099
for ev in events:
91100

92101
classes = ['event']
@@ -100,8 +109,8 @@ def generate_calendar_html(date):
100109

101110
html.append("%s<br>" % ev.MeetingName)
102111
html.append(("<small>%s - %s</small>" % (
103-
ev.MeetingDate.ToString("h:mmtt"),
104-
ev.MeetingDate.ToString("h:mmtt") if ev.MeetingEnd is None else ev.MeetingEnd.ToString("h:mmtt")
112+
ev.MeetingDate.ToString("h:mmtt").replace('M',''),
113+
ev.MeetingDate.ToString("h:mmtt").replace('M','') if ev.MeetingEnd is None else ev.MeetingEnd.ToString("h:mmtt").replace('M','')
105114
)).lower().replace(":00",""))
106115

107116
html.append("</div></a>")
@@ -112,26 +121,40 @@ def generate_calendar_html(date):
112121
return "\n".join(html)
113122

114123

115-
def generate_calendar_week_html(start_date):
124+
def generate_calendar_vert_html(start_date, dayCount):
116125
"""
117126
Generate an HTML week view calendar with overlap handling,
118127
horizontal hour/half-hour lines, scrollable, and auto-scroll to 8am.
119128
"""
120-
start_of_week = start_date - datetime.timedelta(days=start_date.weekday() + 1 if start_date.weekday() != 6 else 0)
121-
days = [start_of_week + datetime.timedelta(days=i) for i in range(7)]
129+
start_of_span = start_date
130+
if dayCount % 7 == 0:
131+
start_of_span = start_date - datetime.timedelta(days=start_date.weekday() + 1 if start_date.weekday() != 6 else 0)
132+
days = [start_of_span + datetime.timedelta(days=i) for i in range(dayCount)]
133+
134+
span_term = "Span"
135+
span_link = str(dayCount)
136+
if dayCount == 14:
137+
span_term = "Fortnight"
138+
elif dayCount == 7:
139+
span_term = "Week"
140+
span_link = 'w'
141+
elif dayCount == 1:
142+
span_term = "Day"
143+
span_link = 'd'
122144

123145
hour_height = 6 # vh per hour
124146
day_start = 0
125147
day_end = 24
126148

127-
prev_date = start_date - datetime.timedelta(days=7)
128-
next_date = start_date + datetime.timedelta(days=7)
149+
prev_date = start_date - datetime.timedelta(days=dayCount)
150+
next_date = start_date + datetime.timedelta(days=dayCount)
129151

130-
prev_link = "?v=w&d=%d-%d-%d" % (prev_date.year, prev_date.month, prev_date.day)
131-
next_link = "?v=w&d=%d-%d-%d" % (next_date.year, next_date.month, next_date.day)
132-
month_link = "?d=%d-%d-%d" % (next_date.year, next_date.month, next_date.day)
152+
prev_link = "&d=%d-%d-%d" % (prev_date.year, prev_date.month, prev_date.day)
153+
next_link = "&d=%d-%d-%d" % (next_date.year, next_date.month, next_date.day)
154+
curr_link = "&d=%d-%d-%d" % (start_date.year, start_date.month, start_date.day)
155+
month_link = "?d=%d-%d-%d" % (start_date.year, start_date.month, start_date.day)
133156

134-
model.Title = "Calendar: Week of %s %d, %s" % (calendar.month_name[start_date.month], start_date.day, start_date.year)
157+
model.Title = "Calendar: %s of %s %d, %s" % (span_term, calendar.month_name[start_date.month], start_date.day, start_date.year)
135158

136159
html = []
137160
html.append("<style>")
@@ -144,26 +167,34 @@ def generate_calendar_week_html(start_date):
144167
html.append(".event { position: absolute; background: #def; border: 1px solid #69c; border-radius: 3px; padding: 2px; font-size: 0.85em; overflow: hidden; }")
145168
html.append(".feat { font-size: 1.1em; font-weight: bold; }")
146169
html.append(".timegrid { position: absolute; left: 0; right: 0; border-top: 1px dashed #ccc; font-size: 0.7em; color: #666; }")
147-
html.append(".nav { display: flex; gap: 1em; }")
148-
html.append(".nav-l, .nav-r { flex:1; }")
170+
html.append(".nav.cal { display: flex; gap: 1em; }")
171+
html.append(".nav.cal > * { flex:1; }")
149172
html.append(".nav-c { text-align:center; }")
150173
html.append(".nav-r { text-align:right; }")
151174
html.append("</style>")
152175

153-
html.append("<div class='nav'>")
176+
html.append("<div class='nav cal'>")
154177

155178
html.append("<div class='nav-l'>")
156-
html.append("<a href='%s'>&laquo; Previous Week</a> | " % prev_link)
157-
html.append("<a href='%s'>Next Week &raquo;</a>" % next_link)
158179
html.append("</div>")
159180

160181
html.append("<div class='nav-c'>")
161-
html.append("<strong>Week of %s %d, %s</strong>" % (calendar.month_name[start_date.month], start_date.day, start_date.year))
182+
html.append("<a href='?v=%s%s'>&laquo; Previous %s</a> | " % (span_link, prev_link, span_term))
183+
html.append("<strong>%s of %s %d, %s</strong>" % (span_term, calendar.month_name[start_date.month], start_date.day, start_date.year))
184+
html.append(" | <a href='?v=%s%s'>Next %s &raquo;</a>" % (span_link, next_link, span_term))
162185
html.append("</div>")
163186

164187
html.append("<div class='nav-r'>")
165188
html.append("<a href='%s'>Month</a>" % (month_link))
166-
html.append(" | <strong>Week</strong>")
189+
if dayCount == 7:
190+
html.append(" | <strong>Week</strong>")
191+
else:
192+
html.append(" | <a href='?v=w%s'>Week</a>" % (curr_link))
193+
if dayCount == 1:
194+
html.append(" | <strong>Day</strong>")
195+
else:
196+
html.append(" | <a href='?v=d%s'>Day</a>" % (curr_link))
197+
167198
html.append("</div>")
168199

169200
html.append("</div>")
@@ -229,8 +260,8 @@ def generate_calendar_week_html(start_date):
229260
' '.join(classes), top, height, left, width))
230261
html.append("%s<br><small>%s - %s</small>" % (
231262
ev.MeetingName,
232-
ev.MeetingDate.ToString("h:mmtt").lower().replace(":00",""),
233-
ev.MeetingEnd.ToString("h:mmtt").lower().replace(":00","") if ev.MeetingEnd else ""
263+
ev.MeetingDate.ToString("h:mmtt").lower().replace(":00","").replace('m',''),
264+
ev.MeetingEnd.ToString("h:mmtt").lower().replace(":00","").replace('m','') if ev.MeetingEnd else ""
234265
))
235266
html.append("</div></a>")
236267

@@ -268,8 +299,20 @@ def generate_calendar_week_html(start_date):
268299
except:
269300
pass
270301

302+
vi = 0
303+
try:
304+
vi = int(Data.v)
305+
except:
306+
pass
307+
271308
if Data.v == 'w':
272-
print generate_calendar_week_html(d)
309+
vi = 7
310+
311+
elif Data.v == 'd':
312+
vi = 1
313+
314+
if vi > 0:
315+
print generate_calendar_vert_html(d, vi)
273316

274317
else:
275318
print generate_calendar_html(d)

0 commit comments

Comments
 (0)