Show More
@@ -4,7 +4,7 b' from django import forms' | |||||
4 | from django.forms.util import ErrorList |
|
4 | from django.forms.util import ErrorList | |
5 | from boards.models import TITLE_MAX_LENGTH |
|
5 | from boards.models import TITLE_MAX_LENGTH | |
6 | from neboard import settings |
|
6 | from neboard import settings | |
7 |
|
7 | from boards import utils | ||
8 |
|
8 | |||
9 | class PlainErrorList(ErrorList): |
|
9 | class PlainErrorList(ErrorList): | |
10 | def __unicode__(self): |
|
10 | def __unicode__(self): | |
@@ -67,9 +67,6 b' class PostForm(forms.Form):' | |||||
67 | self._errors['image'] = self.error_class([error_message]) |
|
67 | self._errors['image'] = self.error_class([error_message]) | |
68 |
|
68 | |||
69 |
|
69 | |||
70 | class PostCaptchaForm(PostForm): |
|
|||
71 | captcha = CaptchaField() |
|
|||
72 |
|
||||
73 |
|
70 | |||
74 | class ThreadForm(PostForm): |
|
71 | class ThreadForm(PostForm): | |
75 | regex_tags = re.compile(ur'^[\w\s\d]+$', re.UNICODE) |
|
72 | regex_tags = re.compile(ur'^[\w\s\d]+$', re.UNICODE) | |
@@ -91,9 +88,47 b' class ThreadForm(PostForm):' | |||||
91 | return cleaned_data |
|
88 | return cleaned_data | |
92 |
|
89 | |||
93 |
|
90 | |||
|
91 | class PostCaptchaForm(PostForm): | |||
|
92 | captcha = CaptchaField() | |||
|
93 | ||||
|
94 | def __init__(self, *args, **kwargs): | |||
|
95 | self.request = kwargs['request'] | |||
|
96 | del kwargs['request'] | |||
|
97 | ||||
|
98 | super(PostCaptchaForm, self).__init__(*args, **kwargs) | |||
|
99 | ||||
|
100 | def clean(self): | |||
|
101 | cleaned_data = super(PostCaptchaForm, self).clean() | |||
|
102 | ||||
|
103 | success = self.is_valid() | |||
|
104 | utils.update_captcha_access(self.request, success) | |||
|
105 | ||||
|
106 | if success: | |||
|
107 | return cleaned_data | |||
|
108 | else: | |||
|
109 | raise forms.ValidationError("captcha validation failed") | |||
|
110 | ||||
|
111 | ||||
94 | class ThreadCaptchaForm(ThreadForm): |
|
112 | class ThreadCaptchaForm(ThreadForm): | |
95 | captcha = CaptchaField() |
|
113 | captcha = CaptchaField() | |
96 |
|
114 | |||
|
115 | def __init__(self, *args, **kwargs): | |||
|
116 | self.request = kwargs['request'] | |||
|
117 | del kwargs['request'] | |||
|
118 | ||||
|
119 | super(ThreadCaptchaForm, self).__init__(*args, **kwargs) | |||
|
120 | ||||
|
121 | def clean(self): | |||
|
122 | cleaned_data = super(ThreadCaptchaForm, self).clean() | |||
|
123 | ||||
|
124 | success = self.is_valid() | |||
|
125 | utils.update_captcha_access(self.request, success) | |||
|
126 | ||||
|
127 | if success: | |||
|
128 | return cleaned_data | |||
|
129 | else: | |||
|
130 | raise forms.ValidationError("captcha validation failed") | |||
|
131 | ||||
97 |
|
132 | |||
98 | class SettingsForm(forms.Form): |
|
133 | class SettingsForm(forms.Form): | |
99 | theme = forms.ChoiceField(choices=settings.THEMES, widget=forms.RadioSelect) No newline at end of file |
|
134 | theme = forms.ChoiceField(choices=settings.THEMES, widget=forms.RadioSelect) |
@@ -8,6 +8,7 b' from django.db import models' | |||||
8 | from django.http import Http404 |
|
8 | from django.http import Http404 | |
9 | from django.utils import timezone |
|
9 | from django.utils import timezone | |
10 | from markupfield.fields import MarkupField |
|
10 | from markupfield.fields import MarkupField | |
|
11 | from threading import Thread | |||
11 |
|
12 | |||
12 | from neboard import settings |
|
13 | from neboard import settings | |
13 | import thumbs |
|
14 | import thumbs |
@@ -130,6 +130,7 b'' | |||||
130 | </div> |
|
130 | </div> | |
131 | <div class="form-row"> |
|
131 | <div class="form-row"> | |
132 | {{ form.captcha }} |
|
132 | {{ form.captcha }} | |
|
133 | <div class="form-errors">{{ form.captcha.errors }}</div> | |||
133 | </div> |
|
134 | </div> | |
134 | </div> |
|
135 | </div> | |
135 | <div class="form-submit"> |
|
136 | <div class="form-submit"> |
@@ -84,6 +84,7 b'' | |||||
84 | </div> |
|
84 | </div> | |
85 | <div class="form-row"> |
|
85 | <div class="form-row"> | |
86 | {{ form.captcha }} |
|
86 | {{ form.captcha }} | |
|
87 | <div class="form-errors">{{ form.captcha.errors }}</div> | |||
87 | </div> |
|
88 | </div> | |
88 | </div> |
|
89 | </div> | |
89 |
|
90 |
@@ -1,14 +1,64 b'' | |||||
1 | """ |
|
1 | """ | |
2 | This module contains helper functions and helper classes. |
|
2 | This module contains helper functions and helper classes. | |
3 | """ |
|
3 | """ | |
|
4 | ||||
4 | from neboard import settings |
|
5 | from neboard import settings | |
|
6 | import time | |||
5 |
|
7 | |||
6 |
|
8 | |||
7 | def check_if_human(request): |
|
9 | KEY_CAPTCHA_FAILS = 'key_captcha_fails' | |
|
10 | KEY_CAPTCHA_DELAY_TIME = 'key_captcha_delay_time' | |||
|
11 | KEY_CAPTCHA_LAST_ACTIVITY = 'key_captcha_last_activity' | |||
|
12 | ||||
|
13 | ||||
|
14 | def need_include_captcha(request): | |||
8 | """ |
|
15 | """ | |
9 | Check if request is made by a user. |
|
16 | Check if request is made by a user. | |
10 | It contains rules which check for bots. |
|
17 | It contains rules which check for bots. | |
11 | """ |
|
18 | """ | |
12 |
|
19 | |||
13 | # FIXME: need to insert checking logic |
|
20 | if not settings.ENABLE_CAPTCHA: | |
14 | return not settings.ENABLE_CAPTCHA |
|
21 | return False | |
|
22 | ||||
|
23 | enable_captcha = False | |||
|
24 | ||||
|
25 | #newcomer | |||
|
26 | if KEY_CAPTCHA_LAST_ACTIVITY not in request.session: | |||
|
27 | return settings.ENABLE_CAPTCHA | |||
|
28 | ||||
|
29 | last_activity = request.session[KEY_CAPTCHA_LAST_ACTIVITY] | |||
|
30 | current_delay = int(time.time()) - last_activity | |||
|
31 | ||||
|
32 | delay_time = (request.session[KEY_CAPTCHA_DELAY_TIME] | |||
|
33 | if KEY_CAPTCHA_DELAY_TIME in request.session | |||
|
34 | else settings.CAPTCHA_DEFAULT_SAFE_TIME) | |||
|
35 | ||||
|
36 | if current_delay < delay_time: | |||
|
37 | enable_captcha = True | |||
|
38 | ||||
|
39 | print 'ENABLING' + str(enable_captcha) | |||
|
40 | ||||
|
41 | return enable_captcha | |||
|
42 | ||||
|
43 | ||||
|
44 | def update_captcha_access(request, passed): | |||
|
45 | """ | |||
|
46 | Update captcha fields. | |||
|
47 | It will reduce delay time if user passed captcha verification and | |||
|
48 | it will increase it otherwise. | |||
|
49 | """ | |||
|
50 | session = request.session | |||
|
51 | ||||
|
52 | delay_time = (request.session[KEY_CAPTCHA_DELAY_TIME] | |||
|
53 | if KEY_CAPTCHA_DELAY_TIME in request.session | |||
|
54 | else settings.CAPTCHA_DEFAULT_SAFE_TIME) | |||
|
55 | ||||
|
56 | print "DELAY TIME = " + str(delay_time) | |||
|
57 | ||||
|
58 | if passed: | |||
|
59 | delay_time -= 2 if delay_time >= 7 else 5 | |||
|
60 | else: | |||
|
61 | delay_time += 10 | |||
|
62 | ||||
|
63 | session[KEY_CAPTCHA_LAST_ACTIVITY] = int(time.time()) | |||
|
64 | session[KEY_CAPTCHA_DELAY_TIME] = delay_time |
@@ -16,18 +16,21 b' import neboard' | |||||
16 | def index(request, page=0): |
|
16 | def index(request, page=0): | |
17 | context = RequestContext(request) |
|
17 | context = RequestContext(request) | |
18 |
|
18 | |||
19 | threadFormClass = (ThreadForm |
|
19 | if utils.need_include_captcha(request): | |
20 | if utils.check_if_human(request) |
|
20 | threadFormClass = ThreadCaptchaForm | |
21 | else ThreadCaptchaForm) |
|
21 | kwargs = {'request': request} | |
|
22 | else: | |||
|
23 | threadFormClass = ThreadForm | |||
|
24 | kwargs = {} | |||
22 |
|
25 | |||
23 | if request.method == 'POST': |
|
26 | if request.method == 'POST': | |
24 | form = threadFormClass(request.POST, request.FILES, |
|
27 | form = threadFormClass(request.POST, request.FILES, | |
25 | error_class=PlainErrorList) |
|
28 | error_class=PlainErrorList, **kwargs) | |
26 |
|
29 | |||
27 | if form.is_valid(): |
|
30 | if form.is_valid(): | |
28 | return _new_post(request, form) |
|
31 | return _new_post(request, form) | |
29 | else: |
|
32 | else: | |
30 | form = threadFormClass(error_class=PlainErrorList) |
|
33 | form = threadFormClass(error_class=PlainErrorList, **kwargs) | |
31 |
|
34 | |||
32 | threads = Post.objects.get_threads(page=int(page)) |
|
35 | threads = Post.objects.get_threads(page=int(page)) | |
33 |
|
36 | |||
@@ -119,16 +122,20 b' def tag(request, tag_name, page=0):' | |||||
119 | def thread(request, post_id): |
|
122 | def thread(request, post_id): | |
120 | """Get all thread posts""" |
|
123 | """Get all thread posts""" | |
121 |
|
124 | |||
122 | postFormClass = (PostForm if utils.check_if_human(request) else |
|
125 | if utils.need_include_captcha(request): | |
123 |
|
|
126 | postFormClass = PostCaptchaForm | |
|
127 | kwargs = {'request': request} | |||
|
128 | else: | |||
|
129 | postFormClass = PostForm | |||
|
130 | kwargs = {} | |||
124 |
|
131 | |||
125 | if request.method == 'POST': |
|
132 | if request.method == 'POST': | |
126 | form = postFormClass(request.POST, request.FILES, |
|
133 | form = postFormClass(request.POST, request.FILES, | |
127 | error_class=PlainErrorList) |
|
134 | error_class=PlainErrorList, **kwargs) | |
128 | if form.is_valid(): |
|
135 | if form.is_valid(): | |
129 | return _new_post(request, form, post_id) |
|
136 | return _new_post(request, form, post_id) | |
130 | else: |
|
137 | else: | |
131 | form = postFormClass(error_class=PlainErrorList) |
|
138 | form = postFormClass(error_class=PlainErrorList, **kwargs) | |
132 |
|
139 | |||
133 | posts = Post.objects.get_thread(post_id) |
|
140 | posts = Post.objects.get_thread(post_id) | |
134 |
|
141 |
@@ -187,9 +187,12 b" SITE_NAME = 'Neboard'" | |||||
187 | THEMES = [ |
|
187 | THEMES = [ | |
188 | ('md', 'Mystic Dark'), |
|
188 | ('md', 'Mystic Dark'), | |
189 | ('sw', 'Snow White') ] |
|
189 | ('sw', 'Snow White') ] | |
|
190 | ||||
190 | DEFAULT_THEME = 'md' |
|
191 | DEFAULT_THEME = 'md' | |
191 |
|
192 | |||
192 | POPULAR_TAGS = 10 |
|
193 | POPULAR_TAGS = 10 | |
193 | LAST_REPLIES_COUNT = 3 |
|
194 | LAST_REPLIES_COUNT = 3 | |
194 |
|
195 | |||
195 |
ENABLE_CAPTCHA = |
|
196 | ENABLE_CAPTCHA = True | |
|
197 | # if user tries to post before CAPTCHA_DEFAULT_SAFE_TIME. Captcha will be shown | |||
|
198 | CAPTCHA_DEFAULT_SAFE_TIME = 30 # seconds No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now