diff --git a/boards/middlewares.py b/boards/middlewares.py new file mode 100644 --- /dev/null +++ b/boards/middlewares.py @@ -0,0 +1,17 @@ +from django.shortcuts import redirect +from boards import views, utils +from boards.models import Ban + + +class BanMiddleware: + """This is run before showing the thread. Banned users don't need to see + anything""" + + def process_view(self, request, view_func, view_args, view_kwargs): + + if view_func != views.you_are_banned: + ip = utils.get_client_ip(request) + is_banned = Ban.objects.filter(ip=ip).exists() + + if is_banned: + return redirect(views.you_are_banned) \ No newline at end of file diff --git a/boards/templates/boards/base.html b/boards/templates/boards/base.html --- a/boards/templates/boards/base.html +++ b/boards/templates/boards/base.html @@ -7,7 +7,7 @@ + href="{{ STATIC_URL }}css/{{ theme }}/base_page.css?4" media="all"/> diff --git a/boards/templates/boards/settings.html b/boards/templates/boards/settings.html --- a/boards/templates/boards/settings.html +++ b/boards/templates/boards/settings.html @@ -27,9 +27,9 @@
- {% endblock %} diff --git a/boards/utils.py b/boards/utils.py --- a/boards/utils.py +++ b/boards/utils.py @@ -62,3 +62,12 @@ def update_captcha_access(request, passe session[KEY_CAPTCHA_LAST_ACTIVITY] = int(time.time()) session[KEY_CAPTCHA_DELAY_TIME] = delay_time + + +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') + return ip \ No newline at end of file diff --git a/boards/views.py b/boards/views.py --- a/boards/views.py +++ b/boards/views.py @@ -14,6 +14,7 @@ from boards.forms import ThreadForm, Pos from boards.models import Post, Tag, Ban, User, RANK_USER, SETTING_MODERATE from boards import authors +from boards.utils import get_client_ip import neboard @@ -53,8 +54,8 @@ def index(request, page=0): def _new_post(request, form, thread_id=boards.models.NO_PARENT): """Add a new post (in thread or as a reply).""" - ip = _get_client_ip(request) - is_banned = Ban.objects.filter(ip=ip).count() > 0 + ip = get_client_ip(request) + is_banned = Ban.objects.filter(ip=ip).exists() if is_banned: return redirect(you_are_banned) @@ -319,15 +320,6 @@ def _get_theme(request, user=None): return theme -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') - return ip - - def _init_default_context(request): """Create context with default values that are used in most views""" diff --git a/neboard/settings.py b/neboard/settings.py --- a/neboard/settings.py +++ b/neboard/settings.py @@ -117,7 +117,8 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', # Uncomment the next line for simple clickjacking protection: - # 'django.middleware.clickjacking.XFrameOptionsMiddleware', + # 'django.middleware.clickjacking.XFrameOptionsMiddleware' + 'boards.middlewares.BanMiddleware', ) ROOT_URLCONF = 'neboard.urls'