-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpycalweb.py
136 lines (103 loc) · 4.17 KB
/
pycalweb.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
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
from flask import Flask, Markup, render_template, request, g
import jinja2
import flask_sijax
import os
import time
import operator
from event_storage import *
from datetime import datetime
import htmlCalendar
tags = []
class Tag:
def __init__(self, name, icon=None):
self.name = name
self.icon_type, self.icon_data = icon.split(':')
def __str__(self):
return '%s,%s:' % (self.name, self.icon_type, self.icon_data)
def register(self):
tags.append(self)
def find_tag(name):
for tag in tags:
if tag.name == name:
return tag
else:
return None
Tag('PORT ZERO', 'img:port-zero-fav.png').register()
Tag('Cloud', 'color:blue').register()
Tag('Michael', 'color:darkgreen').register()
app = Flask(__name__, static_folder='templates')
app.config['SIJAX_STATIC_PATH'] = os.path.join('.', os.path.dirname(__file__), 'templates/sijax_js/')
flask_sijax.Sijax(app)
@app.route('/')
def show_month_view():
def sgn(x):
if x > 1:
return +1
else:
return -1
def make_calendar(month_offset=0):
cur_year = time.localtime().tm_year
cur_mon = time.localtime().tm_mon
# modular arithmetic with some minor adjustments
effective_month = ((cur_mon + month_offset - 1) % 12) + 1
effective_year = cur_year + ((cur_mon + month_offset - 1) // 12)
cal = None
if month_offset == 0:
cal = htmlCalendar.MonthlyCalendar(effective_year, effective_month, cssClass='current')
else:
cal = htmlCalendar.MonthlyCalendar(effective_year, effective_month)
for app in get_appointments_for_month(r, effective_month):
cal.viewEvent(app.datetime.day, app.datetime.day, app.text,
[find_tag(name) for name in app.tags])
cal.offset = 2 # week starts on Monday
return cal
prev_cals = [make_calendar(mo) for mo in range(-1,0)]
cur_cal = make_calendar()
next_cals = [make_calendar(mo) for mo in range(1,2)]
prev_cals_html = ''#reduce(operator.add, [cal.create() for cal in prev_cals])
cur_cal_html = cur_cal.create()
next_cals_html = reduce(operator.add, [cal.create() for cal in next_cals])
merged_html = '<div class="previous_calendars">' + prev_cals_html + '</div>';
merged_html += '<div class="current_calendars">' + cur_cal_html + '</div>';
merged_html += '<div class="next_calendars">' + next_cals_html + '</div>';
return render_template("cal.html",
cals_html=jinja2.Markup(merged_html),
events=get_appointments(r),
tags=tags)
@flask_sijax.route(app, '/add-event')
def add_event():
def process_add_event(obj_response, values):
print values
date = map(int, values['date'].split('-'))
add_appointment(r, datetime(date[0], date[1], date[2]), values['text'], values['tags'])
obj_response.script("$('#dialog').dialog('close');")
obj_response.script("location.reload();")
if g.sijax.is_sijax_request:
g.sijax.register_callback('process_add_event', process_add_event)
return g.sijax.process_request()
as_dialog = request.args.get('dialog')
date = request.args.get('date').split('-')
year, month, day = date
return render_template("add-event.html", year=year, month=month,
day=day, tags=tags)
@flask_sijax.route(app, '/edit-event')
def edit_event():
def process_edit_event(obj_response, values):
print values
date = map(int, values['date'].split('-'))
# STUB
# delete, add event
obj_response.script("$('#dialog').dialog('close');")
obj_response.script("location.reload();")
if g.sijax.is_sijax_request:
g.sijax.register_callback('process_edit_event', process_edit_event)
return g.sijax.process_request()
as_dialog = request.args.get('dialog')
date = request.args.get('date').split('-')
year, month, day = date
return render_template("edit-event.html", year=year, month=month,
day=day, tags=tags)
app.debug = True
r = redis.StrictRedis()
if __name__ == '__main__':
app.run()