Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Django admin command for reupload #686

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion core_api/src/routes/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ def delete_file(file_uuid: UUID, user_uuid: Annotated[UUID, Depends(get_user_uui
if file.creator_user_uuid != user_uuid:
return file_not_found_response(file_uuid=file_uuid)

s3.delete_object(Bucket=env.bucket_name, Key=file.key)
storage_handler.delete_item(file)

chunks = storage_handler.get_file_chunks(file.uuid, user_uuid)
Expand Down
2 changes: 0 additions & 2 deletions core_api/tests/routes/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ def test_delete_file(s3_client, app_client, elasticsearch_storage_handler, chunk
elasticsearch_storage_handler.refresh()

# check assets dont exist
with pytest.raises(s3_client.exceptions.NoSuchKey):
s3_client.get_object(Bucket=env.bucket_name, Key=chunked_file.key)

with pytest.raises(NotFoundError):
elasticsearch_storage_handler.read_item(item_uuid=chunked_file.uuid, model_type="file")
Expand Down
35 changes: 35 additions & 0 deletions django_app/redbox_app/redbox_core/admin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import csv
import logging

from django.conf import settings
from django.contrib import admin
from django.http import HttpResponse
from import_export.admin import ImportMixin
from requests.exceptions import RequestException

from redbox_app.redbox_core.client import CoreApiClient

from . import models

logger = logging.getLogger(__name__)
core_api = CoreApiClient(host=settings.CORE_API_HOST, port=settings.CORE_API_PORT)


class UserAdmin(ImportMixin, admin.ModelAdmin):
fields = ["email", "business_unit", "grade", "profession", "is_superuser", "is_staff", "last_login"]
Expand All @@ -30,9 +38,36 @@ class Meta:


class FileAdmin(admin.ModelAdmin):
def reupload(self, request, queryset): # noqa:ARG002
for file in queryset:
try:
logger.info("Deleting existing file from core-api: %s", file)
core_api.delete_file(file.core_file_uuid, file.user)
except RequestException as e:
logger.exception("Error deleting File model object %s.", file, exc_info=e)

else:
file.status = models.StatusEnum.deleted
file.save()

try:
logger.info("Re-uploading file to core-api: %s", file)
upload_file_response = core_api.upload_file(file.unique_name, file.user)
except RequestException as e:
logger.exception("Error re-uploading File model object %s.", file, exc_info=e)
file.status = models.StatusEnum.errored
file.save()
else:
file.core_file_uuid = upload_file_response.uuid
file.status = models.StatusEnum.uploaded
file.save()

logger.info("Successfully reuploaded file %s.", file)

list_display = ["original_file_name", "user", "status", "created_at", "last_referenced"]
list_filter = ["user", "status"]
date_hierarchy = "created_at"
actions = ["reupload"]


class CitationInline(admin.StackedInline):
Expand Down
Loading