Skip to content

Commit

Permalink
Opravené nastavenie IP adresy
Browse files Browse the repository at this point in the history
  • Loading branch information
mireq committed Jan 27, 2016
1 parent 0b4b066 commit 4d5f501
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions comments/views.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions common_utils/__init__.py
Expand Up @@ -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
19 changes: 19 additions & 0 deletions 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),
),
]
4 changes: 2 additions & 2 deletions forum/views.py
Expand Up @@ -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

0 comments on commit 4d5f501

Please sign in to comment.