summaryrefslogtreecommitdiffstats
path: root/cal/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'cal/views.py')
-rw-r--r--cal/views.py139
1 files changed, 139 insertions, 0 deletions
diff --git a/cal/views.py b/cal/views.py
new file mode 100644
index 0000000..f2b7056
--- /dev/null
+++ b/cal/views.py
@@ -0,0 +1,139 @@
+import datetime, calendar
+import copy
+
+from django.template.loader import get_template
+from django.template import Context
+from infopoint.agenda.models import Event
+from django.http import HttpResponse, HttpResponseRedirect
+
+from controller import CalendarController
+
+
+def view(request):
+ """Calendar view"""
+ user = request.user
+ date = datetime.datetime.now()
+ default = dict(month=date.month, year=date.year, day=1, rowid=1,
+ title="", description="", start_date="")
+ url = request.path
+
+ p = getParams(request, default)
+ year, month, day = p['year'], p['month'], p['day']
+
+ # create calendar obj
+ cal = CalendarController(user, day)
+ cal.load(year, month)
+
+ # template
+ t = get_template('cal/calendar.html')
+ c = Context(dict(username=user, cal=cal, url=url))
+ html = t.render(c)
+
+ return HttpResponse(html)
+
+
+def addEvent(request):
+ """add event to calendar"""
+ user = request.user
+ default = dict(month=1, year=1, day=1, rowid=1, title="", desc="", when="")
+ p = getParams(request, default)
+ year, month, day = p['year'], p['month'], p['day']
+
+ # create calendar obj
+ cal = CalendarController(user, day)
+ cal.load(year, month)
+
+ # parpare data for adding Event
+ whenDate = datetime.date(year, month, day)
+ whenTime = datetime.time(*map(int, p['when'].strip().split(':')))
+ when = datetime.datetime.combine(whenDate, whenTime)
+
+ # adding Event
+ cal.addEvent(day, p['title'], when, p['Event.description'])
+
+ # show view with new result
+ return redirect2view(request.path, year, month, day)
+
+
+def delEvent(request):
+ """delete event from calendar"""
+ user = request.user
+ default = dict(month=1, year=1, day=1, rowid=1, title="", desc="", when="")
+ p = getParams(request, default)
+ year, month, day = p['year'], p['month'], p['day']
+
+ # create calendar obj
+ cal = CalendarController(user, day)
+ cal.load(year, month)
+
+ # delete Event
+ cal.delEvent(day, p['rowid'])
+
+ # show view with new result
+ return redirect2view(request.path, year, month, day)
+
+
+def updEvent(request):
+ """update selected event to calendar"""
+ user = request.user
+ default = dict(month=1, year=1, day=1, rowid=1, title="", description="", when="")
+ p = getParams(request, default)
+ year, month, day = p['year'], p['month'], p['day']
+
+ # create calendar obj
+ cal = CalendarController(user, day)
+ cal.load(year, month)
+
+ # parpare data for adding Event
+ whenDate = datetime.date(year, month, day)
+ whenTime = datetime.time(*map(int, p['when'].strip().split(':')))
+ when = datetime.datetime.combine(whenDate, whenTime)
+
+ # update Event
+ cal.updEvent(day, p['rowid'], p['title'], when, p['desc'])
+
+ # show view with new result
+ return redirect2view(request.path, year, month, day)
+
+
+## helper function
+def getParams(request, default):
+ """get parameter (case insensitive for key) (support both get and post)
+ param is updated as side effect (for now)"""
+ paramKeys = ['year', 'month', 'day', 'rowid', 'title', 'desc', 'when']
+ paramFunc = [int, int, int, int, str, str, str ]
+ paramDef = dict(zip(paramKeys, paramFunc))
+
+ inputDict = getattr(request, request.method, {})
+ result = copy.deepcopy(default)
+ for k in inputDict:
+ kl = k.lower()
+ if kl in default and kl in paramDef:
+ try:
+ result[kl] = paramDef[kl](getattr(request, request.method)[k])
+ except KeyError:
+ pass
+ return result
+
+
+def upOneLevelURL(url):
+ """eg1: upOneLevelURL('http://aaa/bbb/ccc') == 'http://aaa/bbb/'
+ eg2: upOneLevelURL('http://aaa/bbb/ccc/') == 'http://aaa/bbb/'
+ eg3: upOneLevelURL('http://aaa/bbb/ccc/?xyz') == 'http://aaa/bbb/'
+ eg4: upOneLevelURL('http://aaa/bbb/ccc?xyz') == 'http://aaa/bbb/'
+ """
+ sep = '/'
+ result = url.split('?')[0]
+ if result[-1] == sep:
+ result = result[:-1].rsplit(sep, 1)[0]
+ else:
+ result = result.rsplit(sep, 1)[0]
+ return result + sep
+
+
+def redirect2view(url, year, month, day):
+ url = upOneLevelURL(url)
+ qstr = '?year=%d&month=%d&day=%d' % (year, month, day)
+ url += qstr
+ return HttpResponseRedirect(url)
+