-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtmlCalendar.py
More file actions
332 lines (271 loc) · 13.2 KB
/
htmlCalendar.py
File metadata and controls
332 lines (271 loc) · 13.2 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""
+-------------------------------------------------------------------+
| H T M L - C A L E N D A R (v2.16) |
| |
| Copyright Gerd Tentler www.gerd-tentler.de/tools |
| Created: May 27, 2003 Last modified: Feb. 12, 2012 |
+-------------------------------------------------------------------+
| This program may be used and hosted free of charge by anyone for |
| personal purpose as long as this copyright notice remains intact. |
| |
| Obtain permission before selling the code for this program or |
| hosting this software on a commercial website or redistributing |
| this software over the Internet or in any other medium. In all |
| cases copyright must remain intact. |
+-------------------------------------------------------------------+
EXAMPLE #1: myCal = calendar.MonthlyCalendar()
print myCal.create()
EXAMPLE #2: myCal = calendar.MonthlyCalendar(2004, 12)
print myCal.create()
EXAMPLE #3: myCal = calendar.MonthlyCalendar()
myCal.year = 2004
myCal.month = 12
print myCal.create()
Returns HTML code
=========================================================================================================
"""
import time
import math
import operator
import cgi
import json
def intersperse(iterable, delimiter):
it = iter(iterable)
yield next(it)
for x in it:
yield delimiter
yield x
cal_ID = 0
class MonthlyCalendar:
"""creates a monthly calendar"""
def __init__(self, year = None, month = None, week = None, cssClass = ''):
#========================================================================================================
# Configuration
#========================================================================================================
self.cssClass = cssClass
self.tFontFace = 'Arial, Helvetica' # title: font family (CSS-spec, e.g. "Arial, Helvetica")
self.tFontSize = 14 # title: font size (pixels)
self.tFontColor = '#FFFFFF' # title: font color
self.tBGColor = '#304B90' # title: background color
self.hFontFace = 'Arial, Helvetica' # heading: font family (CSS-spec, e.g. "Arial, Helvetica")
self.hFontSize = 12 # heading: font size (pixels)
self.hFontColor = '#FFFFFF' # heading: font color
self.hBGColor = '#304B90' # heading: background color
self.dFontFace = 'Arial, Helvetica' # days: font family (CSS-spec, e.g. "Arial, Helvetica")
self.dFontSize = 14 # days: font size (pixels)
self.dFontColor = '#000000' # days: font color
self.dBGColor = '#FFFFFF' # days: background color
self.wFontFace = 'Arial, Helvetica' # weeks: font family (CSS-spec, e.g. "Arial, Helvetica")
self.wFontSize = 12 # weeks: font size (pixels)
self.wFontColor = '#FFFFFF' # weeks: font color
self.wBGColor = '#304B90' # weeks: background color
self.saFontColor = '#0000D0' # Saturdays: font color
self.saBGColor = '#F6F6FF' # Saturdays: background color
self.suFontColor = '#D00000' # Sundays: font color
self.suBGColor = '#FFF0F0' # Sundays: background color
self.tdBorderColor = '#FF0000' # today: border color
self.borderColor = '#304B90' # border color
self.hilightColor = '#FFFF00' # hilight color (works only in combination with link)
self.link = '' # page to link to when day is clicked
self.linkTarget = '' # link target frame or window, e.g. parent.myFrame
self.offset = 1 # week start: 0 - 6 (0 = Saturday, 1 = Sunday, 2 = Monday ...)
self.weekNumbers = 1 # view week numbers: 1 = yes, 0 = no
#--------------------------------------------------------------------------------------------------------
# You should change these variables only if you want to translate them into your language:
#--------------------------------------------------------------------------------------------------------
# weekdays: must start with Saturday because January 1st of year 1 was a Saturday
self.weekdays = ('Sa', 'So', 'Mo', 'Di', 'Mi', 'Do', 'Fr')
# months: must start with January
self.months = ('Januar', 'Februar', u'Maerz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember')
# error messages
self.error = ('Year must be 1 - 3999!', 'Month must be 1 - 12!')
#========================================================================================================
if year is None and month is None:
year = time.localtime().tm_year
month = time.localtime().tm_mon
elif year is None and month is not None: year = time.localtime().tm_year
elif month is None: month = 1
if week is None: week = 0;
self.year = int(year)
self.month = int(month)
self.week = int(week)
self.specDays = {}
self.specDays2 = {}
if self.linkTarget == '': self.linkTarget = 'document'
__size = 0
__mDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def set_styles(self):
"""set calendar styles"""
globals()['cal_ID'] += 1
return ''
def leap_year(self, year):
"""check if year is a leap year"""
return not (year % 4) and (year < 1582 or year % 100 or not (year % 400))
def get_weekday(self, year, days):
"""return weekday (0 - 6) of nth day in year"""
a = days
if year: a += (year - 1) * 365
for i in range(1, year):
if self.leap_year(i): a += 1
if year > 1582 or (year == 1582 and days > 277): a -= 10
if a: a = (a - self.offset) % 7
elif self.offset: a += 7 - self.offset
return a
def get_week(self, year, days):
"""return week number of nth day in year"""
firstWDay = self.get_weekday(year, 0)
if year == 1582 and days > 277: days -= 10
return int(math.floor((days + firstWDay) / 7) + (firstWDay <= 3))
def table_cell(self, content, classes = '', date = '', header=False):
"""return formatted table cell with content"""
size = int(round(self.__size * 1.5))
if isinstance(classes, basestring):
classes = [classes]
if header:
html = '<th'
else:
html = '<td'
final_content = ''
event_info = []
if content != ' ': #and cls.lower().find('day') != -1:
eventClass = ''
events = []
tags = []
final_content += '<ul class="tags">'
if self.specDays.has_key(content):
for v in self.specDays[content]:
events.append(v[0])
if v[1]:
tags += v[1]
event_info.append({'event':v[0], 'tags':[tag.name for tag in v[1]]})
tags = list(frozenset(tags))
classes.append('event')
for tag in tags:
if tag.icon_type == 'color':
background = tag.icon_data
elif tag.icon_type == 'img':
background = "url('/templates/%s')" % tag.icon_data
else:
raise Exception("Tag icon type must be either 'color' or 'img'!")
final_content += '<li style="background:%s;"></li>' % background
# TODO append tags to css classes
#if eventClass:
# classes.append(eventClass)
final_content += '</ul>'
final_content += '<div class="content">' + content + '</div>'
if 'week' not in classes and header == False: # not supported yet
day = int(content)
if events:
click_behavior = 'edit'
else:
click_behavior = 'add'
html += (' onclick="%sEvent(%d,%d,%d);"' %
(click_behavior, self.year, self.month, day))
final_content += '<div class="actions">'
final_content += '</div>'
html += (' class="%s"' % reduce(operator.add, intersperse(classes, ' ')))
if event_info:
html += ' event-info="%s"' % cgi.escape(json.dumps(event_info))
html += '>' + final_content + '</td>'
return html
def table_head(self, content):
"""return formatted table head with content"""
cols = self.weekNumbers and '8' or '7'
html = '<thead>'
html += '<tr><th colspan=' + cols + ' class="cssTitle' + str(globals()['cal_ID']) + '"><b>' + \
content + '</b></th></tr><tr>'
for i in range(len(self.weekdays)):
ind = (i + self.offset) % 7
wDay = self.weekdays[ind]
html += self.table_cell(wDay, header=True)
if self.weekNumbers: html += self.table_cell('KW', header=True)
html += '</tr>'
html += '</thead>'
return html
def viewEvent(self, start, end, title, tags=[]):
"""add event to calendar"""
if start > end: return
if start < 1 or start > 31: return
if end < 1 or end > 31: return
while start <= end:
if not self.specDays.has_key(str(start)): self.specDays[str(start)] = []
self.specDays[str(start)].append((title, tags))
start += 1
def viewEventEach(self, weekday, title, tags=[]):
"""add event to calendar"""
if weekday < 0 or weekday > 6: return
if not self.specDays2.has_key(str(weekday)): self.specDays2[str(weekday)] = []
self.specDays2[str(weekday)].append((title, tags))
def create(self):
"""create monthly calendar"""
self.__size = (self.hFontSize > self.dFontSize) and self.hFontSize or self.dFontSize
if self.wFontSize > self.__size: self.__size = self.wFontSize
date = time.strftime('%Y-%m-%d', time.localtime())
(curYear, curMonth, curDay) = [int(v) for v in date.split('-')]
if self.year < 1 or self.year > 3999: html = '<b>' + self.error[0] + '</b>'
elif self.month < 1 or self.month > 12: html = '<b>' + self.error[1] + '</b>'
else:
self.__mDays[1] = self.leap_year(self.year) and 29 or 28
days = 0
for i in range(self.month - 1): days += self.__mDays[i]
start = self.get_weekday(self.year, days)
stop = self.__mDays[self.month-1]
html = self.set_styles()
html += ('<table class="calendar %s"><tr>' % self.cssClass)
title = self.months[self.month-1] + ' ' + str(self.year)
html += self.table_head(title)
daycount = 1
if self.year == curYear and self.month == curMonth: inThisMonth = 1
else: inThisMonth = 0
if self.weekNumbers or self.week: weekNr = self.get_week(self.year, days)
for i in range(self.__mDays[self.month-1] + 1):
for j, v in self.specDays2.iteritems():
if self.get_weekday(self.year, days + i) == int(j) - self.offset + 1:
if not self.specDays.has_key(str(i)): self.specDays[str(i)] = []
for v in self.specDays2[j]:
self.specDays[str(i)].append(v)
while daycount <= stop:
if self.week and self.week != weekNr:
daycount += 7 - (daycount == 1 and start or 0)
weekNr += 1
continue
html += '<tr>'
wdays = 0
for i in range(len(self.weekdays)):
cls = []
ind = (i + self.offset) % 7
if ind == 0: cls.append('Sa')
elif ind == 1: cls.append('Su')
else: cls.append(self.weekdays[i])
date = "%4d-%02d-%02d" % (self.year, self.month, daycount)
if (daycount == 1 and i < start) or daycount > stop:
content = ' '
cls.append('empty')
else:
content = str(daycount)
if inThisMonth and daycount == curDay:
cls.append('current')
elif self.year == 1582 and self.month == 10 and daycount == 4:
daycount = 14
daycount += 1
wdays += 1
html += self.table_cell(content, cls, date)
if self.weekNumbers:
if not weekNr:
if self.year == 1:
content = ' '
elif self.year == 1583:
content = '51'
else:
content = str(self.get_week(self.year - 1, 365))
elif self.month == 12 and weekNr >= 52 and wdays < 4:
content = '1'
else:
content = str(weekNr)
html += self.table_cell(content, 'week')
weekNr += 1
html += '</tr>'
html += '</table>'
return html
if __name__ == '__main__':
print __doc__