|
| 1 | +# -*- coding: utf-8; indent-tabs-mode: nil; -*- |
| 2 | +# |
| 3 | +# This file is part of Radicale Server - Calendar Server |
| 4 | +# Copyright © 2008 The Radicale Team |
| 5 | +# |
| 6 | +# This library is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This library is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with Radicale. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +# TODO: Manage errors (see xmlutils) |
| 20 | + |
| 21 | +from twisted.web.resource import Resource |
| 22 | +from twisted.web import http |
| 23 | +import posixpath |
| 24 | + |
| 25 | +import config |
| 26 | + |
| 27 | +import support |
| 28 | +import acl |
| 29 | + |
| 30 | +import xmlutils |
| 31 | +import calendar |
| 32 | + |
| 33 | +_users = acl.users() |
| 34 | +_calendars = support.calendars() |
| 35 | + |
| 36 | +class CalendarResource(Resource): |
| 37 | + """ |
| 38 | + Twisted resource for requests at calendar depth (/user/calendar) |
| 39 | + """ |
| 40 | + isLeaf = True |
| 41 | + |
| 42 | + def __init__(self, user, cal): |
| 43 | + """ |
| 44 | + Initialize resource creating a calendar object corresponding |
| 45 | + to the stocked calendar named user/cal |
| 46 | + """ |
| 47 | + Resource.__init__(self) |
| 48 | + self.calendar = calendar.Calendar(user, cal) |
| 49 | + |
| 50 | + def render_DELETE(self, request): |
| 51 | + """ |
| 52 | + Manage DELETE requests |
| 53 | + """ |
| 54 | + obj = request.getHeader("if-match") |
| 55 | + answer = xmlutils.delete(obj, self.calendar, str(request.URLPath())) |
| 56 | + request.setResponseCode(http.NO_CONTENT) |
| 57 | + return answer |
| 58 | + |
| 59 | + def render_OPTIONS(self, request): |
| 60 | + """ |
| 61 | + Manage OPTIONS requests |
| 62 | + """ |
| 63 | + request.setHeader("Allow", "DELETE, OPTIONS, PROPFIND, PUT, REPORT") |
| 64 | + request.setHeader("DAV", "1, calendar-access") |
| 65 | + request.setResponseCode(http.OK) |
| 66 | + return "" |
| 67 | + |
| 68 | + def render_PROPFIND(self, request): |
| 69 | + """ |
| 70 | + Manage PROPFIND requests |
| 71 | + """ |
| 72 | + xmlRequest = request.content.read() |
| 73 | + answer = xmlutils.propfind(xmlRequest, self.calendar, str(request.URLPath())) |
| 74 | + request.setResponseCode(http.MULTI_STATUS) |
| 75 | + return answer |
| 76 | + |
| 77 | + def render_PUT(self, request): |
| 78 | + """ |
| 79 | + Manage PUT requests |
| 80 | + """ |
| 81 | + # TODO: Improve charset detection |
| 82 | + contentType = request.getHeader("content-type") |
| 83 | + if contentType and "charset=" in contentType: |
| 84 | + charset = contentType.split("charset=")[1].strip() |
| 85 | + else: |
| 86 | + charset = config.get("encoding", "request") |
| 87 | + icalRequest = unicode(request.content.read(), charset) |
| 88 | + obj = request.getHeader("if-match") |
| 89 | + xmlutils.put(icalRequest, self.calendar, str(request.URLPath()), obj) |
| 90 | + request.setResponseCode(http.CREATED) |
| 91 | + return "" |
| 92 | + |
| 93 | + def render_REPORT(self, request): |
| 94 | + """ |
| 95 | + Manage REPORT requests |
| 96 | + """ |
| 97 | + xmlRequest = request.content.read() |
| 98 | + answer = xmlutils.report(xmlRequest, self.calendar, str(request.URLPath())) |
| 99 | + request.setResponseCode(http.MULTI_STATUS) |
| 100 | + return answer |
| 101 | + |
| 102 | +class UserResource(Resource): |
| 103 | + """ |
| 104 | + Twisted resource for requests at user depth (/user) |
| 105 | + """ |
| 106 | + def __init__(self, user): |
| 107 | + """ |
| 108 | + Initialize resource by connecting children requests to |
| 109 | + the user calendars resources |
| 110 | + """ |
| 111 | + Resource.__init__(self) |
| 112 | + for cal in _calendars: |
| 113 | + if cal.startswith("%s%s"%(user, posixpath.sep)): |
| 114 | + calName = cal.split(posixpath.sep)[1] |
| 115 | + self.putChild(calName, CalendarResource(user, cal)) |
| 116 | + |
| 117 | + def getChild(self, cal, request): |
| 118 | + """ |
| 119 | + Get calendar resource if user exists |
| 120 | + """ |
| 121 | + if cal in _calendars: |
| 122 | + return Resource.getChild(self, cal, request) |
| 123 | + else: |
| 124 | + return self |
| 125 | + |
| 126 | +class HttpResource(Resource): |
| 127 | + """ |
| 128 | + Twisted resource for requests at root depth (/) |
| 129 | + """ |
| 130 | + def __init__(self): |
| 131 | + """ |
| 132 | + Initialize resource by connecting children requests to |
| 133 | + the users resources |
| 134 | + """ |
| 135 | + Resource.__init__(self) |
| 136 | + for user in _users: |
| 137 | + self.putChild(user, UserResource(user)) |
| 138 | + |
| 139 | + def getChild(self, user, request): |
| 140 | + """ |
| 141 | + Get user resource if user exists |
| 142 | + """ |
| 143 | + if user in _users: |
| 144 | + return Resource.getChild(self, user, request) |
| 145 | + else: |
| 146 | + return self |
0 commit comments