Show More
@@ -1,62 +1,63 b'' | |||
|
1 | import re | |
|
1 | 2 | from django import forms |
|
2 | 3 | from neboard import settings |
|
3 | 4 | |
|
4 | 5 | |
|
5 | 6 | class PostForm(forms.Form): |
|
6 | 7 | MAX_TEXT_LENGTH = 10000 |
|
7 | 8 | MAX_IMAGE_SIZE = 8 * 1024 * 1024 |
|
8 | 9 | |
|
9 | 10 | title = forms.CharField(max_length=50, required=False) |
|
10 | 11 | text = forms.CharField(widget=forms.Textarea, required=False) |
|
11 | 12 | image = forms.ImageField(required=False) |
|
12 | 13 | |
|
13 | 14 | def clean_text(self): |
|
14 | 15 | text = self.cleaned_data['text'] |
|
15 | 16 | if text: |
|
16 | 17 | if len(text) > self.MAX_TEXT_LENGTH: |
|
17 | 18 | raise forms.ValidationError('Too many text') |
|
18 | 19 | return text |
|
19 | 20 | |
|
20 | 21 | def clean_image(self): |
|
21 | 22 | image = self.cleaned_data['image'] |
|
22 | 23 | if image: |
|
23 | 24 | if image._size > self.MAX_IMAGE_SIZE: |
|
24 | 25 | raise forms.ValidationError('Too large image: more than ' + |
|
25 | 26 | str(self.MAX_IMAGE_SIZE) + ' bytes') |
|
26 | 27 | return image |
|
27 | 28 | |
|
28 | 29 | def clean(self): |
|
29 | 30 | cleaned_data = super(PostForm, self).clean() |
|
30 | 31 | |
|
31 | 32 | text = cleaned_data.get('text') |
|
32 | 33 | image = cleaned_data.get('image') |
|
33 | 34 | |
|
34 | 35 | if (not text) and (not image): |
|
35 | 36 | raise forms.ValidationError('Enter either text or image') |
|
36 | 37 | |
|
37 | 38 | return cleaned_data |
|
38 | 39 | |
|
39 | 40 | |
|
40 | 41 | class ThreadForm(PostForm): |
|
41 | INVALID_TAG_CHARACTERS = ['+', '/', '&', '=', '?', '-', '.', ','] | |
|
42 | regex_tags = re.compile(r'^[\w\s]+$') | |
|
42 | 43 | |
|
43 | 44 | tags = forms.CharField(max_length=100) |
|
44 | 45 | |
|
45 | 46 | def clean_tags(self): |
|
46 | 47 | tags = self.cleaned_data['tags'] |
|
48 | ||
|
47 | 49 | if tags: |
|
48 | for character in tags: | |
|
49 | if character in self.INVALID_TAG_CHARACTERS: | |
|
50 | if not self.regex_tags.match(tags): | |
|
50 | 51 |
|
|
51 | 52 |
|
|
52 | 53 | |
|
53 | 54 | return tags |
|
54 | 55 | |
|
55 | 56 | def clean(self): |
|
56 | 57 | cleaned_data = super(ThreadForm, self).clean() |
|
57 | 58 | |
|
58 | 59 | return cleaned_data |
|
59 | 60 | |
|
60 | 61 | |
|
61 | 62 | class SettingsForm(forms.Form): |
|
62 | 63 | theme = forms.ChoiceField(choices=settings.THEMES, widget=forms.RadioSelect) No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now