delete.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 httputils, storage, xmlutils
  22. def xml_delete(base_prefix, path, collection, href=None):
  23. """Read and answer DELETE requests.
  24. Read rfc4918-9.6 for info.
  25. """
  26. collection.delete(href)
  27. multistatus = ET.Element(xmlutils.make_tag("D", "multistatus"))
  28. response = ET.Element(xmlutils.make_tag("D", "response"))
  29. multistatus.append(response)
  30. href = ET.Element(xmlutils.make_tag("D", "href"))
  31. href.text = xmlutils.make_href(base_prefix, path)
  32. response.append(href)
  33. status = ET.Element(xmlutils.make_tag("D", "status"))
  34. status.text = xmlutils.make_response(200)
  35. response.append(status)
  36. return multistatus
  37. class ApplicationDeleteMixin:
  38. def do_DELETE(self, environ, base_prefix, path, user):
  39. """Manage DELETE request."""
  40. if not self.access(user, path, "w"):
  41. return httputils.NOT_ALLOWED
  42. with self.storage.acquire_lock("w", user):
  43. item = next(self.storage.discover(path), None)
  44. if not item:
  45. return httputils.NOT_FOUND
  46. if not self.access(user, path, "w", item):
  47. return httputils.NOT_ALLOWED
  48. if_match = environ.get("HTTP_IF_MATCH", "*")
  49. if if_match not in ("*", item.etag):
  50. # ETag precondition not verified, do not delete item
  51. return httputils.PRECONDITION_FAILED
  52. if isinstance(item, storage.BaseCollection):
  53. xml_answer = xml_delete(base_prefix, path, item)
  54. else:
  55. xml_answer = xml_delete(
  56. base_prefix, path, item.collection, item.href)
  57. headers = {"Content-Type": "text/xml; charset=%s" % self.encoding}
  58. return client.OK, headers, self.write_xml_content(xml_answer)