Revision 08494423 snf-astakos-app/astakos/im/management/commands/service-token-renew.py

b/snf-astakos-app/astakos/im/management/commands/service-token-renew.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from optparse import make_option
35
from datetime import datetime, timedelta
36

  
34 37
from django.core.management.base import BaseCommand, CommandError
35 38

  
36 39
from astakos.im.models import Service
......
40 43
    args = "<name>"
41 44
    help = "Renew service token"
42 45

  
46
    option_list = BaseCommand.option_list + (
47
        make_option('--duration',
48
                    dest='duration',
49
                    metavar='NUM',
50
                    help="In how many days token is going to expire."),)
51

  
43 52
    def handle(self, *args, **options):
44 53
        if len(args) != 1:
45 54
            raise CommandError("Invalid number of arguments")
46 55

  
47 56
        try:
48 57
            service = Service.objects.get(name=args[0])
49
            service.renew_token()
58
            expires = None
59
            if options['duration']:
60
                try:
61
                    duration = int(options['duration'])
62
                except ValueError, e:
63
                    raise CommandError('Invalid duration')
64
                expires = datetime.now() + timedelta(days=duration)
65
            service.renew_token(expires)
50 66
            service.save()
51 67
            self.stdout.write('New service token: %s\n' % service.auth_token)
52 68
        except Service.DoesNotExist:

Also available in: Unified diff