mkcalendar.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. import posixpath
  20. import socket
  21. from http import client
  22. from radicale import httputils
  23. from radicale import item as radicale_item
  24. from radicale import pathutils, storage, xmlutils
  25. from radicale.log import logger
  26. class ApplicationMkcalendarMixin:
  27. def do_MKCALENDAR(self, environ, base_prefix, path, user):
  28. """Manage MKCALENDAR request."""
  29. if "w" not in self._rights.authorization(user, path):
  30. return httputils.NOT_ALLOWED
  31. try:
  32. xml_content = self._read_xml_content(environ)
  33. except RuntimeError as e:
  34. logger.warning(
  35. "Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
  36. return httputils.BAD_REQUEST
  37. except socket.timeout:
  38. logger.debug("client timed out", exc_info=True)
  39. return httputils.REQUEST_TIMEOUT
  40. # Prepare before locking
  41. props = xmlutils.props_from_request(xml_content)
  42. props["tag"] = "VCALENDAR"
  43. # TODO: use this?
  44. # timezone = props.get("C:calendar-timezone")
  45. try:
  46. radicale_item.check_and_sanitize_props(props)
  47. except ValueError as e:
  48. logger.warning(
  49. "Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
  50. with self._storage.acquire_lock("w", user):
  51. item = next(self._storage.discover(path), None)
  52. if item:
  53. return self._webdav_error_response(
  54. client.CONFLICT, "D:resource-must-be-null")
  55. parent_path = pathutils.unstrip_path(
  56. posixpath.dirname(pathutils.strip_path(path)), True)
  57. parent_item = next(self._storage.discover(parent_path), None)
  58. if not parent_item:
  59. return httputils.CONFLICT
  60. if (not isinstance(parent_item, storage.BaseCollection) or
  61. parent_item.get_meta("tag")):
  62. return httputils.FORBIDDEN
  63. try:
  64. self._storage.create_collection(path, props=props)
  65. except ValueError as e:
  66. logger.warning(
  67. "Bad MKCALENDAR request on %r: %s", path, e, exc_info=True)
  68. return httputils.BAD_REQUEST
  69. return client.CREATED, {}, None