be251264059b — Peter Sanchez 13 years ago
Added cleanup_callbacks management command. Version bump.
4 files changed, 52 insertions(+), 2 deletions(-)

M callback/__init__.py
A => callback/management/commands/cleanup_callbacks.py
M callback/managers.py
M setup.py
M callback/__init__.py +1 -1
@@ 2,5 2,5 @@ from callback import callback_manager
 from base import CallbackException, CallbackBase
 
 
-__version__ = '0.1'
+__version__ = '0.2'
 __all__ = ['CallbackException', 'CallbackBase', 'callback_manager']

          
A => callback/management/commands/cleanup_callbacks.py +41 -0
@@ 0,0 1,41 @@ 
+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()

          
M callback/managers.py +9 -0
@@ 1,5 1,6 @@ 
 import random
 import hashlib
+import datetime
 from django.db import models
 from django.contrib.auth.models import User
 

          
@@ 21,3 22,11 @@ class CallbackMapManager(models.Manager)
     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)

          
M setup.py +1 -1
@@ 28,7 28,7 @@ for dirpath, dirnames, filenames in os.w
 
 setup(
     name='django-callback',
-    version='0.1',
+    version='0.2',
     package_dir={project_name: project_name},
     packages=packages,
     package_data={project_name: data_files},