mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 12:01:14 +00:00
Merge pull request #1176 from bookwyrm-social/cover-naming
Fixes renaming images on save
This commit is contained in:
commit
5c65a2c73c
1 changed files with 9 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
||||||
""" activitypub-aware django model fields """
|
""" activitypub-aware django model fields """
|
||||||
from dataclasses import MISSING
|
from dataclasses import MISSING
|
||||||
|
import imghdr
|
||||||
import re
|
import re
|
||||||
from uuid import uuid4
|
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.exceptions import ValidationError
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
from django.db import models
|
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 import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from bookwyrm import activitypub
|
from bookwyrm import activitypub
|
||||||
|
@ -334,10 +335,14 @@ class TagField(ManyToManyField):
|
||||||
|
|
||||||
|
|
||||||
class ClearableFileInputWithWarning(ClearableFileInput):
|
class ClearableFileInputWithWarning(ClearableFileInput):
|
||||||
|
"""max file size warning"""
|
||||||
|
|
||||||
template_name = "widgets/clearable_file_input_with_warning.html"
|
template_name = "widgets/clearable_file_input_with_warning.html"
|
||||||
|
|
||||||
|
|
||||||
class CustomImageField(ImageField):
|
class CustomImageField(DjangoImageField):
|
||||||
|
"""overwrites image field for form"""
|
||||||
|
|
||||||
widget = ClearableFileInputWithWarning
|
widget = ClearableFileInputWithWarning
|
||||||
|
|
||||||
|
|
||||||
|
@ -400,11 +405,12 @@ class ImageField(ActivitypubFieldMixin, models.ImageField):
|
||||||
if not response:
|
if not response:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
image_name = str(uuid4()) + "." + url.split(".")[-1]
|
|
||||||
image_content = ContentFile(response.content)
|
image_content = ContentFile(response.content)
|
||||||
|
image_name = str(uuid4()) + "." + imghdr.what(None, image_content.read())
|
||||||
return [image_name, image_content]
|
return [image_name, image_content]
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
|
"""special case for forms"""
|
||||||
return super().formfield(
|
return super().formfield(
|
||||||
**{
|
**{
|
||||||
"form_class": CustomImageField,
|
"form_class": CustomImageField,
|
||||||
|
|
Loading…
Reference in a new issue