|
|
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]
|
|
|
|
|
|
|