Commits

Anonymous committed 98f5f33

Abstracted retrieving remote IP

  • Participants
  • Parent commits 6e0edf0

Comments (0)

Files changed (1)

File gnocchi_blog/views.py

 class PostDetail(PostMixin, DateDetailView):
     template_name = 'blog/post_detail.html'
 
+    def _get_remote_ip(self, request):
+        try:
+            return request.META['HTTP_X_FORWARDED_FOR']
+        except KeyError:
+            return request.META['REMOTE_ADDR']
+
     def post(self, request, *args, **kwargs):
         self.object = self.get_object()
-        form = forms.CommentForm(request.META['REMOTE_ADDR'], request.POST)
+        form = forms.CommentForm(self._get_remote_ip(request), request.POST)
         if form.is_valid():
             comment = form.save(commit=False)
             comment.post = self.object
-            comment.ip_address = reqpoest.META['REMOTE_ADDR']
+            comment.ip_address = self._get_remote_ip(request)
             if request.user.is_authenticated():
                 comment.user = request.user
             comment.save()
     def get_context_data(self, **kwargs):
         context = super(PostDetail, self).get_context_data(**kwargs)
         if not self.request.method == 'POST':
-            context['comment_form'] = forms.CommentForm(self.request.META['REMOTE_ADDR'])
+            context['comment_form'] = forms.CommentForm(self._get_remote_ip(request))
         return context