From 290a99bf176a441261ba0d755c1e2d014c7db240 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 14 Jun 2021 15:19:54 -0700 Subject: [PATCH 1/2] Fixes renaming images on save --- bookwyrm/models/fields.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index caa22fcd..70ba4a5b 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -1,5 +1,6 @@ """ activitypub-aware django model fields """ from dataclasses import MISSING +import imghdr import re from uuid import uuid4 @@ -9,7 +10,7 @@ from django.contrib.postgres.fields import ArrayField as DjangoArrayField from django.core.exceptions import ValidationError from django.core.files.base import ContentFile from django.db import models -from django.forms import ClearableFileInput, ImageField +from django.forms import ClearableFileInput, ImageField as DjangoImageField from django.utils import timezone from django.utils.translation import gettext_lazy as _ from bookwyrm import activitypub @@ -334,10 +335,12 @@ class TagField(ManyToManyField): class ClearableFileInputWithWarning(ClearableFileInput): + """max file size warning""" template_name = "widgets/clearable_file_input_with_warning.html" -class CustomImageField(ImageField): +class CustomImageField(DjangoImageField): + """overwrites image field for form""" widget = ClearableFileInputWithWarning @@ -400,11 +403,12 @@ class ImageField(ActivitypubFieldMixin, models.ImageField): if not response: return None - image_name = str(uuid4()) + "." + url.split(".")[-1] image_content = ContentFile(response.content) + image_name = str(uuid4()) + "." + imghdr.what(None, image_content.read()) return [image_name, image_content] def formfield(self, **kwargs): + """ special case for forms """ return super().formfield( **{ "form_class": CustomImageField, From cd014e43ad7cb4b223fac7e00f52c13fd97b71d4 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 14 Jun 2021 15:29:07 -0700 Subject: [PATCH 2/2] Python formatting --- bookwyrm/models/fields.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 70ba4a5b..379323b9 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -336,11 +336,13 @@ class TagField(ManyToManyField): class ClearableFileInputWithWarning(ClearableFileInput): """max file size warning""" + template_name = "widgets/clearable_file_input_with_warning.html" class CustomImageField(DjangoImageField): """overwrites image field for form""" + widget = ClearableFileInputWithWarning @@ -408,7 +410,7 @@ class ImageField(ActivitypubFieldMixin, models.ImageField): return [image_name, image_content] def formfield(self, **kwargs): - """ special case for forms """ + """special case for forms""" return super().formfield( **{ "form_class": CustomImageField,