|
| 1 | +import calendar |
| 2 | +import datetime |
| 3 | + |
| 4 | +def get_events(dt): |
| 5 | + return q.QuerySql(''' |
| 6 | + SELECT |
| 7 | + m.MeetingDate, |
| 8 | + m.MeetingEnd, |
| 9 | + COALESCE(NULLIF(m.Description,''), o.organizationName) as MeetingName, |
| 10 | + m.MeetingId, |
| 11 | + 1 * o.ShowInSites as Featured |
| 12 | + FROM Meetings m |
| 13 | + LEFT JOIN Organizations o ON m.OrganizationId = o.OrganizationId |
| 14 | + |
| 15 | + WHERE m.MeetingDate < DATEADD(day, 1, '{0}') AND m.MeetingEnd > '{0}' |
| 16 | + ORDER BY MeetingDate, o.DivisionId |
| 17 | +'''.format(dt)) |
| 18 | + |
| 19 | +def generate_calendar_html(year, month): |
| 20 | + cal = calendar.Calendar(firstweekday=6) # Sunday start |
| 21 | + month_days = cal.monthdatescalendar(year, month) |
| 22 | + |
| 23 | + # Compute adjacent months |
| 24 | + prev_month = month - 1 |
| 25 | + prev_year = year |
| 26 | + next_month = month + 1 |
| 27 | + next_year = year |
| 28 | + |
| 29 | + if prev_month == 0: |
| 30 | + prev_month = 12 |
| 31 | + prev_year -= 1 |
| 32 | + if next_month == 13: |
| 33 | + next_month = 1 |
| 34 | + next_year += 1 |
| 35 | + |
| 36 | + prev_link = "?year=%d&month=%d" % (prev_year, prev_month) |
| 37 | + next_link = "?year=%d&month=%d" % (next_year, next_month) |
| 38 | + |
| 39 | + html = [] |
| 40 | + html.append("<style>") |
| 41 | + html.append("table { border-collapse: collapse; width: 100%; }") |
| 42 | + html.append("th, td { border: 1px solid #999; vertical-align: top; width: 14%; }") |
| 43 | + html.append(".feat { font-size: 1.3em; font-weight: bold; }") |
| 44 | + html.append("th { background: #eee; }") |
| 45 | + html.append(".daynum { font-weight: bold; }") |
| 46 | + html.append(".event { margin: 2px 0; padding: 2px; background: #def; border-radius: 3px; }") |
| 47 | + html.append("</style>") |
| 48 | + # html.append("<h2>%s %d</h2>" % (calendar.month_name[month], year)) |
| 49 | + |
| 50 | + model.Title = "Calendar: %s %d" % (calendar.month_name[month], year) |
| 51 | + model.Header = "%s %d" % (calendar.month_name[month], year) |
| 52 | + |
| 53 | + html.append("<div class='nav'>") |
| 54 | + html.append("<a href='%s'>« %s %d</a> | " % (prev_link, calendar.month_name[prev_month], prev_year)) |
| 55 | + html.append("<strong>%s %d</strong>" % (calendar.month_name[month], year)) |
| 56 | + html.append(" | <a href='%s'>%s %d »</a>" % (next_link, calendar.month_name[next_month], next_year)) |
| 57 | + html.append("</div>") |
| 58 | + |
| 59 | + |
| 60 | + html.append("<table>") |
| 61 | + html.append("<tr>" + "".join("<th>%s</th>" % d for d in ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]) + "</tr>") |
| 62 | + |
| 63 | + for week in month_days: |
| 64 | + html.append("<tr>") |
| 65 | + for day in week: |
| 66 | + events = get_events(day) |
| 67 | + |
| 68 | + if day.month == month: |
| 69 | + html.append("<td>") |
| 70 | + |
| 71 | + else: |
| 72 | + html.append("<td style='background:#f9f9f9; opacity:.5'>") |
| 73 | + |
| 74 | + html.append("<div class='daynum'>%d</div>" % day.day) |
| 75 | + for ev in events: |
| 76 | + |
| 77 | + classes = ['event'] |
| 78 | + |
| 79 | + if ev.Featured: |
| 80 | + classes.append('feat') |
| 81 | + |
| 82 | + html.append("<a href='/Meeting/%s'>" % ev.MeetingId) |
| 83 | + |
| 84 | + html.append("<div class='%s'>" % ' '.join(classes)) |
| 85 | + |
| 86 | + html.append("%s<br>" % ev.MeetingName) |
| 87 | + html.append(("<small>%s - %s</small>" % ( |
| 88 | + ev.MeetingDate.ToString("h:mmtt"), |
| 89 | + ev.MeetingDate.ToString("h:mmtt") if ev.MeetingEnd is None else ev.MeetingEnd.ToString("h:mmtt") |
| 90 | + )).lower().replace(":00","")) |
| 91 | + |
| 92 | + html.append("</div></a>") |
| 93 | + html.append("</td>") |
| 94 | + html.append("</tr>") |
| 95 | + |
| 96 | + html.append("</table>") |
| 97 | + return "\n".join(html) |
| 98 | + |
| 99 | + |
| 100 | +if True: |
| 101 | + |
| 102 | + today = datetime.date.today() |
| 103 | + year = today.year |
| 104 | + month = today.month |
| 105 | + |
| 106 | + if Data.year != '': |
| 107 | + try: |
| 108 | + year = int(Data.year) |
| 109 | + except: |
| 110 | + pass |
| 111 | + if Data.month != '': |
| 112 | + try: |
| 113 | + month = int(Data.month) |
| 114 | + except: |
| 115 | + pass |
| 116 | + |
| 117 | + html_calendar = generate_calendar_html(year, month) |
| 118 | + print(html_calendar) |
0 commit comments