Nessuna descrizione

widgets.py 1.0KB

12345678910111213141516171819202122232425262728
  1. from __future__ import annotations
  2. from django.forms.widgets import ClearableFileInput
  3. class ImagePreviewWidget(ClearableFileInput):
  4. template_name = "cms/widgets/image_preview_input.html"
  5. def __init__(self, attrs=None):
  6. attrs = {**({} if attrs is None else attrs), "accept": "image/*"}
  7. super().__init__(attrs=attrs)
  8. def get_context(self, name, value, attrs):
  9. # Merge our button-like classes into any provided classes without duplicating the class attribute in template
  10. base_classes = (
  11. "file:mr-3 file:px-3 file:py-2 file:rounded file:border-0 "
  12. "file:bg-blue-600 file:text-white hover:file:bg-blue-700 text-sm text-gray-700"
  13. )
  14. attrs = attrs or {}
  15. existing = (attrs.get("class") or self.attrs.get("class") or "").strip()
  16. merged = (existing + " " + base_classes).strip() if existing else base_classes
  17. attrs["class"] = merged
  18. return super().get_context(name, value, attrs)
  19. class Media:
  20. css = {"all": []}
  21. js = []