~nataliabidart/canonical-identity-provider/better-preferredemail

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright 2013 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

from datetime import datetime
from optparse import make_option

from django.core.management.base import BaseCommand
from django.utils.translation import ugettext as _

from identityprovider.utils import clean_sessions_until


class Command(BaseCommand):

    option_list = BaseCommand.option_list + (
        make_option('-l', '--limit', dest='limit', default=None,
                    action='store',
                    help='Number of rows to process per batch. Defaults to '
                    'None, which means no limit.'),
        make_option('-c', '--creation-date', dest='creation_date',
                    action='store',
                    help='Cleanup records created until this date. The format '
                    'should be %Y-%m-%d'),
        make_option('--dry-run', dest='dry_run', default=False,
                    action='store_true', help='Do not delete for real, just '
                    'print what would have been done.'),
    )
    help = _("""Clean sessions created until provided date.""")

    def handle(self, *args, **options):
        creation_date = datetime.strptime(options['creation_date'], '%Y-%m-%d')
        result = clean_sessions_until(creation_date, limit=options['limit'],
                                      dry_run=options['dry_run'])
        if result is not None:
            self.stdout.write(result)