-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
executable file
·147 lines (128 loc) · 4.81 KB
/
index.py
File metadata and controls
executable file
·147 lines (128 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import calendar
import os
from datetime import date, datetime, timedelta
import pandas as pd
from flask import Blueprint
from flask import request
import settings as s
import utils as u
index = Blueprint("index", __name__)
year = date.today().year
month_names = ["", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"]
def eventdb():
"""Load the events and return it as a 2D array of strings.
events[] = [year, month, day, filename, title, comments]"""
with open("data/list.txt", "r") as events:
events = events.read().splitlines()
events = sorted(events)
for n, e in enumerate(events):
e = e.split(">")
events[n] = [e[0][:4], e[0][4:6], e[0][6:8], e[0], e[2], e[1]]
return events
def month_events(month):
"""Return an HTML table of events in a given month."""
events = eventdb()
eventlist = [e for e in events if int(e[1]) == int(month)]
table = ["<table><tr><th>date<th>title"]
for e in eventlist:
table.append(f"<tr><td>{e[1]}-{e[2]}<td><a href='/e/{e[3]}'>{e[4]}</a>")
table.append("</table>")
return "\n".join(table)
@index.route('/list/')
def event_index():
"""Show a table of monthly events in HTML."""
with open("data/list.txt", "r") as entries:
entries = entries.read().splitlines()
entries = [e.split(">") for e in entries]
entries = sorted(entries, key=lambda x: x[0])
for n, e in enumerate(entries):
when = f"{e[0][4:6]}-{e[0][6:8]}"
entries[n].append(when)
e[2] = f"<a href='/e/{e[0]}'>{e[2]}</a>"
etable = ["<table><tr><th>date<th>title<th>guests"]
etable.append("<tr><td colspan='3'><center><a href='/create/'>Create new!</a></center>")
for e in entries:
etable.append("".join(["<tr><td>", "<td>".join([e[-1], e[2], e[1]])]))
etable.append("</table>")
return u.html("".join(etable), "Event list")
def cal(mont=2):
"For a given month, return a calendar as an HTML-formatted table."
monts = str(mont).zfill(2) # get month as 0 padded string
# Get the last day of the month...
last = calendar.monthrange(year, mont)[1]
# Find the number of weeks in a month
d1 = date(year, mont, 1)
d2 = date(year, mont, last)
startweek = d1.isocalendar()[1]
endweek = d2.isocalendar()[1]
if endweek < startweek:
endweek = date(year, 12, 28).isocalendar()[1] + 1
weeks = endweek-startweek
# Get the first and last days of the month's days of the week.
start = pd.Timestamp(f'{year}-{monts}-01').dayofweek
end = pd.Timestamp(f'{year}-{monts}-' + \
str(calendar.monthrange(year, mont)[1])).dayofweek
extra = 6 - end
# if (end == 0) or (start == 6):
# weeks += 1
pos = [0,0]
cnt = 0
prev, nex = 1, 12
if mont > 1:
prev = mont - 1
if mont < 12:
nex = mont + 1
mon = ["<table>"]
mon.append(f"<tr><th><a href='/calendar/{prev}'>« {prev}</a>")
mon.append(f"<th colspan='5'>{month_names[mont]} {year}</td>")
mon.append(f"<th><a href='/calendar/{nex}'>{nex} »</a>")
mon.append("<tr><th>Mon<th>Tue<th>Wed<th>Thu<th>Fri<th>Sat<th>Sun")
while pos != [weeks, 6]:
if pos[1] == 0:
mon.append("\n<tr>")
if pos[0] == 0:
if pos[1] < start:
mon.append("<td> .")
else:
cnt += 1
mon.append("<td>" + str(cnt)\
.zfill(2))
elif pos[0] == weeks and pos[1] > end:
mon.append("<td> .")
else:
cnt += 1
mon.append("<td>" + str(cnt)\
.zfill(2))
pos = [pos[0], pos[1]+1]
if pos[1] == 7:
pos = [pos[0]+1, 0]
if extra != 0:
mon.append("<td> .")
else:
mon.append("<td>" + str(cnt+1))
mon.append("</table>")
# Format the month based on its events:
month_events(mont)
return "".join(mon)
@index.route('/calendar/')
def currmonth():
"""Show the current month as a calendar with a list of events."""
month = date.today().month
return monthview(month)
@index.route('/calendar/<month>/')
def monthview(month):
"""Show a calendar for a given month and an HTML table of its events."""
table = cal(int(month))
events = eventdb()
eventlist = [e[2] for e in events if int(e[1]) == int(month)]
for e in eventlist:
table = table.replace(f"<td>{e}",
f"<td class='event'>{e}")
table = table.replace("<td> .", "<td class='null'>")
table += ("<p><center><a href='/create/'>Create New Event</a></center>")
elist = month_events(month)
if "-" in elist:
table += "<p>" + elist
return u.html(table, f"Calendar: {year}/{month}")