lib.itmens/common/forms.py

29 lines
802 B
Python
Raw Normal View History

import json
2020-10-03 23:27:41 +02:00
import django.contrib.postgres.forms as postgres
from django import forms
2020-05-05 23:50:48 +08:00
from django.core.exceptions import ValidationError
from django.utils import formats
2020-05-05 23:50:48 +08:00
from django.utils.translation import gettext_lazy as _
from markdownx.fields import MarkdownxFormField
2020-05-01 22:46:15 +08:00
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))