Show More
@@ -2,10 +2,16 b' import re' | |||||
2 | from captcha.fields import CaptchaField |
|
2 | from captcha.fields import CaptchaField | |
3 | from django import forms |
|
3 | from django import forms | |
4 | from django.forms.util import ErrorList |
|
4 | from django.forms.util import ErrorList | |
|
5 | from django.utils import timezone | |||
|
6 | import time | |||
|
7 | import math | |||
5 | from boards.models import TITLE_MAX_LENGTH, User |
|
8 | from boards.models import TITLE_MAX_LENGTH, User | |
6 | from neboard import settings |
|
9 | from neboard import settings | |
7 | from boards import utils |
|
10 | from boards import utils | |
8 |
|
11 | |||
|
12 | LAST_POST_TIME = "last_post_time" | |||
|
13 | ||||
|
14 | ||||
9 | class PlainErrorList(ErrorList): |
|
15 | class PlainErrorList(ErrorList): | |
10 | def __unicode__(self): |
|
16 | def __unicode__(self): | |
11 | return self.as_text() |
|
17 | return self.as_text() | |
@@ -23,6 +29,8 b' class PostForm(forms.Form):' | |||||
23 | text = forms.CharField(widget=forms.Textarea, required=False) |
|
29 | text = forms.CharField(widget=forms.Textarea, required=False) | |
24 | image = forms.ImageField(required=False) |
|
30 | image = forms.ImageField(required=False) | |
25 |
|
31 | |||
|
32 | session = None | |||
|
33 | ||||
26 | def clean_title(self): |
|
34 | def clean_title(self): | |
27 | title = self.cleaned_data['title'] |
|
35 | title = self.cleaned_data['title'] | |
28 | if title: |
|
36 | if title: | |
@@ -56,6 +64,9 b' class PostForm(forms.Form):' | |||||
56 | if not self.errors: |
|
64 | if not self.errors: | |
57 | self._clean_text_image() |
|
65 | self._clean_text_image() | |
58 |
|
66 | |||
|
67 | if not self.errors and self.session: | |||
|
68 | self._validate_posting_speed() | |||
|
69 | ||||
59 | return cleaned_data |
|
70 | return cleaned_data | |
60 |
|
71 | |||
61 | def _clean_text_image(self): |
|
72 | def _clean_text_image(self): | |
@@ -65,7 +76,26 b' class PostForm(forms.Form):' | |||||
65 | if (not text) and (not image): |
|
76 | if (not text) and (not image): | |
66 | error_message = 'Either text or image must be entered.' |
|
77 | error_message = 'Either text or image must be entered.' | |
67 | self._errors['text'] = self.error_class([error_message]) |
|
78 | self._errors['text'] = self.error_class([error_message]) | |
68 | self._errors['image'] = self.error_class([error_message]) |
|
79 | ||
|
80 | def _validate_posting_speed(self): | |||
|
81 | can_post = True | |||
|
82 | ||||
|
83 | if LAST_POST_TIME in self.session: | |||
|
84 | now = time.time() | |||
|
85 | last_post_time = self.session[LAST_POST_TIME] | |||
|
86 | ||||
|
87 | current_delay = int(now - last_post_time) | |||
|
88 | ||||
|
89 | if current_delay < settings.POSTING_DELAY: | |||
|
90 | error_message = 'Wait ' + str(settings.POSTING_DELAY - | |||
|
91 | current_delay)\ | |||
|
92 | + ' seconds after last posting' | |||
|
93 | self._errors['text'] = self.error_class([error_message]) | |||
|
94 | ||||
|
95 | can_post = False | |||
|
96 | ||||
|
97 | if can_post: | |||
|
98 | self.session[LAST_POST_TIME] = time.time() | |||
69 |
|
99 | |||
70 |
|
100 | |||
71 | class ThreadForm(PostForm): |
|
101 | class ThreadForm(PostForm): |
@@ -144,6 +144,9 b'' | |||||
144 | {{ form.captcha }} |
|
144 | {{ form.captcha }} | |
145 | <div class="form-errors">{{ form.captcha.errors }}</div> |
|
145 | <div class="form-errors">{{ form.captcha.errors }}</div> | |
146 | </div> |
|
146 | </div> | |
|
147 | <div class="form-row"> | |||
|
148 | <div class="form-errors">{{ form.other.errors }}</div> | |||
|
149 | </div> | |||
147 | </div> |
|
150 | </div> | |
148 | <div class="form-submit"> |
|
151 | <div class="form-submit"> | |
149 | <input type="submit" value="{% trans "Post" %}"/></div> |
|
152 | <input type="submit" value="{% trans "Post" %}"/></div> |
@@ -90,6 +90,9 b'' | |||||
90 | {{ form.captcha }} |
|
90 | {{ form.captcha }} | |
91 | <div class="form-errors">{{ form.captcha.errors }}</div> |
|
91 | <div class="form-errors">{{ form.captcha.errors }}</div> | |
92 | </div> |
|
92 | </div> | |
|
93 | <div class="form-row"> | |||
|
94 | <div class="form-errors">{{ form.other.errors }}</div> | |||
|
95 | </div> | |||
93 | </div> |
|
96 | </div> | |
94 |
|
97 | |||
95 | <div class="form-submit"><input type="submit" |
|
98 | <div class="form-submit"><input type="submit" |
@@ -28,6 +28,7 b' def index(request, page=0):' | |||||
28 | if request.method == 'POST': |
|
28 | if request.method == 'POST': | |
29 | form = threadFormClass(request.POST, request.FILES, |
|
29 | form = threadFormClass(request.POST, request.FILES, | |
30 | error_class=PlainErrorList, **kwargs) |
|
30 | error_class=PlainErrorList, **kwargs) | |
|
31 | form.session = request.session | |||
31 |
|
32 | |||
32 | if form.is_valid(): |
|
33 | if form.is_valid(): | |
33 | return _new_post(request, form) |
|
34 | return _new_post(request, form) | |
@@ -133,6 +134,8 b' def thread(request, post_id):' | |||||
133 | if request.method == 'POST': |
|
134 | if request.method == 'POST': | |
134 | form = postFormClass(request.POST, request.FILES, |
|
135 | form = postFormClass(request.POST, request.FILES, | |
135 | error_class=PlainErrorList, **kwargs) |
|
136 | error_class=PlainErrorList, **kwargs) | |
|
137 | form.session = request.session | |||
|
138 | ||||
136 | if form.is_valid(): |
|
139 | if form.is_valid(): | |
137 | return _new_post(request, form, post_id) |
|
140 | return _new_post(request, form, post_id) | |
138 | else: |
|
141 | else: |
@@ -199,3 +199,4 b' LAST_REPLIES_COUNT = 3' | |||||
199 | ENABLE_CAPTCHA = False |
|
199 | ENABLE_CAPTCHA = False | |
200 | # if user tries to post before CAPTCHA_DEFAULT_SAFE_TIME. Captcha will be shown |
|
200 | # if user tries to post before CAPTCHA_DEFAULT_SAFE_TIME. Captcha will be shown | |
201 | CAPTCHA_DEFAULT_SAFE_TIME = 30 # seconds |
|
201 | CAPTCHA_DEFAULT_SAFE_TIME = 30 # seconds | |
|
202 | POSTING_DELAY = 20 # seconds No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now