1
0

delete.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. def xml_delete(base_prefix: str, path: str, collection: storage.BaseCollection,
  25. item_href: Optional[str] = None) -> ET.Element:
  26. """Read and answer DELETE requests.
  27. Read rfc4918-9.6 for info.
  28. """
  29. collection.delete(item_href)
  30. multistatus = ET.Element(xmlutils.make_clark("D:multistatus"))
  31. response = ET.Element(xmlutils.make_clark("D:response"))
  32. multistatus.append(response)
  33. href_element = ET.Element(xmlutils.make_clark("D:href"))
  34. href_element.text = xmlutils.make_href(base_prefix, path)
  35. response.append(href_element)
  36. status = ET.Element(xmlutils.make_clark("D:status"))
  37. status.text = xmlutils.make_response(200)
  38. response.append(status)
  39. return multistatus
  40. class ApplicationPartDelete(ApplicationBase):
  41. def do_DELETE(self, environ: types.WSGIEnviron, base_prefix: str,
  42. path: str, user: str) -> types.WSGIResponse:
  43. """Manage DELETE request."""
  44. access = Access(self._rights, user, path)
  45. if not access.check("w"):
  46. return httputils.NOT_ALLOWED
  47. with self._storage.acquire_lock("w", user):
  48. item = next(iter(self._storage.discover(path)), None)
  49. if not item:
  50. return httputils.NOT_FOUND
  51. if not access.check("w", item):
  52. return httputils.NOT_ALLOWED
  53. if_match = environ.get("HTTP_IF_MATCH", "*")
  54. if if_match not in ("*", item.etag):
  55. # ETag precondition not verified, do not delete item
  56. return httputils.PRECONDITION_FAILED
  57. if isinstance(item, storage.BaseCollection):
  58. xml_answer = xml_delete(base_prefix, path, item)
  59. else:
  60. assert item.collection is not None
  61. assert item.href is not None
  62. xml_answer = xml_delete(
  63. base_prefix, path, item.collection, item.href)
  64. headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
  65. return client.OK, headers, self._xml_response(xml_answer)