summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan McGee <dpmcgee@gmail.com>2011-06-12 17:17:45 -0500
committerDan McGee <dpmcgee@gmail.com>2011-06-12 17:17:50 -0500
commit41f4914f577b3cfe12d463ee95b91d5217781da0 (patch)
treefcc765ebdcba560a89efe0fe2e81e5ec197066b6
parent15657eea7f273e7373eae8ffac7c5552885d3be2 (diff)
downloadwebsite-41f4914f577b3cfe12d463ee95b91d5217781da0.tar.gz
website-41f4914f577b3cfe12d463ee95b91d5217781da0.zip
Rewrite IPv6 addrs to IPv4 if possible
This allows my IP filtering stuff to work better, as well as allows editing of these comments through the admin interface without being a total pain in the ass. Signed-off-by: Dan McGee <dpmcgee@gmail.com>
-rw-r--r--mycomments/models.py18
-rw-r--r--mycomments/signals.py15
2 files changed, 29 insertions, 4 deletions
diff --git a/mycomments/models.py b/mycomments/models.py
index 007bcde..1728088 100644
--- a/mycomments/models.py
+++ b/mycomments/models.py
@@ -28,9 +28,19 @@ class BannedUserName(models.Model):
# connect our signals
-from django.contrib.comments.signals import comment_was_posted, comment_will_be_posted
-from mycomments.signals import send_comment_by_mail, check_spam_comment
-comment_was_posted.connect(send_comment_by_mail)
-comment_will_be_posted.connect(check_spam_comment)
+from django.contrib.comments.signals import (comment_was_posted,
+ comment_will_be_posted)
+from django.db.models.signals import pre_save
+from mycomments.signals import (send_comment_by_mail, check_spam_comment,
+ rewrite_ip_address)
+
+comment_was_posted.connect(send_comment_by_mail,
+ dispatch_uid='mycomments.models')
+comment_will_be_posted.connect(check_spam_comment,
+ dispatch_uid='mycomments.models')
+# Note: why I have to connect to 'Comment' and not 'MyComment' is beyond me and
+# probably a bug, but this does the job.
+pre_save.connect(rewrite_ip_address, sender=Comment,
+ dispatch_uid='mycomments.models')
# vim: set ts=4 sw=4 et:
diff --git a/mycomments/signals.py b/mycomments/signals.py
index ef1b6aa..0436ab9 100644
--- a/mycomments/signals.py
+++ b/mycomments/signals.py
@@ -66,4 +66,19 @@ def check_spam_comment(sender, **kwargs):
# mark the comment as non-public/bogus
comment.is_public = False
+def rewrite_ip_address(sender, **kwargs):
+ """
+ Coerce an IPv6 address to IPv4 if possible.
+ Examples:
+ ::ffff:1.2.3.4 --> 1.2.3.4
+ ::1 --> 127.0.0.1
+ """
+ comment = kwargs['instance']
+ addr = comment.ip_address
+ if addr.startswith('::ffff:'):
+ addr = addr[7:]
+ if addr == '::1':
+ addr = '127.0.0.1'
+ comment.ip_address = addr
+
# vim: set ts=4 sw=4 et: