-
Notifications
You must be signed in to change notification settings - Fork 0
/
mycalender.py
77 lines (71 loc) · 2.93 KB
/
mycalender.py
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
import calendar
from datetime import datetime
class MyCalendar(calendar.LocaleHTMLCalendar):
def formatmonth(self, theyear, themonth, withyear=True):
# 親クラスほとんど継承 (クラスだけ違う)
v = []
a = v.append
# 変更点
a('<table class="table table-bordered table-sm" style="table-layout: fixed;">')
# 変更点ここまで
a('\n')
a(self.formatmonthname(theyear, themonth, withyear=withyear))
a('\n')
a(self.formatweekheader())
a('\n')
for week in self.monthdays2calendar(theyear, themonth):
# ここも違う。年月日全て渡すようにする(後述)
a(self.my_formatweek(week, theyear, themonth))
a('\n')
a('</table><br>')
a('\n')
return ''.join(v)
def my_formatweek(self, theweek, theyear, themonth):
'''
オーバーライド (引数を変えるのはPythonでは多分非推奨)
引数で year と month を渡すようにした。
'''
s = ''.join(self.my_formatday(d, theyear, themonth) for (d, wd) in theweek)
return '<tr>%s</tr>' % s
def my_formatday(self, day, theyear, themonth):
'''
オーバーライド (引数を変えるのはPythonでは多分非推奨)
引数で year と month を渡すようにした。
'''
if day == 0:
# 空白
return '<td style="background-color: #eeeeee"> </td>'
else:
html = '<td class="text-center {highlight}"><a href="{url}" style="color:{text}">{day}</a></td>'
text = 'blue'
highlight = ''
# もし予定があるなら強調
date = datetime(year=theyear, month=themonth, day=day)
date_str = date.strftime('%Y%m%d')
if date_str in self.linked_date:
# 終了した予定
if self.linked_date[date_str]:
highlight = 'bg-success'
text = 'white'
# 過去の予定
elif date < datetime.now():
highlight = 'bg-secondary'
text = 'white'
# これからの予定
else:
highlight = 'bg-warning'
# urlをtodo/{username}/year/month/day に変更
return html.format(
url='/todo/{}/{}/{}/{}'.format(self.username, theyear, themonth, day),
text=text,
day=day,
highlight=highlight)
def __init__(self, username, linked_date: dict):
calendar.LocaleHTMLCalendar.__init__(self,
firstweekday=6,
locale='ja_jp')
# 何か予定がある日付はリンクする
self.username = username
# dict{'datetime': done}
self.linked_date = linked_date
pass