##// END OF EJS Templates
Added file and url multi value field that will simplify adding many files to one post in future
neko259 -
r1752:4ca1286e default
parent child Browse files
Show More
@@ -0,0 +1,42 b''
1 from django import forms
2 from django.utils.translation import ugettext_lazy as _, ungettext_lazy
3
4
5 ATTRIBUTE_PLACEHOLDER = 'placeholder'
6
7
8 class UrlFileWidget(forms.MultiWidget):
9 def __init__(self, *args, **kwargs):
10 widgets = (
11 forms.ClearableFileInput(attrs={'accept': 'file/*'}),
12 forms.TextInput(attrs={ATTRIBUTE_PLACEHOLDER:
13 'http://example.com/image.png'}),
14 )
15 super().__init__(widgets, *args, **kwargs)
16
17 def decompress(self, value):
18 return [None, None]
19
20
21 class UrlFileField(forms.MultiValueField):
22 widget = UrlFileWidget
23
24 def __init__(self, *args, **kwargs):
25 fields = (
26 forms.FileField(required=False, label=_('File'),
27 widget=forms.ClearableFileInput(
28 attrs={'accept': 'file/*'})),
29 forms.CharField(required=False, label=_('File URL'),
30 widget=forms.TextInput(
31 attrs={ATTRIBUTE_PLACEHOLDER:
32 'http://example.com/image.png'})),
33 )
34
35 super().__init__(
36 fields=fields,
37 require_all_fields=False, *args, **kwargs)
38
39 def compress(self, data_list):
40 if data_list and len(data_list) >= 2:
41 return data_list[0] or data_list[1]
42
@@ -8,6 +8,7 b' import pytz'
8 from django import forms
8 from django import forms
9 from django.core.files.uploadedfile import SimpleUploadedFile
9 from django.core.files.uploadedfile import SimpleUploadedFile
10 from django.core.exceptions import ObjectDoesNotExist
10 from django.core.exceptions import ObjectDoesNotExist
11 from django.core.files.uploadedfile import UploadedFile
11 from django.forms.utils import ErrorList
12 from django.forms.utils import ErrorList
12 from django.utils.translation import ugettext_lazy as _, ungettext_lazy
13 from django.utils.translation import ugettext_lazy as _, ungettext_lazy
13 from django.utils import timezone
14 from django.utils import timezone
@@ -20,6 +21,7 b' from boards.models.post import TITLE_MAX'
20 from boards.models import Tag, Post
21 from boards.models import Tag, Post
21 from boards.utils import validate_file_size, get_file_mimetype, \
22 from boards.utils import validate_file_size, get_file_mimetype, \
22 FILE_EXTENSION_DELIMITER
23 FILE_EXTENSION_DELIMITER
24 from boards.abstracts.fields import UrlFileField
23 from neboard import settings
25 from neboard import settings
24 import boards.settings as board_settings
26 import boards.settings as board_settings
25 import neboard
27 import neboard
@@ -167,13 +169,7 b' class PostForm(NeboardForm):'
167 ATTRIBUTE_ROWS: TEXTAREA_ROWS,
169 ATTRIBUTE_ROWS: TEXTAREA_ROWS,
168 }),
170 }),
169 required=False, label=LABEL_TEXT)
171 required=False, label=LABEL_TEXT)
170 file = forms.FileField(required=False, label=_('File'),
172 file = UrlFileField(required=False, label=_('File'))
171 widget=forms.ClearableFileInput(
172 attrs={'accept': 'file/*'}))
173 file_url = forms.CharField(required=False, label=_('File URL'),
174 widget=forms.TextInput(
175 attrs={ATTRIBUTE_PLACEHOLDER:
176 'http://example.com/image.png'}))
177
173
178 # This field is for spam prevention only
174 # This field is for spam prevention only
179 email = forms.CharField(max_length=100, required=False, label=_('e-mail'),
175 email = forms.CharField(max_length=100, required=False, label=_('e-mail'),
@@ -224,15 +220,21 b' class PostForm(NeboardForm):'
224 def clean_file(self):
220 def clean_file(self):
225 file = self.cleaned_data['file']
221 file = self.cleaned_data['file']
226
222
227 if file:
223 if isinstance(file, UploadedFile):
228 validate_file_size(file.size)
224 file = self._clean_file_file(file)
225 else:
226 file = self._clean_file_url(file)
227
228 return file
229
230 def _clean_file_file(self, file):
231 validate_file_size(file.size)
229 self._update_file_extension(file)
232 self._update_file_extension(file)
230
233
231 return file
234 return file
232
235
233 def clean_file_url(self):
234 url = self.cleaned_data['file_url']
235
236
237 def _clean_file_url(self, url):
236 file = None
238 file = None
237
239
238 if url:
240 if url:
@@ -296,15 +298,13 b' class PostForm(NeboardForm):'
296 """
298 """
297
299
298 file = self.cleaned_data['file']
300 file = self.cleaned_data['file']
299 if type(self.cleaned_data['file_url']) is not str:
301 if isinstance(file, UploadedFile):
300 file_url = self.cleaned_data['file_url']
302 return file
301 else:
302 file_url = None
303 return file or file_url
304
303
305 def get_file_url(self):
304 def get_file_url(self):
306 if not self.get_file():
305 file = self.cleaned_data['file']
307 return self.cleaned_data['file_url']
306 if type(file) == str:
307 return file
308
308
309 def get_tripcode(self):
309 def get_tripcode(self):
310 title = self.cleaned_data['title']
310 title = self.cleaned_data['title']
@@ -164,7 +164,6 b''
164 <div class="form-submit">
164 <div class="form-submit">
165 <input type="submit" value="{% trans "Post" %}"/>
165 <input type="submit" value="{% trans "Post" %}"/>
166 <button id="preview-button" type="button" onclick="return false;">{% trans 'Preview' %}</button>
166 <button id="preview-button" type="button" onclick="return false;">{% trans 'Preview' %}</button>
167 <button id="file-source-button" type="button" onclick="return false;">{% trans 'Change file source' %}</button>
168 </div>
167 </div>
169 </form>
168 </form>
170 </div>
169 </div>
@@ -54,7 +54,6 b''
54 <div class="form-submit">
54 <div class="form-submit">
55 <input type="submit" value="{% trans "Post" %}"/>
55 <input type="submit" value="{% trans "Post" %}"/>
56 <button id="preview-button" type="button" onclick="return false;">{% trans 'Preview' %}</button>
56 <button id="preview-button" type="button" onclick="return false;">{% trans 'Preview' %}</button>
57 <button id="file-source-button" type="button" onclick="return false;">{% trans 'Change file source' %}</button>
58 </div>
57 </div>
59 </form>
58 </form>
60 </div>
59 </div>
General Comments 0
You need to be logged in to leave comments. Login now