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