PageRenderTime 34ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/comment_utils/bin/delete_spam_comments.py

http://django-comment-utils.googlecode.com/
Python | 93 lines | 86 code | 1 blank | 6 comment | 0 complexity | 97bc24b6ed29fa2d2b6eb472c46a8f82 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. A script which removes spam comments from the database.
  3. The determination of which comments are spam is based on two pieces of
  4. information:
  5. 1. The comment's ``is_public`` field, and
  6. 2. The comment's age.
  7. If the ``is_public`` field is set to ``False`` and the comment is
  8. older than a specified threshold (see below), it will be considered
  9. spam and will be deleted.
  10. **Arguments:**
  11. ``-a``, ``--age=AGE``
  12. The age threshold, in days, past which a non-public comment will
  13. be considered spam, and thus deleted. Defaults to 14 if not
  14. supplied (and thus will delete non-public comments which are at
  15. least two weeks old).
  16. ``-d``, ``--dry-run``
  17. Does not delete any comments, but merely prints the number of
  18. comments which would have been deleted.
  19. ``-s``, ``--settings=SETTINGS``
  20. Django settings module to use. This argument is required.
  21. ``-t``, ``--type=TYPE``
  22. The type of comment to perform deletion on; ``free`` will use the
  23. ``FreeComment`` model, and ``registered`` will use the ``Comment``
  24. model. Defaults to ``free`` if not specified.
  25. ``-v``, ``--verbose``
  26. Run verbosely, printing information to standard output about each
  27. comment as it is deleted.
  28. Regardless of the ``verbose``flag, this script will print the total
  29. number of deleted comments to standard output when finished.
  30. This script is intended to be run as a cron job; for example, to have
  31. it run at midnight each Sunday, with default values::
  32. 0 0 * sun python /path/to/comment_utils/bin/delete_spam_comments.py --settings=yoursite.settings
  33. """
  34. import datetime, os
  35. from optparse import OptionParser
  36. def delete_spam_comments(age, dry_run, type, verbose):
  37. from django.contrib.comments import models
  38. comment_model = { 'free': models.FreeComment,
  39. 'registered': models.Comment }[type]
  40. age_cutoff = datetime.datetime.now() - datetime.timedelta(days=age)
  41. comments_to_delete = comment_model.objects.filter(is_public__exact=False,
  42. submit_date__lt=age_cutoff)
  43. deleted_count = comments_to_delete.count()
  44. if not dry_run:
  45. for comment in comments_to_delete:
  46. if verbose:
  47. print "Deleting spam comment '%s' on '%s', from %s" % (comment,
  48. comment.get_content_object(),
  49. comment.submit_date.strftime("%Y-%m-%d"))
  50. comment.delete()
  51. print "Deleted %s spam comments" % deleted_count
  52. if __name__ == '__main__':
  53. usage = "usage: %prog --settings=settings [options]"
  54. parser = OptionParser(usage)
  55. parser.add_option('-a', '--age', dest='age', metavar='AGE', type='int',
  56. help="The age threshold, in days, past which a non-public comment will be considered spam, and thus be deleted. Defaults to 14 if not supplied.")
  57. parser.add_option('-d', '--dry-run', action="store_true", dest="dry_run",
  58. help="Does not delete any comments, but merely outputs the number of comments which would have been deleted.")
  59. parser.add_option('-s', '--settings', dest='settings', metavar='SETTINGS',
  60. help="Django settings module to use. This argument is required.")
  61. parser.add_option('-t', '--type', dest='type', metavar='TYPE', type='choice', choices=('free', 'registered'),
  62. help="The type of comment to perform deletion on; 'free' will use the 'FreeComment' model, and 'registered' will use the 'Comment' model. Defaults to 'free' if not specified.")
  63. parser.add_option('-v', '--verbose', dest='verbose', metavar='VERBOSE', action='store_true',
  64. help="Run verbosely, printing information to standard output about each comment as it is deleted.")
  65. (options, args) = parser.parse_args()
  66. if not options.settings:
  67. parser.error("You must specify a settings module.")
  68. os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
  69. age = options.age or 14
  70. dry_run = options.dry_run or False
  71. comment_type = options.type or 'free'
  72. verbose = options.verbose or False
  73. delete_spam_comments(age=age, dry_run=dry_run,
  74. type=comment_type, verbose=verbose)