Sex Comments. How to delete them from Mezzanine

Sex Comments: How to delete them from Mezzanine

Hey, I was receiving a lot of comments in this blog(created in Mezzanine, Django) from people that wants to get higher page rank. Most of the comments were about sex pills. I will show you how I got rid of them with an script.

So I created a bin folder in my app that contains this script:

#!/usr/bin/env python

import os
import sys

APP_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)))
BASE_DIR = os.path.dirname(APP_DIR)

sys.path.append(APP_DIR)
sys.path.append(BASE_DIR)
#
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api_builder.settings')

import django
django.setup()

from django.db.models import Q
from mezzanine.generic.models import ThreadedComment


def delete_sex_pill_comments():
qs_filter = Q()
bad = ['levitra', 'cialis', 'viagra', 'prescription', 'buy', 'cheap', 'erectile', 'dysfuntion']
for b in bad:
qs_filter |= Q(comment__contains=b)
qs = ThreadedComment.objects.filter(qs_filter)
print "deleting: {} comments".format(qs.count())
qs.delete()


def main():
delete_sex_pill_comments()

if __name__ == '__main__':
main()

As you can see the cript is very simple, having a list of banned words we just create a query that will find all the comments containing these words. Then we print how many we are going to delete, just for a test. An we procced to delete them all.

So then I just created a cron job to run this script every day at 5 AM.

0 5 * * * cd <PROJECT_DIR> && python bin/delete_spam_comments.py

Where PROJECT_DIR needs to be changed by your real project folder and then run the script. In my case I am running virtualenv so really I used the python env according to my app, but I guess you can figure it out.

I hope this little script can help to others folks havng the same issues with Mezzanine and the crazy guys out there in the wild.

Currently unrated