diff --git a/comments/views.py b/comments/views.py index a10f2bb5b..ff1ee59f0 100644 --- a/comments/views.py +++ b/comments/views.py @@ -15,7 +15,7 @@ from .models import Comment from .utils import update_comments_header from comments.models import RootHeader, UserDiscussionAttribute -from common_utils import get_meta +from common_utils import get_meta, get_client_ip def get_module_name(content_object): @@ -84,7 +84,7 @@ def form_valid(self, form): return TemplateResponse(self.request, 'comments/error.html', self.get_context_data(form=form)) comment = form.get_comment_object() - comment.ip_address = self.request.META.get('REMOTE_ADDR', None) + comment.ip_address = get_client_ip(self.request) if self.request.user.is_authenticated(): comment.user = self.request.user diff --git a/common_utils/__init__.py b/common_utils/__init__.py index a7ffa87ce..58b8f3a9c 100644 --- a/common_utils/__init__.py +++ b/common_utils/__init__.py @@ -56,3 +56,12 @@ def get_default_manager(obj): def reload_model(obj): return get_default_manager(obj.__class__).get(pk=obj.pk) + + +def get_client_ip(request): + x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') + if x_forwarded_for: + ip = x_forwarded_for.split(',')[0] + else: + ip = request.META.get('REMOTE_ADDR') + return ip diff --git a/forum/migrations/0002_topic_ip_address.py b/forum/migrations/0002_topic_ip_address.py new file mode 100644 index 000000000..c915bb6aa --- /dev/null +++ b/forum/migrations/0002_topic_ip_address.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('forum', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='topic', + name='ip_address', + field=models.GenericIPAddressField(null=True, verbose_name='IP adresa', blank=True), + ), + ] diff --git a/forum/views.py b/forum/views.py index da8a2b8d7..0e49af912 100644 --- a/forum/views.py +++ b/forum/views.py @@ -45,7 +45,7 @@ class TopicCreateView(PreviewCreateView): form_class = TopicForm def form_valid(self, form): - ret = super(TopicCreateView, self).form_valid(form) + response = super(TopicCreateView, self).form_valid(form) if self.object: form.move_attachments(self.object) - return ret + return response