delete.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. from http import client
  20. from xml.etree import ElementTree as ET
  21. from radicale import app, httputils, storage, xmlutils
  22. from radicale.hook import HookNotificationItem, HookNotificationItemTypes
  23. def xml_delete(base_prefix, path, collection, href=None):
  24. """Read and answer DELETE requests.
  25. Read rfc4918-9.6 for info.
  26. """
  27. collection.delete(href)
  28. multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
  29. response = ET.Element(xmlutils.make_clark("D:response"))
  30. multistatus.append(response)
  31. href = ET.Element(xmlutils.make_clark("D:href"))
  32. href.text = xmlutils.make_href(base_prefix, path)
  33. response.append(href)
  34. status = ET.Element(xmlutils.make_clark("D:status"))
  35. status.text = xmlutils.make_response(200)
  36. response.append(status)
  37. return multistatus
  38. class ApplicationDeleteMixin:
  39. def do_DELETE(self, environ, base_prefix, path, user):
  40. """Manage DELETE request."""
  41. access = app.Access(self._rights, user, path)
  42. if not access.check("w"):
  43. return httputils.NOT_ALLOWED
  44. with self._storage.acquire_lock("w", user):
  45. item = next(self._storage.discover(path), None)
  46. if not item:
  47. return httputils.NOT_FOUND
  48. if not access.check("w", item):
  49. return httputils.NOT_ALLOWED
  50. if_match = environ.get("HTTP_IF_MATCH", "*")
  51. if if_match not in ("*", item.etag):
  52. # ETag precondition not verified, do not delete item
  53. return httputils.PRECONDITION_FAILED
  54. if isinstance(item, storage.BaseCollection):
  55. xml_answer = xml_delete(base_prefix, path, item)
  56. for item in item.get_all():
  57. hook_notification_item = HookNotificationItem(
  58. HookNotificationItemTypes.DELETE, item.uid)
  59. self._hook.notify(hook_notification_item)
  60. else:
  61. xml_answer = xml_delete(
  62. base_prefix, path, item.collection, item.href)
  63. hook_notification_item = HookNotificationItem(
  64. HookNotificationItemTypes.DELETE, item.uid)
  65. self._hook.notify(hook_notification_item)
  66. headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
  67. return client.OK, headers, self._write_xml_content(xml_answer)