delete.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # This file is part of Radicale - CalDAV and CardDAV 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. import xml.etree.ElementTree as ET
  20. from http import client
  21. from typing import Optional
  22. from radicale import httputils, storage, types, xmlutils
  23. from radicale.app.base import Access, ApplicationBase
  24. from radicale.hook import HookNotificationItem, HookNotificationItemTypes
  25. def xml_delete(base_prefix: str, path: str, collection: storage.BaseCollection,
  26. item_href: Optional[str] = None) -> ET.Element:
  27. """Read and answer DELETE requests.
  28. Read rfc4918-9.6 for info.
  29. """
  30. collection.delete(item_href)
  31. multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
  32. response = ET.Element(xmlutils.make_clark("D:response"))
  33. multistatus.append(response)
  34. href_element = ET.Element(xmlutils.make_clark("D:href"))
  35. href_element.text = xmlutils.make_href(base_prefix, path)
  36. response.append(href_element)
  37. status = ET.Element(xmlutils.make_clark("D:status"))
  38. status.text = xmlutils.make_response(200)
  39. response.append(status)
  40. return multistatus
  41. class ApplicationPartDelete(ApplicationBase):
  42. def do_DELETE(self, environ: types.WSGIEnviron, base_prefix: str,
  43. path: str, user: str) -> types.WSGIResponse:
  44. """Manage DELETE request."""
  45. access = Access(self._rights, user, path)
  46. if not access.check("w"):
  47. return httputils.NOT_ALLOWED
  48. with self._storage.acquire_lock("w", user):
  49. item = next(iter(self._storage.discover(path)), None)
  50. if not item:
  51. return httputils.NOT_FOUND
  52. if not access.check("w", item):
  53. return httputils.NOT_ALLOWED
  54. if_match = environ.get("HTTP_IF_MATCH", "*")
  55. if if_match not in ("*", item.etag):
  56. # ETag precondition not verified, do not delete item
  57. return httputils.PRECONDITION_FAILED
  58. hook_notification_item_list = []
  59. if isinstance(item, storage.BaseCollection):
  60. if self._permit_delete_collection:
  61. for i in item.get_all():
  62. hook_notification_item_list.append(
  63. HookNotificationItem(
  64. HookNotificationItemTypes.DELETE,
  65. access.path,
  66. i.uid
  67. )
  68. )
  69. xml_answer = xml_delete(base_prefix, path, item)
  70. else:
  71. return httputils.NOT_ALLOWED
  72. else:
  73. assert item.collection is not None
  74. assert item.href is not None
  75. hook_notification_item_list.append(
  76. HookNotificationItem(
  77. HookNotificationItemTypes.DELETE,
  78. access.path,
  79. item.uid
  80. )
  81. )
  82. xml_answer = xml_delete(
  83. base_prefix, path, item.collection, item.href)
  84. for notification_item in hook_notification_item_list:
  85. self._hook.notify(notification_item)
  86. headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
  87. return client.OK, headers, self._xml_response(xml_answer)