Skip to content

Commit

Permalink
Ingest images
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmwangemi committed Jan 24, 2024
1 parent f35546b commit 8a6a56a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 48 deletions.
13 changes: 7 additions & 6 deletions peachjam/adapters/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from peachjam.models import (
Author,
CoreDocument,
DocumentMedia,
DocumentNature,
DocumentTopic,
GenericDocument,
Image,
LegalInstrument,
Legislation,
Locality,
Expand Down Expand Up @@ -354,28 +354,29 @@ def list_images_from_content_api(self, document):
for link in links:
if link["href"].endswith("media.json"):
response = self.client_get(link["href"])
if response.status_code == 200 and response.json()["results"]:
if response.json()["results"]:
return response.json()["results"]

def download_and_save_document_images(self, document, created_document):
image_list = self.list_images_from_content_api(document)
if image_list:
for result in image_list:
if "image/" in result["mime_type"]:
if result["mime_type"].startswith("image/"):
with NamedTemporaryFile() as file:
r = self.client_get(result["url"])
file.write(r.content)

DocumentMedia.objects.get_or_create(
Image.objects.get_or_create(
document=created_document,
defaults={
"file": File(file, name=result["filename"]),
"mime_type": result["mime_type"],
"mimetype": result["mime_type"],
"filename": result["filename"],
"size": result["size"],
},
)
logger.info(f"Downloaded image for {created_document}")

logger.info(f"Downloaded image(s) for {created_document}")

def get_model(self, document):
if document["nature"] == "act":
Expand Down
6 changes: 6 additions & 0 deletions peachjam/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
ExternalDocument,
Gazette,
GenericDocument,
Image,
Ingestor,
IngestorSetting,
Journal,
Expand Down Expand Up @@ -336,13 +337,18 @@ class AttachedFilesInline(BaseAttachmentFileInline):
form = AttachedFilesForm


class ImageInline(BaseAttachmentFileInline):
model = Image


class DocumentAdmin(BaseAdmin):
form = DocumentForm
inlines = [
DocumentTopicInline,
SourceFileInline,
AlternativeNameInline,
AttachedFilesInline,
ImageInline,
]
list_display = (
"title",
Expand Down
9 changes: 1 addition & 8 deletions peachjam/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
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 @@ -171,7 +170,7 @@
name="document_source_pdf",
),
re_path(
r"^(?P<frbr_uri>akn/.*)/media/(?P<filename>.+)$",
r"^(?P<frbr_uri>akn/.*)/media/media/(?P<filename>.+)$",
cache_page(CACHE_DURATION)(DocumentMediaView.as_view()),
name="document_media",
),
Expand Down Expand Up @@ -258,12 +257,6 @@
),
# 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: 0 additions & 1 deletion peachjam/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# 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: 0 additions & 33 deletions peachjam/views/attachment_media.py

This file was deleted.

1 change: 1 addition & 0 deletions peachjam/views/generic_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def get_context_data(self, **kwargs):
context["display_type"] = (
"akn" if context["document"].content_html_is_akn else "html"
)
if not context["document"].content_html_is_akn:
self.prefix_images(context["document"])
elif hasattr(context["document"], "source_file"):
context["display_type"] = "pdf"
Expand Down

0 comments on commit 8a6a56a

Please sign in to comment.