##// END OF EJS Templates
Do not index post's uid field as mysql does not support it
Do not index post's uid field as mysql does not support it

File last commit:

r1764:ae6e7d4c default
r1835:aa9b2361 default
Show More
fields.py
90 lines | 2.5 KiB | text/x-python | PythonLexer
neko259
Move forms to a separate folder module with fields module
r1757 from django import forms
neko259
Updated placeholder for the URL input to be more user-friendly
r1764 from django.utils.translation import ugettext_lazy as _
neko259
Move forms to a separate folder module with fields module
r1757
ATTRIBUTE_PLACEHOLDER = 'placeholder'
neko259
Use one input for multiple files and multiple URLs
r1761 ATTRIBUTE_ROWS = 'rows'
URL_ROWS = 2
neko259
Updated placeholder for the URL input to be more user-friendly
r1764 URL_PLACEHOLDER = _('Insert URLs on separate lines.')
neko259
Use one input for multiple files and multiple URLs
r1761
class MultipleFileInput(forms.FileInput):
"""
Input that allows to enter many files at once, utilizing the html5 "multiple"
attribute of a file input.
"""
def value_from_datadict(self, data, files, name):
if len(files) == 0:
return None
else:
return files.getlist(name)
def render(self, name, value, attrs=None):
if not attrs:
attrs = dict()
attrs['multiple'] = ''
return super().render(name, None, attrs=attrs)
class MultipleFileField(forms.FileField):
"""
Field that allows to enter multiple files from one input.
"""
def to_python(self, data):
if not data or len(data) == 0:
return None
for file in data:
super().to_python(file)
return data
neko259
Move forms to a separate folder module with fields module
r1757
class UrlFileWidget(forms.MultiWidget):
neko259
Use one input for multiple files and multiple URLs
r1761 """
Widget with a file input and a text input that allows to enter either a
file from a local system, or a URL from which the file would be downloaded.
"""
neko259
Move forms to a separate folder module with fields module
r1757 def __init__(self, *args, **kwargs):
widgets = (
neko259
Use one input for multiple files and multiple URLs
r1761 MultipleFileInput(attrs={'accept': 'file/*'}),
forms.Textarea(attrs={
neko259
Updated placeholder for the URL input to be more user-friendly
r1764 ATTRIBUTE_PLACEHOLDER: URL_PLACEHOLDER,
neko259
Use one input for multiple files and multiple URLs
r1761 ATTRIBUTE_ROWS: URL_ROWS,
}),
neko259
Move forms to a separate folder module with fields module
r1757 )
super().__init__(widgets, *args, **kwargs)
def decompress(self, value):
return [None, None]
class UrlFileField(forms.MultiValueField):
neko259
Use one input for multiple files and multiple URLs
r1761 """
Field with a file input and a text input that allows to enter either a
file from a local system, or a URL from which the file would be downloaded.
"""
neko259
Move forms to a separate folder module with fields module
r1757 widget = UrlFileWidget
def __init__(self, *args, **kwargs):
fields = (
neko259
Use one input for multiple files and multiple URLs
r1761 MultipleFileField(required=False, label=_('File')),
forms.CharField(required=False, label=_('File URL')),
neko259
Move forms to a separate folder module with fields module
r1757 )
super().__init__(
fields=fields,
require_all_fields=False, *args, **kwargs)
def compress(self, data_list):
neko259
Allow loading files and links together
r1762 all_data = []
for data in data_list:
if type(data) == list:
all_data += data
neko259
Fixed shadowing of an internal keyword. Fixed empty URL being treated as a file input
r1763 elif type(data) == str and len(data) > 0:
file_input = data.replace('\r\n', '\n')
url_list = file_input.split('\n')
neko259
Allow loading files and links together
r1762 all_data += url_list
neko259
Move forms to a separate folder module with fields module
r1757
neko259
Allow loading files and links together
r1762 return all_data