Show More
@@ -45,7 +45,8 LABEL_TITLE = _('Title') | |||||
45 | LABEL_TEXT = _('Text') |
|
45 | LABEL_TEXT = _('Text') | |
46 | LABEL_TAG = _('Tag') |
|
46 | LABEL_TAG = _('Tag') | |
47 | LABEL_SEARCH = _('Search') |
|
47 | LABEL_SEARCH = _('Search') | |
48 | LABEL_FILE = _('File') |
|
48 | LABEL_FILE_1 = _('File 1') | |
|
49 | LABEL_FILE_2 = _('File 2') | |||
49 |
|
50 | |||
50 | ERROR_SPEED = 'Please wait %(delay)d second before sending message' |
|
51 | ERROR_SPEED = 'Please wait %(delay)d second before sending message' | |
51 | ERROR_SPEED_PLURAL = 'Please wait %(delay)d seconds before sending message' |
|
52 | ERROR_SPEED_PLURAL = 'Please wait %(delay)d seconds before sending message' | |
@@ -168,8 +169,8 class PostForm(NeboardForm): | |||||
168 | ATTRIBUTE_ROWS: TEXTAREA_ROWS, |
|
169 | ATTRIBUTE_ROWS: TEXTAREA_ROWS, | |
169 | }), |
|
170 | }), | |
170 | required=False, label=LABEL_TEXT) |
|
171 | required=False, label=LABEL_TEXT) | |
171 |
file_1 = UrlFileField(required=False, label=LABEL_FILE |
|
172 | file_1 = UrlFileField(required=False, label=LABEL_FILE_1) | |
172 |
file_2 = UrlFileField(required=False, label=LABEL_FILE |
|
173 | file_2 = UrlFileField(required=False, label=LABEL_FILE_2) | |
173 |
|
174 | |||
174 | # This field is for spam prevention only |
|
175 | # This field is for spam prevention only | |
175 | email = forms.CharField(max_length=100, required=False, label=_('e-mail'), |
|
176 | email = forms.CharField(max_length=100, required=False, label=_('e-mail'), | |
@@ -185,20 +186,6 class PostForm(NeboardForm): | |||||
185 | need_to_ban = False |
|
186 | need_to_ban = False | |
186 | image = None |
|
187 | image = None | |
187 |
|
188 | |||
188 | def _update_file_extension(self, file): |
|
|||
189 | if file: |
|
|||
190 | mimetype =get_file_mimetype(file) |
|
|||
191 | extension = MIMETYPE_EXTENSIONS.get(mimetype) |
|
|||
192 | if extension: |
|
|||
193 | filename = file.name.split(FILE_EXTENSION_DELIMITER, 1)[0] |
|
|||
194 | new_filename = filename + FILE_EXTENSION_DELIMITER + extension |
|
|||
195 |
|
||||
196 | file.name = new_filename |
|
|||
197 | else: |
|
|||
198 | logger = logging.getLogger('boards.forms.extension') |
|
|||
199 |
|
||||
200 | logger.info('Unrecognized file mimetype: {}'.format(mimetype)) |
|
|||
201 |
|
||||
202 | def clean_title(self): |
|
189 | def clean_title(self): | |
203 | title = self.cleaned_data['title'] |
|
190 | title = self.cleaned_data['title'] | |
204 | if title: |
|
191 | if title: | |
@@ -223,48 +210,6 class PostForm(NeboardForm): | |||||
223 | def clean_file_2(self): |
|
210 | def clean_file_2(self): | |
224 | return self._clean_file(self.cleaned_data['file_2']) |
|
211 | return self._clean_file(self.cleaned_data['file_2']) | |
225 |
|
212 | |||
226 | def _clean_file(self, file): |
|
|||
227 | if isinstance(file, UploadedFile): |
|
|||
228 | file = self._clean_file_file(file) |
|
|||
229 | else: |
|
|||
230 | file = self._clean_file_url(file) |
|
|||
231 |
|
||||
232 | return file |
|
|||
233 |
|
||||
234 | def _clean_file_file(self, file): |
|
|||
235 | validate_file_size(file.size) |
|
|||
236 | self._update_file_extension(file) |
|
|||
237 |
|
||||
238 | return file |
|
|||
239 |
|
||||
240 | def _clean_file_url(self, url): |
|
|||
241 | file = None |
|
|||
242 |
|
||||
243 | if url: |
|
|||
244 | try: |
|
|||
245 | file = get_image_by_alias(url, self.session) |
|
|||
246 | self.image = file |
|
|||
247 |
|
||||
248 | if file is not None: |
|
|||
249 | return |
|
|||
250 |
|
||||
251 | if file is None: |
|
|||
252 | file = self._get_file_from_url(url) |
|
|||
253 | if not file: |
|
|||
254 | raise forms.ValidationError(_('Invalid URL')) |
|
|||
255 | else: |
|
|||
256 | validate_file_size(file.size) |
|
|||
257 | self._update_file_extension(file) |
|
|||
258 | except forms.ValidationError as e: |
|
|||
259 | # Assume we will get the plain URL instead of a file and save it |
|
|||
260 | if REGEX_URL.match(url): |
|
|||
261 | logger.info('Error in forms: {}'.format(e)) |
|
|||
262 | return url |
|
|||
263 | else: |
|
|||
264 | raise e |
|
|||
265 |
|
||||
266 | return file |
|
|||
267 |
|
||||
268 | def clean(self): |
|
213 | def clean(self): | |
269 | cleaned_data = super(PostForm, self).clean() |
|
214 | cleaned_data = super(PostForm, self).clean() | |
270 |
|
215 | |||
@@ -350,6 +295,60 class PostForm(NeboardForm): | |||||
350 | def is_subscribe(self): |
|
295 | def is_subscribe(self): | |
351 | return self.cleaned_data['subscribe'] |
|
296 | return self.cleaned_data['subscribe'] | |
352 |
|
297 | |||
|
298 | def _update_file_extension(self, file): | |||
|
299 | if file: | |||
|
300 | mimetype = get_file_mimetype(file) | |||
|
301 | extension = MIMETYPE_EXTENSIONS.get(mimetype) | |||
|
302 | if extension: | |||
|
303 | filename = file.name.split(FILE_EXTENSION_DELIMITER, 1)[0] | |||
|
304 | new_filename = filename + FILE_EXTENSION_DELIMITER + extension | |||
|
305 | ||||
|
306 | file.name = new_filename | |||
|
307 | else: | |||
|
308 | logger.info('Unrecognized file mimetype: {}'.format(mimetype)) | |||
|
309 | ||||
|
310 | def _clean_file(self, file): | |||
|
311 | if isinstance(file, UploadedFile): | |||
|
312 | file = self._clean_file_file(file) | |||
|
313 | else: | |||
|
314 | file = self._clean_file_url(file) | |||
|
315 | ||||
|
316 | return file | |||
|
317 | ||||
|
318 | def _clean_file_file(self, file): | |||
|
319 | validate_file_size(file.size) | |||
|
320 | self._update_file_extension(file) | |||
|
321 | ||||
|
322 | return file | |||
|
323 | ||||
|
324 | def _clean_file_url(self, url): | |||
|
325 | file = None | |||
|
326 | ||||
|
327 | if url: | |||
|
328 | try: | |||
|
329 | file = get_image_by_alias(url, self.session) | |||
|
330 | self.image = file | |||
|
331 | ||||
|
332 | if file is not None: | |||
|
333 | return | |||
|
334 | ||||
|
335 | if file is None: | |||
|
336 | file = self._get_file_from_url(url) | |||
|
337 | if not file: | |||
|
338 | raise forms.ValidationError(_('Invalid URL')) | |||
|
339 | else: | |||
|
340 | validate_file_size(file.size) | |||
|
341 | self._update_file_extension(file) | |||
|
342 | except forms.ValidationError as e: | |||
|
343 | # Assume we will get the plain URL instead of a file and save it | |||
|
344 | if REGEX_URL.match(url): | |||
|
345 | logger.info('Error in forms: {}'.format(e)) | |||
|
346 | return url | |||
|
347 | else: | |||
|
348 | raise e | |||
|
349 | ||||
|
350 | return file | |||
|
351 | ||||
353 | def _clean_text_file(self): |
|
352 | def _clean_text_file(self): | |
354 | text = self.cleaned_data.get('text') |
|
353 | text = self.cleaned_data.get('text') | |
355 | file = self.get_files() |
|
354 | file = self.get_files() | |
@@ -404,9 +403,6 class PostForm(NeboardForm): | |||||
404 | raise forms.ValidationError(e) |
|
403 | raise forms.ValidationError(e) | |
405 |
|
404 | |||
406 | def _validate_hash(self, timestamp: str, iteration: str, guess: str, message: str): |
|
405 | def _validate_hash(self, timestamp: str, iteration: str, guess: str, message: str): | |
407 | post_time = timezone.datetime.fromtimestamp( |
|
|||
408 | int(timestamp[:-3]), tz=timezone.get_current_timezone()) |
|
|||
409 |
|
||||
410 | payload = timestamp + message.replace('\r\n', '\n') |
|
406 | payload = timestamp + message.replace('\r\n', '\n') | |
411 | difficulty = board_settings.get_int('Forms', 'PowDifficulty') |
|
407 | difficulty = board_settings.get_int('Forms', 'PowDifficulty') | |
412 | target = str(int(2 ** (POW_HASH_LENGTH * 3) / difficulty)) |
|
408 | target = str(int(2 ** (POW_HASH_LENGTH * 3) / difficulty)) | |
@@ -420,7 +416,6 class PostForm(NeboardForm): | |||||
420 | [_('Invalid PoW.')]) |
|
416 | [_('Invalid PoW.')]) | |
421 |
|
417 | |||
422 |
|
418 | |||
423 |
|
||||
424 | class ThreadForm(PostForm): |
|
419 | class ThreadForm(PostForm): | |
425 |
|
420 | |||
426 | tags = forms.CharField( |
|
421 | tags = forms.CharField( |
1 | NO CONTENT: modified file, binary diff hidden |
|
NO CONTENT: modified file, binary diff hidden |
@@ -63,9 +63,13 msgstr "Метка" | |||||
63 | msgid "Search" |
|
63 | msgid "Search" | |
64 | msgstr "Поиск" |
|
64 | msgstr "Поиск" | |
65 |
|
65 | |||
66 |
#: forms.py: |
|
66 | #: forms.py:48 | |
67 | msgid "File" |
|
67 | msgid "File 1" | |
68 | msgstr "Файл" |
|
68 | msgstr "Файл 1" | |
|
69 | ||||
|
70 | #: forms.py:48 | |||
|
71 | msgid "File 2" | |||
|
72 | msgstr "Файл 2" | |||
69 |
|
73 | |||
70 | #: forms.py:142 |
|
74 | #: forms.py:142 | |
71 | msgid "File URL" |
|
75 | msgid "File URL" |
General Comments 0
You need to be logged in to leave comments.
Login now