Added new widget to alert if a file is set at larger than 10 MB.

- Updated default widget to use template that adds a notification box.
- Added JS to add onchange & load events to look at the value in the input and trigger the notification & disable the form submits.
This commit is contained in:
Jason Kelly
2021-05-23 14:12:00 +08:00
parent c9617c4bd3
commit 6e655cb0e0
3 changed files with 53 additions and 0 deletions

View File

@ -9,6 +9,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.utils import timezone
from django.utils.translation import gettext_lazy as _
from bookwyrm import activitypub
@ -332,6 +333,14 @@ class TagField(ManyToManyField):
return items
class ClearableFileInputWithWarning(ClearableFileInput):
template_name = 'widgets/clearable_file_input_with_warning.html'
class CustomImageField(ImageField):
widget = ClearableFileInputWithWarning
def image_serializer(value, alt):
"""helper for serializing images"""
if value and hasattr(value, "url"):
@ -395,6 +404,12 @@ class ImageField(ActivitypubFieldMixin, models.ImageField):
image_content = ContentFile(response.content)
return [image_name, image_content]
def formfield(self, **kwargs):
return super().formfield(**{
'form_class': CustomImageField,
**kwargs,
})
class DateTimeField(ActivitypubFieldMixin, models.DateTimeField):
"""activitypub-aware datetime field"""