Commits

Peter Sanchez  committed be25126

Added cleanup_callbacks management command. Version bump.

  • Participants
  • Parent commits 1979585

Comments (0)

Files changed (4)

File callback/__init__.py

 from base import CallbackException, CallbackBase
 
 
-__version__ = '0.1'
+__version__ = '0.2'
 __all__ = ['CallbackException', 'CallbackBase', 'callback_manager']

File callback/management/commands/cleanup_callbacks.py

+from optparse import OptionParser, make_option
+from django.conf import settings
+from django.core.management.base import BaseCommand
+from callback.models import CallbackMap
+
+
+class Command(BaseCommand):
+    help = 'Clean database of callback records.'
+
+    option_list = BaseCommand.option_list  + (
+        make_option(
+            '-e',
+            '--errors-delete',
+            dest='errors_delete',
+            action='store_true',
+            default=False,
+            help='Delete inactive callbacks that contain errors.',
+        ),
+        make_option(
+            '-d',
+            '--days',
+            dest='days',
+            type='int',
+            help='How many days back worth of callbacks should be kept?',
+        ),
+    )
+
+    def handle(self, *args, **kwargs):
+        errors_delete = kwargs.get('errors_delete', False)
+        days = kwargs.get('days', None)
+        if days is None:
+            days = getattr(settings, 'CALLBACK_KEEP_DAYS', 7)
+
+        # Sanity checks
+        if not isinstance(days, int):
+            days = int(days)
+
+        if not isinstance(errors_delete, bool):
+            errors_delete = bool(errors_delete)
+
+        CallbackMap.objects.all_expired_callbacks(days, errors_delete).delete()

File callback/managers.py

 import random
 import hashlib
+import datetime
 from django.db import models
 from django.contrib.auth.models import User
 
     def all_live_callbacks(self):
         query = Q(is_active=True) & Q(is_error=False)
         return self.get_query_set().filter(query)
+
+    def all_expired_callbacks(self, days=7, delete_errors=False):
+        use_date = datetime.date.today() - datetime.timedelta(days=days)
+        query = Q(is_active=False) 
+        if not delete_errors:
+            query &= Q(is_error=False)
+        query &= Q(updated_on__lte=use_date)
+        return self.get_query_set().filter(query)
 
 setup(
     name='django-callback',
-    version='0.1',
+    version='0.2',
     package_dir={project_name: project_name},
     packages=packages,
     package_data={project_name: data_files},