delete.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. hook_notification_item_list = []
  55. if isinstance(item, storage.BaseCollection):
  56. for i in item.get_all():
  57. hook_notification_item_list.append(
  58. HookNotificationItem(
  59. HookNotificationItemTypes.DELETE,
  60. access.path,
  61. i.uid
  62. )
  63. )
  64. xml_answer = xml_delete(base_prefix, path, item)
  65. else:
  66. hook_notification_item_list.append(
  67. HookNotificationItem(
  68. HookNotificationItemTypes.DELETE,
  69. access.path,
  70. item.uid
  71. )
  72. )
  73. xml_answer = xml_delete(
  74. base_prefix, path, item.collection, item.href)
  75. for i in hook_notification_item_list:
  76. self._hook.notify(i)
  77. headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
  78. return client.OK, headers, self._write_xml_content(xml_answer)