mkcalendar.py 3.1 KB

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