xmlutils.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 errors (see __init__)
  27. # TODO: Manage depth and calendars/collections (see main)
  28. import xml.etree.ElementTree as ET
  29. from radicale import config, ical
  30. # TODO: This is a well-known and accepted hack for ET to avoid ET from renaming
  31. # namespaces, which is accepted in XML norm but often not in XML
  32. # readers. Is there another clean solution to force namespaces?
  33. for key,value in config.items("namespace"):
  34. ET._namespace_map[value] = key
  35. def _tag(short_name, local):
  36. """Get XML Clark notation {uri(``short_name``)}``local``."""
  37. return "{%s}%s"%(config.get("namespace", short_name), local)
  38. def delete(obj, calendar, url):
  39. """Read and answer DELETE requests.
  40. Read rfc4918-9.6 for info.
  41. """
  42. # Reading request
  43. calendar.remove(obj)
  44. # Writing answer
  45. multistatus = ET.Element(_tag("D", "multistatus"))
  46. response = ET.Element(_tag("D", "response"))
  47. multistatus.append(response)
  48. href = ET.Element(_tag("D", "href"))
  49. href.text = url
  50. response.append(href)
  51. status = ET.Element(_tag("D", "status"))
  52. status.text = config.get("status", "200")
  53. response.append(status)
  54. return ET.tostring(multistatus, config.get("encoding", "request"))
  55. def propfind(xml_request, calendar, url):
  56. """Read and answer PROPFIND requests.
  57. Read rfc4918-9.1 for info.
  58. """
  59. # Reading request
  60. root = ET.fromstring(xml_request)
  61. propElement = root.find(_tag("D", "prop"))
  62. propList = propElement.getchildren()
  63. properties = [property.tag for property in propList]
  64. # Writing answer
  65. multistatus = ET.Element(_tag("D", "multistatus"))
  66. response = ET.Element(_tag("D", "response"))
  67. multistatus.append(response)
  68. href = ET.Element(_tag("D", "href"))
  69. href.text = url
  70. response.append(href)
  71. propstat = ET.Element(_tag("D", "propstat"))
  72. response.append(propstat)
  73. prop = ET.Element(_tag("D", "prop"))
  74. propstat.append(prop)
  75. if _tag("D", "resourcetype") in properties:
  76. resourcetype = ET.Element(_tag("D", "resourcetype"))
  77. resourcetype.append(ET.Element(_tag("C", "calendar")))
  78. prop.append(resourcetype)
  79. if _tag("D", "owner") in properties:
  80. owner = ET.Element(_tag("D", "owner"))
  81. owner.text = calendar.owner
  82. prop.append(owner)
  83. if _tag("D", "getcontenttype") in properties:
  84. getcontenttype = ET.Element(_tag("D", "getcontenttype"))
  85. getcontenttype.text = "text/calendar"
  86. prop.append(getcontenttype)
  87. if _tag("D", "getetag") in properties:
  88. getetag = ET.Element(_tag("D", "getetag"))
  89. getetag.text = calendar.etag()
  90. prop.append(getetag)
  91. if _tag("CS", "getctag") in properties:
  92. getctag = ET.Element(_tag("CS", "getctag"))
  93. getctag.text = calendar.ctag
  94. prop.append(getctag)
  95. status = ET.Element(_tag("D", "status"))
  96. status.text = config.get("status", "200")
  97. propstat.append(status)
  98. return ET.tostring(multistatus, config.get("encoding", "request"))
  99. def put(icalRequest, calendar, url, obj):
  100. """Read PUT requests."""
  101. if obj:
  102. # PUT is modifying obj
  103. calendar.replace(obj, icalRequest)
  104. else:
  105. # PUT is adding a new object
  106. calendar.append(icalRequest)
  107. def report(xml_request, calendar, url):
  108. """Read and answer REPORT requests.
  109. Read rfc3253-3.6 for info.
  110. """
  111. # Reading request
  112. root = ET.fromstring(xml_request)
  113. propElement = root.find(_tag("D", "prop"))
  114. propList = propElement.getchildren()
  115. properties = [property.tag for property in propList]
  116. filters = {}
  117. filterElement = root.find(_tag("C", "filter"))
  118. filterList = propElement.getchildren()
  119. # TODO: This should be recursive
  120. # TODO: Really manage filters (see ical)
  121. for filter in filterList:
  122. sub = filters[filter.get("name")] = {}
  123. for subfilter in filter.getchildren():
  124. sub[subfilter.get("name")] = {}
  125. if root.tag == _tag("C", "calendar-multiget"):
  126. # Read rfc4791-7.9 for info
  127. hreferences = set([hrefElement.text for hrefElement in root.findall(_tag("D", "href"))])
  128. else:
  129. hreferences = [url]
  130. # Writing answer
  131. multistatus = ET.Element(_tag("D", "multistatus"))
  132. # TODO: WTF, sunbird needs one response by object,
  133. # is that really what is needed?
  134. # Read rfc4791-9.[6|10] for info
  135. for hreference in hreferences:
  136. headers = ical.headers(calendar.vcalendar())
  137. # TODO: Define timezones by obj
  138. timezones = ical.timezones(calendar.vcalendar())
  139. objects = []
  140. objects.extend(ical.events(calendar.vcalendar()))
  141. objects.extend(ical.todos(calendar.vcalendar()))
  142. if not objects:
  143. # TODO: Read rfc4791-9.[6|10] to find a right answer
  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. status = ET.Element(_tag("D", "status"))
  150. status.text = config.get("status", "204")
  151. response.append(status)
  152. for obj in objects:
  153. # TODO: Use the hreference to read data and create href.text
  154. # We assume here that hreference is url
  155. response = ET.Element(_tag("D", "response"))
  156. multistatus.append(response)
  157. href = ET.Element(_tag("D", "href"))
  158. href.text = url
  159. response.append(href)
  160. propstat = ET.Element(_tag("D", "propstat"))
  161. response.append(propstat)
  162. prop = ET.Element(_tag("D", "prop"))
  163. propstat.append(prop)
  164. if _tag("D", "getetag") in properties:
  165. # TODO: Can UID and ETAG be the same?
  166. getetag = ET.Element(_tag("D", "getetag"))
  167. getetag.text = obj.etag()
  168. prop.append(getetag)
  169. if _tag("C", "calendar-data") in properties:
  170. cdata = ET.Element(_tag("C", "calendar-data"))
  171. # TODO: Maybe assume that events and todos are not the same
  172. cdata.text = ical.write_calendar(headers, timezones, [obj])
  173. prop.append(cdata)
  174. status = ET.Element(_tag("D", "status"))
  175. status.text = config.get("status", "200")
  176. propstat.append(status)
  177. return ET.tostring(multistatus, config.get("encoding", "request"))