##// END OF EJS Templates
Added file and url multi value field that will simplify adding many files to one post in future
Added file and url multi value field that will simplify adding many files to one post in future

File last commit:

r1752:4ca1286e default
r1752:4ca1286e default
Show More
fields.py
42 lines | 1.3 KiB | text/x-python | PythonLexer
from django import forms
from django.utils.translation import ugettext_lazy as _, ungettext_lazy
ATTRIBUTE_PLACEHOLDER = 'placeholder'
class UrlFileWidget(forms.MultiWidget):
def __init__(self, *args, **kwargs):
widgets = (
forms.ClearableFileInput(attrs={'accept': 'file/*'}),
forms.TextInput(attrs={ATTRIBUTE_PLACEHOLDER:
'http://example.com/image.png'}),
)
super().__init__(widgets, *args, **kwargs)
def decompress(self, value):
return [None, None]
class UrlFileField(forms.MultiValueField):
widget = UrlFileWidget
def __init__(self, *args, **kwargs):
fields = (
forms.FileField(required=False, label=_('File'),
widget=forms.ClearableFileInput(
attrs={'accept': 'file/*'})),
forms.CharField(required=False, label=_('File URL'),
widget=forms.TextInput(
attrs={ATTRIBUTE_PLACEHOLDER:
'http://example.com/image.png'})),
)
super().__init__(
fields=fields,
require_all_fields=False, *args, **kwargs)
def compress(self, data_list):
if data_list and len(data_list) >= 2:
return data_list[0] or data_list[1]