diff options
author | Dan McGee <dpmcgee@gmail.com> | 2011-06-12 17:17:45 -0500 |
---|---|---|
committer | Dan McGee <dpmcgee@gmail.com> | 2011-06-12 17:17:50 -0500 |
commit | 41f4914f577b3cfe12d463ee95b91d5217781da0 (patch) | |
tree | fcc765ebdcba560a89efe0fe2e81e5ec197066b6 | |
parent | 15657eea7f273e7373eae8ffac7c5552885d3be2 (diff) | |
download | website-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.py | 18 | ||||
-rw-r--r-- | mycomments/signals.py | 15 |
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: |