mkcalendar.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 socket
  20. from http import client
  21. from radicale import httputils
  22. from radicale import item as radicale_item
  23. from radicale import pathutils, storage, xmlutils
  24. from radicale.log import logger
  25. import posixpath # isort:skip
  26. class ApplicationMkcalendarMixin:
  27. def do_MKCALENDAR(self, environ, base_prefix, path, user):
  28. """Manage MKCALENDAR request."""
  29. if not self.Rights.authorized(user, path, "w"):
  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.Collection.acquire_lock("w", user):
  51. item = next(self.Collection.discover(path), None)
  52. if item:
  53. return self.webdav_error_response(
  54. "D", "resource-must-be-null")
  55. parent_path = pathutils.unstrip_path(
  56. posixpath.dirname(pathutils.strip_path(path)), True)
  57. parent_item = next(self.Collection.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.Collection.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