Skip to content

Commit

Permalink
Merge branch 'main' into aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmwangemi committed Jan 26, 2024
2 parents b396d3f + c49c728 commit 5eb59f8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
33 changes: 33 additions & 0 deletions peachjam/adapters/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
DocumentNature,
DocumentTopic,
GenericDocument,
Image,
LegalInstrument,
Legislation,
Locality,
Expand Down Expand Up @@ -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":
Expand Down
6 changes: 6 additions & 0 deletions peachjam/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
ExternalDocument,
Gazette,
GenericDocument,
Image,
Ingestor,
IngestorSetting,
Journal,
Expand Down Expand Up @@ -335,13 +336,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
5 changes: 3 additions & 2 deletions peachjam/views/generic_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]"):
Expand Down

0 comments on commit 5eb59f8

Please sign in to comment.