Skip to content

Commit

Permalink
use bmanager to get file everywhere to make sure annotated fields are…
Browse files Browse the repository at this point in the history
… available
  • Loading branch information
tykling committed Nov 17, 2024
1 parent 28f54ca commit 6433a9b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 12 additions & 10 deletions src/files/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def upload(request: HttpRequest, f: UploadedFile, metadata: UploadRequestSchema)
# create jobs
uploaded_file.create_jobs()

# get file using the manager
uploaded_file = BaseFile.bmanager.get(uuid=uploaded_file.uuid)
# all good
return 201, {"bma_response": uploaded_file, "message": f"File {uploaded_file.uuid} uploaded OK!"}

Expand Down Expand Up @@ -238,7 +240,7 @@ def api_file_action(
return 202, {"message": "OK"}
updated = getattr(db_files, action)()
logger.debug(f"{action} {updated} OK")
db_files = BaseFile.objects.filter(
db_files = BaseFile.bmanager.filter(
uuid__in=db_uuids,
)
if single:
Expand Down Expand Up @@ -437,7 +439,7 @@ def unpublish_files(
)
def file_get(request: HttpRequest, file_uuid: uuid.UUID) -> FileApiResponseType:
"""Return a file object."""
basefile = get_object_or_404(BaseFile, uuid=file_uuid)
basefile = get_object_or_404(BaseFile.bmanager.all(), uuid=file_uuid)
if basefile.permitted(user=request.user):
return 200, {"bma_response": basefile}
return 403, {"message": "Permission denied."}
Expand Down Expand Up @@ -475,7 +477,7 @@ def file_update(
check: bool = False,
) -> FileApiResponseType:
"""Update (PATCH) or replace (PUT) a file metadata object."""
basefile = get_object_or_404(BaseFile, uuid=file_uuid)
basefile = get_object_or_404(BaseFile.bmanager.all(), uuid=file_uuid)
if not request.user.has_perm("change_basefile", basefile):
return 403, {"message": "Permission denied."}
if check:
Expand Down Expand Up @@ -521,7 +523,7 @@ def file_delete(
request: HttpRequest, file_uuid: uuid.UUID, *, check: bool = False
) -> tuple[int, dict[str, str] | None]:
"""Mark a file for deletion."""
basefile = get_object_or_404(BaseFile, uuid=file_uuid)
basefile = get_object_or_404(BaseFile.bmanager.all(), uuid=file_uuid)
if not request.user.has_perm("softdelete_basefile", basefile):
return 403, {"message": "Permission denied."}
if check:
Expand All @@ -548,8 +550,8 @@ def file_tag(
) -> tuple[int, dict[str, models.QuerySet[BmaTag] | str]]:
"""API endpoint for tagging a file."""
# make sure the tagging user has permissions to see the file
basefile = get_object_or_404(BaseFile, uuid=file_uuid)
if not basefile.permitted: # type: ignore[truthy-function]
basefile = get_object_or_404(BaseFile.bmanager.all(), uuid=file_uuid)
if not basefile.permitted:
return 403, {"message": "Missing file permissions"}

# make sure the tagging user is in the curators group
Expand Down Expand Up @@ -577,8 +579,8 @@ def file_untag(
) -> tuple[int, dict[str, models.QuerySet[BmaTag] | str]]:
"""API endpoint for untagging a file."""
# make sure the untagging user has permissions to see the file
basefile = get_object_or_404(BaseFile, uuid=file_uuid)
if not basefile.permitted: # type: ignore[truthy-function]
basefile = get_object_or_404(BaseFile.bmanager.all(), uuid=file_uuid)
if not basefile.permitted:
return 403, {"message": "Missing file permissions"}

# make sure the tagging user is in the curators group
Expand Down Expand Up @@ -614,7 +616,7 @@ def file_thumbnail(
) -> FileApiResponseType:
"""Endpoint for uploading the source image for thumbnails."""
# make sure the thumbnailing user has permissions to change the file
basefile = get_object_or_404(BaseFile, uuid=file_uuid)
basefile = get_object_or_404(BaseFile.bmanager.all(), uuid=file_uuid)
if not request.user.has_perm("change_basefile", basefile):
return 403, {"message": "Permission denied."}
if check:
Expand Down Expand Up @@ -648,7 +650,7 @@ def file_thumbnail_delete(
request: HttpRequest, file_uuid: uuid.UUID, *, check: bool = False
) -> tuple[int, dict[str, str] | None]:
"""Delete a thumbnail."""
basefile = get_object_or_404(BaseFile, uuid=file_uuid)
basefile = get_object_or_404(BaseFile.bmanager.all(), uuid=file_uuid)
if not request.user.has_perm("change_basefile", basefile):
return 403, {"message": "Permission denied."}
if check:
Expand Down
4 changes: 2 additions & 2 deletions src/files/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class FileResponseSchema(ModelSchema):
license_name: str
license_url: str
tags: list[TagResponseSchema]
jobs_unfinished: list[uuid.UUID]
jobs_finished: list[uuid.UUID]
jobs_unfinished: int
jobs_finished: int
has_thumbnail: bool
# move to seperate ImageResponseSchema pls
exif: dict[str, dict[str, str]] | None = None
Expand Down

0 comments on commit 6433a9b

Please sign in to comment.