This commit is contained in:
relikd
2023-10-02 23:39:20 +02:00
commit 8629b01da3
47 changed files with 1412 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
from django.conf import settings
# from django.contrib.admin.widgets import AdminFileWidget
# from django.core.exceptions import ValidationError
from django.db import models
from django.forms import ValidationError
from django.forms.widgets import ClearableFileInput
# class ImageFileWidget(AdminFileWidget):
class ImageFileWidget(ClearableFileInput):
template_name = 'forms/img-file.html'
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
context['MEDIA_URL'] = settings.MEDIA_URL
return context
class FileWithImagePreview(models.FileField):
def formfield(self, **kwargs):
# if 'widget' not in kwargs: # only if no other is set (admin UI)
kwargs['widget'] = ImageFileWidget
return super().formfield(**kwargs)
class ImageValidator:
help_text = 'Ideal: 250 x 320 px (JPEG oder PNG)'
@staticmethod
def validate(value: 'models.FieldFile') -> None:
# TODO: make configurable
if value.size > 512 * 1024:
raise ValidationError('Datei darf maximal 512 KB groß sein.')
content_type = getattr(value.file, 'content_type', None)
if content_type not in ['image/png', 'image/jpeg', None]:
raise ValidationError('Nur JPEG und PNG Bilder werden unterstützt')