summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-01 14:59:03 -0400
committerEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-01 14:59:03 -0400
commit16439f8b00ca909798eee0dadde901215634acab (patch)
treeb2695ba1a17df2bcf50c9a282aa5c27c873be9a2
parentb457bbfecd16875208c3484a75e5a976e0908bfc (diff)
downloadaskbot-16439f8b00ca909798eee0dadde901215634acab.tar.gz
askbot-16439f8b00ca909798eee0dadde901215634acab.tar.bz2
askbot-16439f8b00ca909798eee0dadde901215634acab.zip
simplified the clean_session command
-rw-r--r--askbot/management/commands/clean_session.py47
1 files changed, 17 insertions, 30 deletions
diff --git a/askbot/management/commands/clean_session.py b/askbot/management/commands/clean_session.py
index 6ba9352c..6800420b 100644
--- a/askbot/management/commands/clean_session.py
+++ b/askbot/management/commands/clean_session.py
@@ -1,13 +1,15 @@
+"""deletes expired sessions from the session database table
+works only when sessions are stored in the database
+"""
from django.core.management.base import NoArgsCommand
from django.contrib.sessions.models import Session
-from django.db import transaction
from optparse import make_option
-from askbot.utils.console import print_progress
+from askbot.utils.console import ProgressBar
from datetime import datetime
-DELETE_LIMIT = 1000
-
class Command(NoArgsCommand):
+ """Django management command class"""
+
option_list = NoArgsCommand.option_list + (
make_option('--quiet',
action='store_true',
@@ -17,34 +19,19 @@ class Command(NoArgsCommand):
),
)
- @transaction.commit_manually
def handle_noargs(self, **options):
- '''deletes old sessions'''
+ """deletes old sessions"""
quiet = options.get('quiet', False)
- expired_session_count = Session.objects.filter(expire_date__lt=datetime.now()).count()
- expired_session_list= Session.objects.filter(expire_date__lt=datetime.now()).values_list('session_key', flat=True)
- transaction.commit()
-
- if not quiet:
- print "There are %d expired sessions" % expired_session_count
-
- range_limit = len(expired_session_list) - 1
- higher_limit = lower_limit = 0
- for i in range(DELETE_LIMIT, range_limit, DELETE_LIMIT):
- lower_limit = i
- higher_limit = lower_limit + DELETE_LIMIT
- sublist = expired_session_list[lower_limit:higher_limit]
- Session.objects.filter(session_key__in = sublist).delete()
- transaction.commit()
- if not quiet:
- print_progress(higher_limit-1, expired_session_count)
+ expired_sessions = Session.objects.filter(
+ expire_date__lt=datetime.now()
+ )
+ count = expired_sessions.count()
+ expired_sessions = expired_sessions.iterator()
+ if quiet is False:
+ message = 'There are %d expired sessions' % count
+ expired_sessions = ProgressBar(expired_sessions, count, message)
- if higher_limit < expired_session_list:
- sublist = expired_session_list[higher_limit:expired_session_count]
- Session.objects.filter(session_key__in = sublist).delete()
- print_progress(expired_session_count, expired_session_count)
- transaction.commit()
+ for session in expired_sessions:
+ session.delete()
- if not quiet:
- print "sessions cleared"