xmlutils.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2008-2010 Guillaume Ayoub
  5. # Copyright © 2008 Nicolas Kandel
  6. # Copyright © 2008 Pascal Halter
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. """
  21. XML and iCal requests manager.
  22. Note that all these functions need to receive unicode objects for full
  23. iCal requests (PUT) and string objects with charset correctly defined
  24. in them for XML requests (all but PUT).
  25. """
  26. # TODO: Manage depth and calendars/collections
  27. import xml.etree.ElementTree as ET
  28. from radicale import client, config, ical
  29. NAMESPACES = {
  30. "C": "urn:ietf:params:xml:ns:caldav",
  31. "D": "DAV:",
  32. "CS": "http://calendarserver.org/ns/"}
  33. def _tag(short_name, local):
  34. """Get XML Clark notation {uri(``short_name``)}``local``."""
  35. return "{%s}%s" % (NAMESPACES[short_name], local)
  36. def _response(code):
  37. """Return full W3C names from HTTP status codes."""
  38. return "HTTP/1.1 %i %s" % (code, client.responses[code])
  39. def delete(obj, calendar, url):
  40. """Read and answer DELETE requests.
  41. Read rfc4918-9.6 for info.
  42. """
  43. # Reading request
  44. calendar.remove(obj)
  45. # Writing answer
  46. multistatus = ET.Element(_tag("D", "multistatus"))
  47. response = ET.Element(_tag("D", "response"))
  48. multistatus.append(response)
  49. href = ET.Element(_tag("D", "href"))
  50. href.text = url
  51. response.append(href)
  52. status = ET.Element(_tag("D", "status"))
  53. status.text = _response(200)
  54. response.append(status)
  55. return ET.tostring(multistatus, config.get("encoding", "request"))
  56. def propfind(xml_request, calendar, url):
  57. """Read and answer PROPFIND requests.
  58. Read rfc4918-9.1 for info.
  59. """
  60. # Reading request
  61. root = ET.fromstring(xml_request)
  62. prop_element = root.find(_tag("D", "prop"))
  63. prop_list = prop_element.getchildren()
  64. props = [prop.tag for prop in prop_list]
  65. # Writing answer
  66. multistatus = ET.Element(_tag("D", "multistatus"))
  67. response = ET.Element(_tag("D", "response"))
  68. multistatus.append(response)
  69. href = ET.Element(_tag("D", "href"))
  70. href.text = url
  71. response.append(href)
  72. propstat = ET.Element(_tag("D", "propstat"))
  73. response.append(propstat)
  74. prop = ET.Element(_tag("D", "prop"))
  75. propstat.append(prop)
  76. if _tag("D", "resourcetype") in props:
  77. element = ET.Element(_tag("D", "resourcetype"))
  78. element.append(ET.Element(_tag("C", "calendar")))
  79. prop.append(element)
  80. if _tag("D", "owner") in props:
  81. element = ET.Element(_tag("D", "owner"))
  82. element.text = calendar.owner
  83. prop.append(element)
  84. if _tag("D", "getcontenttype") in props:
  85. element = ET.Element(_tag("D", "getcontenttype"))
  86. element.text = "text/calendar"
  87. prop.append(element)
  88. if _tag("D", "getetag") in props:
  89. element = ET.Element(_tag("D", "getetag"))
  90. element.text = calendar.etag
  91. prop.append(element)
  92. if _tag("CS", "getctag") in props:
  93. element = ET.Element(_tag("CS", "getctag"))
  94. element.text = calendar.ctag
  95. prop.append(element)
  96. status = ET.Element(_tag("D", "status"))
  97. status.text = _response(200)
  98. propstat.append(status)
  99. return ET.tostring(multistatus, config.get("encoding", "request"))
  100. def put(ical_request, calendar, url, obj):
  101. """Read PUT requests."""
  102. # TODO: use url to set hreference
  103. if obj:
  104. # PUT is modifying obj
  105. calendar.replace(obj, ical_request)
  106. else:
  107. # PUT is adding a new object
  108. calendar.append(ical_request)
  109. def report(xml_request, calendar, url):
  110. """Read and answer REPORT requests.
  111. Read rfc3253-3.6 for info.
  112. """
  113. # Reading request
  114. root = ET.fromstring(xml_request)
  115. prop_element = root.find(_tag("D", "prop"))
  116. prop_list = prop_element.getchildren()
  117. props = [prop.tag for prop in prop_list]
  118. if root.tag == _tag("C", "calendar-multiget"):
  119. # Read rfc4791-7.9 for info
  120. hreferences = set([href_element.text for href_element
  121. in root.findall(_tag("D", "href"))])
  122. else:
  123. hreferences = [url]
  124. # Writing answer
  125. multistatus = ET.Element(_tag("D", "multistatus"))
  126. # TODO: WTF, sunbird needs one response by object,
  127. # is that really what is needed?
  128. # Read rfc4791-9.[6|10] for info
  129. for hreference in hreferences:
  130. objects = calendar.events + calendar.todos
  131. if not objects:
  132. # TODO: Read rfc4791-9.[6|10] to find a right answer
  133. response = ET.Element(_tag("D", "response"))
  134. multistatus.append(response)
  135. href = ET.Element(_tag("D", "href"))
  136. href.text = url
  137. response.append(href)
  138. status = ET.Element(_tag("D", "status"))
  139. status.text = _response(204)
  140. response.append(status)
  141. for obj in objects:
  142. # TODO: Use the hreference to read data and create href.text
  143. # We assume here that hreference is url
  144. response = ET.Element(_tag("D", "response"))
  145. multistatus.append(response)
  146. href = ET.Element(_tag("D", "href"))
  147. href.text = url
  148. response.append(href)
  149. propstat = ET.Element(_tag("D", "propstat"))
  150. response.append(propstat)
  151. prop = ET.Element(_tag("D", "prop"))
  152. propstat.append(prop)
  153. if _tag("D", "getetag") in props:
  154. element = ET.Element(_tag("D", "getetag"))
  155. element.text = obj.etag
  156. prop.append(element)
  157. if _tag("C", "calendar-data") in props:
  158. element = ET.Element(_tag("C", "calendar-data"))
  159. if isinstance(obj, ical.Event):
  160. element.text = ical.serialize(
  161. calendar.headers, calendar.timezones, events=[obj])
  162. elif isinstance(obj, ical.Todo):
  163. element.text = ical.serialize(
  164. calendar.headers, calendar.timezones, todos=[obj])
  165. prop.append(element)
  166. status = ET.Element(_tag("D", "status"))
  167. status.text = _response(200)
  168. propstat.append(status)
  169. return ET.tostring(multistatus, config.get("encoding", "request"))