Skip to content

Commit

Permalink
Raise storage exceptions outside of threadpool excecution
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic committed Dec 13, 2024
1 parent 50f131d commit f3af123
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions cms/private_media/bulk_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,26 @@ def bulk_set_file_permissions(files: Iterable["FieldFile"], privacy: Privacy) ->
"""
results: dict[FieldFile, bool] = {}

for file in files:
if privacy is Privacy.PUBLIC and not hasattr(file.storage, "make_public"):
raise ImproperlyConfigured(
f"{file.storage.__class__.__name__} does not implement make_public(), "
"which is a requirement for bulk-setting of file permissions."
)
if privacy is Privacy.PRIVATE and not hasattr(file.storage, "make_private"):
raise ImproperlyConfigured(
f"{file.storage.__class__.__name__} does not implement make_private(), "
"which is a requirement for bulk-setting of file permissions."
)

def set_file_permission_and_report(file: "FieldFile") -> None:
storage = file.storage
handler: Callable[[FieldFile], bool] | None
if privacy is Privacy.PRIVATE:
handler = getattr(storage, "make_private", None)
elif privacy is Privacy.PUBLIC:
handler = getattr(storage, "make_public", None)

if handler is None:
raise ImproperlyConfigured(
"%s does not implement the make_private() or make_public() methods, which is a requirement "
"for bulk-setting file permissions.",
storage.__class__.__name__,
)
else:
results[file] = handler(file)
results[file] = handler(file)

with concurrent.futures.ThreadPoolExecutor(
max_workers=int(settings.PRIVATE_MEDIA_BULK_UPDATE_MAX_WORKERS)
Expand Down

0 comments on commit f3af123

Please sign in to comment.