Skip to content

Commit

Permalink
Ingest images
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmwangemi committed Jan 23, 2024
1 parent 0ac2000 commit f35546b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
4 changes: 3 additions & 1 deletion peachjam/adapters/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion peachjam/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 9 additions & 3 deletions peachjam/migrations/0117_documentmedia.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,6 +7,7 @@


class Migration(migrations.Migration):

dependencies = [
("peachjam", "0116_copy_and_delete_old_order_outcome_field_data"),
]
Expand Down Expand Up @@ -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(
Expand All @@ -50,5 +52,9 @@ class Migration(migrations.Migration):
),
),
],
options={
"verbose_name": "document media",
"verbose_name_plural": "document media",
},
),
]
5 changes: 4 additions & 1 deletion peachjam/models/core_document_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions peachjam/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<document_id>[0-9]+)/media/(?P<filename>.*)$",
AttachmentMediaView.as_view(),
name="document-media",
),
]

if settings.DEBUG:
Expand Down
1 change: 1 addition & 0 deletions peachjam/views/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down
33 changes: 33 additions & 0 deletions peachjam/views/attachment_media.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f35546b

Please sign in to comment.