mkcalendar.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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-2021 Unrud <unrud@outlook.com>
  6. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. import posixpath
  21. import socket
  22. from http import client
  23. import radicale.item as radicale_item
  24. from radicale import httputils, pathutils, storage, types, xmlutils
  25. from radicale.app.base import ApplicationBase
  26. from radicale.log import logger
  27. class ApplicationPartMkcalendar(ApplicationBase):
  28. def do_MKCALENDAR(self, environ: types.WSGIEnviron, base_prefix: str,
  29. path: str, user: str) -> types.WSGIResponse:
  30. """Manage MKCALENDAR request."""
  31. if "w" not in self._rights.authorization(user, path):
  32. return httputils.NOT_ALLOWED
  33. try:
  34. xml_content = self._read_xml_request_body(environ)
  35. except RuntimeError as e:
  36. logger.warning(
  37. "Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
  38. return httputils.BAD_REQUEST
  39. except socket.timeout:
  40. logger.debug("Client timed out", exc_info=True)
  41. return httputils.REQUEST_TIMEOUT
  42. # Prepare before locking
  43. props_with_remove = xmlutils.props_from_request(xml_content)
  44. props_with_remove["tag"] = "VCALENDAR"
  45. try:
  46. props = radicale_item.check_and_sanitize_props(props_with_remove)
  47. except ValueError as e:
  48. logger.warning(
  49. "Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
  50. return httputils.BAD_REQUEST
  51. # TODO: use this?
  52. # timezone = props.get("C:calendar-timezone")
  53. with self._storage.acquire_lock("w", user):
  54. item = next(iter(self._storage.discover(path)), None)
  55. if item:
  56. return self._webdav_error_response(
  57. client.CONFLICT, "D:resource-must-be-null")
  58. parent_path = pathutils.unstrip_path(
  59. posixpath.dirname(pathutils.strip_path(path)), True)
  60. parent_item = next(iter(self._storage.discover(parent_path)), None)
  61. if not parent_item:
  62. return httputils.CONFLICT
  63. if (not isinstance(parent_item, storage.BaseCollection) or
  64. parent_item.tag):
  65. return httputils.FORBIDDEN
  66. try:
  67. self._storage.create_collection(path, props=props)
  68. except ValueError as e:
  69. logger.warning(
  70. "Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
  71. return httputils.BAD_REQUEST
  72. return client.CREATED, {}, None