Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hitcount_cleanup.py 1004B

1234567891011121314151617181920212223242526272829303132
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from datetime import timedelta
  4. from django.conf import settings
  5. from django.utils import timezone
  6. try:
  7. from django.core.management.base import BaseCommand
  8. except ImportError:
  9. from django.core.management.base import NoArgsCommand as BaseCommand
  10. from hitcount.models import Hit
  11. class Command(BaseCommand):
  12. help = "Can be run as a cronjob or directly to clean out old Hits objects from the database."
  13. def __init__(self, *args, **kwargs):
  14. super(Command, self).__init__(*args, **kwargs)
  15. def handle(self, *args, **kwargs):
  16. self.handle_noargs()
  17. def handle_noargs(self, **options):
  18. grace = getattr(settings, 'HITCOUNT_KEEP_HIT_IN_DATABASE', {'days': 30})
  19. period = timezone.now() - timedelta(**grace)
  20. qs = Hit.objects.filter(created__lt=period)
  21. number_removed = qs.count()
  22. qs.delete()
  23. self.stdout.write('Successfully removed %s Hits' % number_removed)