<commit_before>from datetime import datetime

from django.core.management.base import BaseCommand
from django.contrib.sessions.models import Session

"""
>>> def clean(count):
...   for idx, s in enumerate(Session.objects.filter(expire_date__lt=now)[:count+1]):
...     s.delete()
...     if str(idx).endswith('000'): print idx
...   print "{0} records left".format(Session.objects.filter(expire_date__lt=now).count())
... 
"""

class Command(NoArgsCommand):

    args = '<count count ...>'
    help = "Delete old sessions"

    def handle(self, *args, **options):
        old_sessions = Session.objects.filter(expire_date__lt=datetime.now())

        self.stdout.write("Deleting {0} expired sessions".format(
                old_sessions.count()
            )
        )

        for index, session in enumerate(old_sessions):
            session.delete()
            if str(idx).endswith('000'):
                self.stdout.write("{0} records deleted".format(index))

        self.stdout.write("{0} expired sessions remaining".format(
                Session.objects.filter(expire_date__lt=datetime.now())
            )
        )<commit_msg>Add delete old sessions command<commit_after>from datetime import datetime

from django.core.management.base import BaseCommand
from django.contrib.sessions.models import Session

"""
>>> def clean(count):
...   for idx, s in enumerate(Session.objects.filter(expire_date__lt=now)[:count+1]):
...     s.delete()
...     if str(idx).endswith('000'): print idx
...   print "{0} records left".format(Session.objects.filter(expire_date__lt=now).count())
... 
"""

class Command(BaseCommand):

    args = '<count count ...>'
    help = "Delete old sessions"

    def handle(self, *args, **options):
        old_sessions = Session.objects.filter(expire_date__lt=datetime.now())

        self.stdout.write("Deleting {0} expired sessions".format(
                old_sessions.count()
            )
        )

        for index, session in enumerate(old_sessions):
            session.delete()
            if str(idx).endswith('000'):
                self.stdout.write("{0} records deleted".format(index))

        self.stdout.write("{0} expired sessions remaining".format(
                Session.objects.filter(expire_date__lt=datetime.now())
            )
        )
{'fraction_non_alphanumeric': 0.14347826086956522, 'fraction_numerical': 0.009565217391304347, 'mean_word_length': 2.7293354943273904, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3305986', 'n_tokens_mistral': 688, 'n_tokens_neox': 648, 'n_words': 132}