Extract method prepare

This commit is contained in:
Unrud 2020-01-17 05:00:31 +01:00
parent e2787d8c2a
commit b4230c4249

View File

@ -31,31 +31,11 @@ from radicale import pathutils, storage, xmlutils
from radicale.log import logger from radicale.log import logger
class ApplicationPutMixin: def prepare(vobject_items, path, content_type, permissions, parent_permissions,
def do_PUT(self, environ, base_prefix, path, user): tag=None, write_whole_collection=None):
"""Manage PUT request."""
if not self.access(user, path, "w"):
return httputils.NOT_ALLOWED
try:
content = self.read_content(environ)
except RuntimeError as e:
logger.warning("Bad PUT request on %r: %s", path, e, exc_info=True)
return httputils.BAD_REQUEST
except socket.timeout:
logger.debug("client timed out", exc_info=True)
return httputils.REQUEST_TIMEOUT
# Prepare before locking
parent_path = pathutils.unstrip_path(
posixpath.dirname(pathutils.strip_path(path)), True)
permissions = self._rights.authorized(user, path, "Ww")
parent_permissions = self._rights.authorized(user, parent_path, "w")
def prepare(vobject_items, tag=None, write_whole_collection=None):
if (write_whole_collection or if (write_whole_collection or
permissions and not parent_permissions): permissions and not parent_permissions):
write_whole_collection = True write_whole_collection = True
content_type = environ.get("CONTENT_TYPE",
"").split(";")[0]
tags = {value: key tags = {value: key
for key, value in xmlutils.MIMETYPES.items()} for key, value in xmlutils.MIMETYPES.items()}
tag = radicale_item.predict_tag_of_whole_collection( tag = radicale_item.predict_tag_of_whole_collection(
@ -140,6 +120,26 @@ class ApplicationPutMixin:
return (items_generator(), tag, write_whole_collection, props, return (items_generator(), tag, write_whole_collection, props,
stored_exc_info) stored_exc_info)
class ApplicationPutMixin:
def do_PUT(self, environ, base_prefix, path, user):
"""Manage PUT request."""
if not self.access(user, path, "w"):
return httputils.NOT_ALLOWED
try:
content = self.read_content(environ)
except RuntimeError as e:
logger.warning("Bad PUT request on %r: %s", path, e, exc_info=True)
return httputils.BAD_REQUEST
except socket.timeout:
logger.debug("client timed out", exc_info=True)
return httputils.REQUEST_TIMEOUT
# Prepare before locking
content_type = environ.get("CONTENT_TYPE", "").split(";")[0]
parent_path = pathutils.unstrip_path(
posixpath.dirname(pathutils.strip_path(path)), True)
permissions = self._rights.authorized(user, path, "Ww")
parent_permissions = self._rights.authorized(user, parent_path, "w")
try: try:
vobject_items = tuple(vobject.readComponents(content or "")) vobject_items = tuple(vobject.readComponents(content or ""))
except Exception as e: except Exception as e:
@ -147,7 +147,9 @@ class ApplicationPutMixin:
"Bad PUT request on %r: %s", path, e, exc_info=True) "Bad PUT request on %r: %s", path, e, exc_info=True)
return httputils.BAD_REQUEST return httputils.BAD_REQUEST
(prepared_items, prepared_tag, prepared_write_whole_collection, (prepared_items, prepared_tag, prepared_write_whole_collection,
prepared_props, prepared_exc_info) = prepare(vobject_items) prepared_props, prepared_exc_info) = prepare(
vobject_items, path, content_type, permissions,
parent_permissions)
with self._storage.acquire_lock("w", user): with self._storage.acquire_lock("w", user):
item = next(self._storage.discover(path), None) item = next(self._storage.discover(path), None)
@ -188,7 +190,8 @@ class ApplicationPutMixin:
prepared_write_whole_collection != write_whole_collection): prepared_write_whole_collection != write_whole_collection):
(prepared_items, prepared_tag, prepared_write_whole_collection, (prepared_items, prepared_tag, prepared_write_whole_collection,
prepared_props, prepared_exc_info) = prepare( prepared_props, prepared_exc_info) = prepare(
vobject_items, tag, write_whole_collection) vobject_items, path, content_type, permissions,
parent_permissions, tag, write_whole_collection)
props = prepared_props props = prepared_props
if prepared_exc_info: if prepared_exc_info:
logger.warning( logger.warning(