From f35546b9fc96944f6b54ff1b702b27a6a3a50dca Mon Sep 17 00:00:00 2001 From: nickmwangemi Date: Tue, 23 Jan 2024 12:35:34 +0300 Subject: [PATCH] Ingest images --- peachjam/adapters/adapters.py | 4 ++- peachjam/admin.py | 2 +- peachjam/migrations/0117_documentmedia.py | 12 ++++++--- peachjam/models/core_document_model.py | 5 +++- peachjam/urls.py | 7 +++++ peachjam/views/__init__.py | 1 + peachjam/views/attachment_media.py | 33 +++++++++++++++++++++++ 7 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 peachjam/views/attachment_media.py diff --git a/peachjam/adapters/adapters.py b/peachjam/adapters/adapters.py index 7375c3882..2a36b8fe5 100644 --- a/peachjam/adapters/adapters.py +++ b/peachjam/adapters/adapters.py @@ -370,10 +370,12 @@ def download_and_save_document_images(self, document, created_document): document=created_document, defaults={ "file": File(file, name=result["filename"]), - "mimetype": result["mime_type"], + "mime_type": result["mime_type"], "filename": result["filename"], + "size": result["size"], }, ) + logger.info(f"Downloaded image for {created_document}") def get_model(self, document): if document["nature"] == "act": diff --git a/peachjam/admin.py b/peachjam/admin.py index dd60a116e..4b7fc3551 100644 --- a/peachjam/admin.py +++ b/peachjam/admin.py @@ -1079,7 +1079,7 @@ class AttorneyAdmin(ImportExportMixin, admin.ModelAdmin): @admin.register(DocumentMedia) class DocumentMediaAdmin(admin.ModelAdmin): - list_display = ("filename", "mimetype") + list_display = ("filename", "mime_type") admin.site.register( diff --git a/peachjam/migrations/0117_documentmedia.py b/peachjam/migrations/0117_documentmedia.py index 39e8251d6..0ceac485d 100644 --- a/peachjam/migrations/0117_documentmedia.py +++ b/peachjam/migrations/0117_documentmedia.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.19 on 2024-01-18 14:10 +# Generated by Django 3.2.19 on 2024-01-23 08:46 import django.db.models.deletion from django.db import migrations, models @@ -7,6 +7,7 @@ class Migration(migrations.Migration): + dependencies = [ ("peachjam", "0116_copy_and_delete_old_order_outcome_field_data"), ] @@ -37,9 +38,10 @@ class Migration(migrations.Migration): ), ), ( - "mimetype", - models.CharField(max_length=1024, verbose_name="mimetype"), + "mime_type", + models.CharField(max_length=1024, verbose_name="mime_type"), ), + ("size", models.PositiveIntegerField(verbose_name="size")), ( "document", models.ForeignKey( @@ -50,5 +52,9 @@ class Migration(migrations.Migration): ), ), ], + options={ + "verbose_name": "document media", + "verbose_name_plural": "document media", + }, ), ] diff --git a/peachjam/models/core_document_model.py b/peachjam/models/core_document_model.py index 9f8c1a7d1..ec75c31ab 100644 --- a/peachjam/models/core_document_model.py +++ b/peachjam/models/core_document_model.py @@ -928,7 +928,10 @@ class DocumentMedia(models.Model): upload_to=file_location, max_length=1024, ) - mimetype = models.CharField(_("mimetype"), max_length=1024, null=False, blank=False) + mime_type = models.CharField( + _("mime_type"), max_length=1024, null=False, blank=False + ) + size = models.PositiveIntegerField(_("size"), null=False, blank=False) class Meta: verbose_name = _("document media") diff --git a/peachjam/urls.py b/peachjam/urls.py index 269bae934..e6e55d521 100644 --- a/peachjam/urls.py +++ b/peachjam/urls.py @@ -68,6 +68,7 @@ UserProfileDetailView, WorkAutocomplete, ) +from peachjam.views.attachment_media import AttachmentMediaView from peachjam.views.generic_views import CSRFTokenView from peachjam.views.metabase_stats import MetabaseStatsView @@ -257,6 +258,12 @@ ), # django-markdown-editor path("martor/", include("martor.urls")), + # Get a specific media file for a document + re_path( + r"documents/(?P[0-9]+)/media/(?P.*)$", + AttachmentMediaView.as_view(), + name="document-media", + ), ] if settings.DEBUG: diff --git a/peachjam/views/__init__.py b/peachjam/views/__init__.py index 90f988d73..11847b3c7 100644 --- a/peachjam/views/__init__.py +++ b/peachjam/views/__init__.py @@ -1,6 +1,7 @@ # flake8: noqa from .about import * from .article import * +from .attachment_media import * from .authors import * from .autocomplete import * from .books import * diff --git a/peachjam/views/attachment_media.py b/peachjam/views/attachment_media.py new file mode 100644 index 000000000..75330f4d6 --- /dev/null +++ b/peachjam/views/attachment_media.py @@ -0,0 +1,33 @@ +from django.http import HttpResponse +from django.shortcuts import get_object_or_404 +from rest_framework.views import APIView + +from peachjam.models import CoreDocument, DocumentMedia + + +class DocumentResourceView: + def initial(self, request, **kwargs): + self.document = self.lookup_document() + super().initial(request, **kwargs) + + def lookup_document(self): + return get_object_or_404(CoreDocument, id=self.kwargs["document_id"]) + + def get_serializer_context(self): + context = super().get_serializer_context() + context["document"] = self.document + return context + + +class AttachmentMediaView(DocumentResourceView, APIView): + def get(self, request, document_id, filename): + document = get_object_or_404(CoreDocument, id=document_id) + attachment = get_object_or_404( + DocumentMedia, document=document, filename=filename + ) + response = HttpResponse( + attachment.file.read(), content_type=attachment.mime_type + ) + response["Content-Disposition"] = f"inline; filename={attachment.filename}" + response["Content-Length"] = str(attachment.size) + return response