##// END OF EJS Templates
Fixed thread update
Fixed thread update

File last commit:

r690:a8dffe47 1.8-dev
r710:3be7d3c8 default
Show More
utils.py
128 lines | 3.3 KiB | text/x-python | PythonLexer
Ilyas
Added django-simple-capthca support...
r78 """
This module contains helper functions and helper classes.
"""
neko259
Implemented search over posts. Moved get_user and get_theme to utils module. Use context processors instead of creating context in the base view. Removed unused imports in some modules
r690 import hashlib
neko259
Rewriting views to class-based
r542 from django.utils import timezone
wnc_21
Added captcha support
r95
neko259
Fixed captcha design. Added setting to enable or disable captcha. This refs #39
r81 from neboard import settings
neko259
Implemented search over posts. Moved get_user and get_theme to utils module. Use context processors instead of creating context in the base view. Removed unused imports in some modules
r690 from boards.models import Post, User
from boards.models.post import SETTING_MODERATE
from boards.models.user import RANK_USER
wnc_21
Added captcha support
r95 import time
neko259
Implemented search over posts. Moved get_user and get_theme to utils module. Use context processors instead of creating context in the base view. Removed unused imports in some modules
r690 import neboard
Ilyas
Added django-simple-capthca support...
r78
wnc_21
Added captcha support
r95 KEY_CAPTCHA_FAILS = 'key_captcha_fails'
KEY_CAPTCHA_DELAY_TIME = 'key_captcha_delay_time'
KEY_CAPTCHA_LAST_ACTIVITY = 'key_captcha_last_activity'
def need_include_captcha(request):
Ilyas
Added django-simple-capthca support...
r78 """
Check if request is made by a user.
It contains rules which check for bots.
"""
wnc_21
Added captcha support
r95 if not settings.ENABLE_CAPTCHA:
return False
enable_captcha = False
#newcomer
if KEY_CAPTCHA_LAST_ACTIVITY not in request.session:
return settings.ENABLE_CAPTCHA
last_activity = request.session[KEY_CAPTCHA_LAST_ACTIVITY]
current_delay = int(time.time()) - last_activity
delay_time = (request.session[KEY_CAPTCHA_DELAY_TIME]
if KEY_CAPTCHA_DELAY_TIME in request.session
else settings.CAPTCHA_DEFAULT_SAFE_TIME)
if current_delay < delay_time:
enable_captcha = True
print 'ENABLING' + str(enable_captcha)
return enable_captcha
def update_captcha_access(request, passed):
"""
Update captcha fields.
It will reduce delay time if user passed captcha verification and
it will increase it otherwise.
"""
session = request.session
delay_time = (request.session[KEY_CAPTCHA_DELAY_TIME]
if KEY_CAPTCHA_DELAY_TIME in request.session
else settings.CAPTCHA_DEFAULT_SAFE_TIME)
print "DELAY TIME = " + str(delay_time)
if passed:
delay_time -= 2 if delay_time >= 7 else 5
else:
delay_time += 10
session[KEY_CAPTCHA_LAST_ACTIVITY] = int(time.time())
session[KEY_CAPTCHA_DELAY_TIME] = delay_time
neko259
Added ban middleware. Now banned user's won't cause load to the server.
r210
def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[-1].strip()
else:
ip = request.META.get('REMOTE_ADDR')
neko259
Rewriting views to class-based
r542 return ip
def datetime_to_epoch(datetime):
return int(time.mktime(timezone.localtime(
datetime,timezone.get_current_timezone()).timetuple())
neko259
Implemented search over posts. Moved get_user and get_theme to utils module. Use context processors instead of creating context in the base view. Removed unused imports in some modules
r690 * 1000000 + datetime.microsecond)
def get_user(request):
"""
Get current user from the session. If the user does not exist, create
a new one.
"""
session = request.session
if not 'user_id' in session:
request.session.save()
md5 = hashlib.md5()
md5.update(session.session_key)
new_id = md5.hexdigest()
while User.objects.filter(user_id=new_id).exists():
md5.update(str(timezone.now()))
new_id = md5.hexdigest()
time_now = timezone.now()
user = User.objects.create(user_id=new_id, rank=RANK_USER,
registration_time=time_now)
session['user_id'] = user.id
else:
user = User.objects.select_related('fav_tags').get(
id=session['user_id'])
return user
def get_theme(request, user=None):
"""
Get user's CSS theme
"""
if not user:
user = get_user(request)
theme = user.get_setting('theme')
if not theme:
theme = neboard.settings.DEFAULT_THEME
return theme