2008-12-30 16:25:42 +00:00
|
|
|
# -*- coding: utf-8; indent-tabs-mode: nil; -*-
|
|
|
|
#
|
|
|
|
# This file is part of Radicale Server - Calendar Server
|
2009-07-27 15:04:54 +00:00
|
|
|
# Copyright © 2008-2009 Guillaume Ayoub
|
|
|
|
# Copyright © 2008 Nicolas Kandel
|
|
|
|
# Copyright © 2008 Pascal Halter
|
2008-12-30 16:25:42 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# TODO: Manage errors (see xmlutils)
|
2009-07-27 15:04:54 +00:00
|
|
|
# TODO: Forget twisted?
|
2008-12-30 16:25:42 +00:00
|
|
|
|
|
|
|
from twisted.web.resource import Resource
|
|
|
|
from twisted.web import http
|
|
|
|
import posixpath
|
|
|
|
|
|
|
|
import config
|
|
|
|
import support
|
|
|
|
import acl
|
|
|
|
import xmlutils
|
|
|
|
import calendar
|
|
|
|
|
|
|
|
_users = acl.users()
|
|
|
|
_calendars = support.calendars()
|
|
|
|
|
|
|
|
class CalendarResource(Resource):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Twisted resource for requests at calendar depth (/user/calendar)."""
|
|
|
|
# Tell twisted this is a leaf for requests
|
2008-12-30 16:25:42 +00:00
|
|
|
isLeaf = True
|
|
|
|
|
|
|
|
def __init__(self, user, cal):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Initialize by creating a calendar object.
|
|
|
|
|
|
|
|
The calendar object corresponds to the stocked calendar named
|
|
|
|
``user``/``cal``.
|
2008-12-30 16:25:42 +00:00
|
|
|
"""
|
|
|
|
Resource.__init__(self)
|
|
|
|
self.calendar = calendar.Calendar(user, cal)
|
|
|
|
|
|
|
|
def render_DELETE(self, request):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Manage DELETE ``request``."""
|
2008-12-30 16:25:42 +00:00
|
|
|
obj = request.getHeader("if-match")
|
|
|
|
answer = xmlutils.delete(obj, self.calendar, str(request.URLPath()))
|
|
|
|
request.setResponseCode(http.NO_CONTENT)
|
|
|
|
return answer
|
|
|
|
|
|
|
|
def render_OPTIONS(self, request):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Manage OPTIONS ``request``."""
|
2008-12-30 16:25:42 +00:00
|
|
|
request.setHeader("Allow", "DELETE, OPTIONS, PROPFIND, PUT, REPORT")
|
|
|
|
request.setHeader("DAV", "1, calendar-access")
|
|
|
|
request.setResponseCode(http.OK)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def render_PROPFIND(self, request):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Manage PROPFIND ``request``."""
|
|
|
|
xml_request = request.content.read()
|
|
|
|
answer = xmlutils.propfind(xml_request, self.calendar, str(request.URLPath()))
|
2008-12-30 16:25:42 +00:00
|
|
|
request.setResponseCode(http.MULTI_STATUS)
|
|
|
|
return answer
|
|
|
|
|
|
|
|
def render_PUT(self, request):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Manage PUT ``request``."""
|
2008-12-30 16:25:42 +00:00
|
|
|
# TODO: Improve charset detection
|
|
|
|
contentType = request.getHeader("content-type")
|
|
|
|
if contentType and "charset=" in contentType:
|
|
|
|
charset = contentType.split("charset=")[1].strip()
|
|
|
|
else:
|
|
|
|
charset = config.get("encoding", "request")
|
2009-07-27 15:04:54 +00:00
|
|
|
ical_request = request.content.read().decode(charset)
|
2008-12-30 16:25:42 +00:00
|
|
|
obj = request.getHeader("if-match")
|
2009-07-27 15:04:54 +00:00
|
|
|
xmlutils.put(ical_request, self.calendar, str(request.URLPath()), obj)
|
2008-12-30 16:25:42 +00:00
|
|
|
request.setResponseCode(http.CREATED)
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def render_REPORT(self, request):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Manage REPORT ``request``."""
|
|
|
|
xml_request = request.content.read()
|
|
|
|
answer = xmlutils.report(xml_request, self.calendar, str(request.URLPath()))
|
2008-12-30 16:25:42 +00:00
|
|
|
request.setResponseCode(http.MULTI_STATUS)
|
|
|
|
return answer
|
|
|
|
|
|
|
|
class UserResource(Resource):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Twisted resource for requests at user depth (/user)."""
|
2008-12-30 16:25:42 +00:00
|
|
|
def __init__(self, user):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Initialize by connecting requests to ``user`` calendars resources."""
|
2008-12-30 16:25:42 +00:00
|
|
|
Resource.__init__(self)
|
|
|
|
for cal in _calendars:
|
|
|
|
if cal.startswith("%s%s"%(user, posixpath.sep)):
|
2009-07-27 15:04:54 +00:00
|
|
|
cal_name = cal.split(posixpath.sep)[1]
|
|
|
|
self.putChild(cal_name, CalendarResource(user, cal))
|
2008-12-30 16:25:42 +00:00
|
|
|
|
|
|
|
def getChild(self, cal, request):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Get calendar resource if ``cal`` exists."""
|
2008-12-30 16:25:42 +00:00
|
|
|
if cal in _calendars:
|
|
|
|
return Resource.getChild(self, cal, request)
|
|
|
|
else:
|
|
|
|
return self
|
|
|
|
|
|
|
|
class HttpResource(Resource):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Twisted resource for requests at root depth (/)."""
|
2008-12-30 16:25:42 +00:00
|
|
|
def __init__(self):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Initialize by connecting requests to the users resources."""
|
2008-12-30 16:25:42 +00:00
|
|
|
Resource.__init__(self)
|
|
|
|
for user in _users:
|
|
|
|
self.putChild(user, UserResource(user))
|
|
|
|
|
|
|
|
def getChild(self, user, request):
|
2009-07-27 15:04:54 +00:00
|
|
|
"""Get user resource if ``user`` exists."""
|
2008-12-30 16:25:42 +00:00
|
|
|
if user in _users:
|
|
|
|
return Resource.getChild(self, user, request)
|
|
|
|
else:
|
|
|
|
return self
|