delete.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. 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_clark("D:multistatus"))
  28. response = ET.Element(xmlutils.make_clark("D:response"))
  29. multistatus.append(response)
  30. href = ET.Element(xmlutils.make_clark("D:href"))
  31. href.text = xmlutils.make_href(base_prefix, path)
  32. response.append(href)
  33. status = ET.Element(xmlutils.make_clark("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. access = app.Access(self._rights, user, path)
  41. if not access.check("w"):
  42. return httputils.NOT_ALLOWED
  43. with self._storage.acquire_lock("w", user):
  44. item = next(self._storage.discover(path), None)
  45. if not item:
  46. return httputils.NOT_FOUND
  47. if not access.check("w", item):
  48. return httputils.NOT_ALLOWED
  49. if_match = environ.get("HTTP_IF_MATCH", "*")
  50. if if_match not in ("*", item.etag):
  51. # ETag precondition not verified, do not delete item
  52. return httputils.PRECONDITION_FAILED
  53. if isinstance(item, storage.BaseCollection):
  54. xml_answer = xml_delete(base_prefix, path, item)
  55. else:
  56. xml_answer = xml_delete(
  57. base_prefix, path, item.collection, item.href)
  58. headers = {"Content-Type": "text/xml; charset=%s" % self._encoding}
  59. return client.OK, headers, self._xml_response(xml_answer)