diff --git a/peachjam/adapters/adapters.py b/peachjam/adapters/adapters.py index 14255f5fb..c59f7cc1d 100644 --- a/peachjam/adapters/adapters.py +++ b/peachjam/adapters/adapters.py @@ -21,6 +21,7 @@ DocumentNature, DocumentTopic, GenericDocument, + Image, LegalInstrument, Legislation, Locality, @@ -348,6 +349,38 @@ def update_document(self, url): self.fetch_and_create_aliases(document, created_doc) self.set_parent(document, created_doc) self.fetch_relationships(document, created_doc) + self.download_and_save_document_images(document, created_doc) + + def list_images_from_content_api(self, document): + links = document["links"] + if links: + for link in links: + if link["href"].endswith("media.json"): + response = self.client_get(link["href"]) + if response.json()["results"]: + return response.json()["results"] + + def download_and_save_document_images(self, document, created_document): + # delete all existing images for the doc + Image.objects.filter(document=created_document).delete() + + image_list = self.list_images_from_content_api(document) + if image_list: + for result in image_list: + if result["mime_type"].startswith("image/"): + with NamedTemporaryFile() as file: + r = self.client_get(result["url"]) + file.write(r.content) + + Image.objects.create( + document=created_document, + file=File(file, name=result["filename"]), + mimetype=result["mime_type"], + filename=result["filename"], + size=result["size"], + ) + + logger.info(f"Downloaded image(s) for {created_document}") def get_model(self, document): if document["nature"] == "act": diff --git a/peachjam/admin.py b/peachjam/admin.py index c01171204..e65ba5829 100644 --- a/peachjam/admin.py +++ b/peachjam/admin.py @@ -56,6 +56,7 @@ ExternalDocument, Gazette, GenericDocument, + Image, Ingestor, IngestorSetting, Journal, @@ -335,6 +336,10 @@ class AttachedFilesInline(BaseAttachmentFileInline): form = AttachedFilesForm +class ImageInline(BaseAttachmentFileInline): + model = Image + + class DocumentAdmin(BaseAdmin): form = DocumentForm inlines = [ @@ -342,6 +347,7 @@ class DocumentAdmin(BaseAdmin): SourceFileInline, AlternativeNameInline, AttachedFilesInline, + ImageInline, ] list_display = ( "title", diff --git a/peachjam/views/generic_views.py b/peachjam/views/generic_views.py index 1bdd954b3..e8cb0fb2b 100644 --- a/peachjam/views/generic_views.py +++ b/peachjam/views/generic_views.py @@ -165,7 +165,8 @@ def get_context_data(self, **kwargs): context["display_type"] = ( "akn" if context["document"].content_html_is_akn else "html" ) - self.prefix_images(context["document"]) + if not context["document"].content_html_is_akn: + self.prefix_images(context["document"]) elif hasattr(context["document"], "source_file"): context["display_type"] = "pdf" else: @@ -274,7 +275,7 @@ def get_notices(self): return [] def prefix_images(self, document): - """Rewrite image URLs so that we can server them correctly.""" + """Rewrite image URLs so that we can serve them correctly.""" root = html.fromstring(document.content_html) for img in root.xpath(".//img[@src]"):