lib.itmens/common/forms.py

34 lines
1,015 B
Python
Raw Permalink Normal View History

from django import forms
2024-06-15 18:26:20 -04:00
from django.forms import ModelForm
2020-05-01 22:46:15 +08:00
2024-06-15 18:26:20 -04:00
class NeoModelForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# if "uuid" in self.fields:
# if self.instance and self.instance.pk:
# self.fields["uuid"].initial = self.instance.uuid
for visible in self.visible_fields():
w = visible.field.widget
w.attrs["class"] = "widget " + w.__class__.__name__.lower()
2020-07-03 15:36:23 +08:00
class PreviewImageInput(forms.FileInput):
2023-01-11 19:11:31 -05:00
template_name = "widgets/image.html"
2020-05-12 14:05:12 +08:00
def format_value(self, value):
"""
Return the file object if it has a defined url attribute.
"""
if self.is_initial(value):
2020-05-12 14:37:12 +08:00
if value.url:
return value.url
else:
return
2020-05-12 14:05:12 +08:00
def is_initial(self, value):
"""
Return whether value is considered to be initial value.
"""
2023-01-11 19:11:31 -05:00
return bool(value and getattr(value, "url", False))