| 1 |
import calendar, copy |
|---|
| 2 |
from cherrypy import expose |
|---|
| 3 |
from datetime import date |
|---|
| 4 |
from FileCabinet import get_entries_by_date |
|---|
| 5 |
from utils import configMap |
|---|
| 6 |
|
|---|
| 7 |
class Calendar(object): |
|---|
| 8 |
def __init__(self, parent): |
|---|
| 9 |
self.now = date.today() |
|---|
| 10 |
self.monthname = self.now.strftime('%B') |
|---|
| 11 |
self.cal = calendar.monthcalendar(self.now.year, self.now.month) |
|---|
| 12 |
self.parent = parent |
|---|
| 13 |
|
|---|
| 14 |
@expose |
|---|
| 15 |
def index(self): |
|---|
| 16 |
cal = copy.deepcopy(self.cal) |
|---|
| 17 |
cal = self.get_entry_dates(self.now.month, self.now.year, cal) |
|---|
| 18 |
return self.render_calendar(self.monthname, self.now.month, self.now.year, cal) |
|---|
| 19 |
|
|---|
| 20 |
def get_entry_dates(self, month, year, cal): |
|---|
| 21 |
entryDays = set() |
|---|
| 22 |
entries = get_entries_by_date(year, month) |
|---|
| 23 |
for e in get_entries_by_date(year, month): |
|---|
| 24 |
entryDays.add(e.time_tuple[2]) |
|---|
| 25 |
for week in cal: |
|---|
| 26 |
for i in range(0, 7): |
|---|
| 27 |
|
|---|
| 28 |
if week[i] in entryDays: |
|---|
| 29 |
week[i] = (week[i], True) |
|---|
| 30 |
else: |
|---|
| 31 |
week[i] = (week[i], False) |
|---|
| 32 |
return cal |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
def render_calendar(self, monthname, month, year, calendar): |
|---|
| 42 |
conf = configMap['cherryblossom'].copy() |
|---|
| 43 |
conf.update({'monthname': monthname, |
|---|
| 44 |
'month': month, |
|---|
| 45 |
'year': year, |
|---|
| 46 |
'cal': calendar}) |
|---|
| 47 |
return [('head', conf), |
|---|
| 48 |
('browse', conf), |
|---|
| 49 |
('foot', conf)] |
|---|
| 50 |
|
|---|
| 51 |
def default(self): |
|---|
| 52 |
pass |
|---|