summaryrefslogtreecommitdiffstats
path: root/askbot/management
diff options
context:
space:
mode:
authorEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-06 11:37:33 -0400
committerEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-06 11:37:33 -0400
commitcc3ebc871f8da4329aad5db65e960b052e8f8834 (patch)
tree450ce91dcc2de37b9837de6e4db94965f77d07dc /askbot/management
parentc46cbfb2e5bf1d28a5fbfbcf156e5d3509241316 (diff)
parent996507782f455c17afcf68667ad83c963a9179c7 (diff)
downloadaskbot-cc3ebc871f8da4329aad5db65e960b052e8f8834.tar.gz
askbot-cc3ebc871f8da4329aad5db65e960b052e8f8834.tar.bz2
askbot-cc3ebc871f8da4329aad5db65e960b052e8f8834.zip
merged with adolfos master branch
Diffstat (limited to 'askbot/management')
-rw-r--r--askbot/management/commands/clean_session.py52
1 files changed, 24 insertions, 28 deletions
diff --git a/askbot/management/commands/clean_session.py b/askbot/management/commands/clean_session.py
index 6ba9352c..2e663b22 100644
--- a/askbot/management/commands/clean_session.py
+++ b/askbot/management/commands/clean_session.py
@@ -1,13 +1,18 @@
+"""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
+ITEMS_PER_TRANSACTION = 1000
class Command(NoArgsCommand):
+ """Django management command class"""
+
option_list = NoArgsCommand.option_list + (
make_option('--quiet',
action='store_true',
@@ -19,32 +24,23 @@ 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
+ 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)
+
+ deleted_count = 0
+ for session in expired_sessions:
+ session.delete()
+ deleted_count += 1
+ if deleted_count % ITEMS_PER_TRANSACTION == 0:
+ transaction.commit()
- 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)
-
- 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()
-
- if not quiet:
- print "sessions cleared"
+ transaction.commit()