Revision 42f0a495 snf-astakos-app/astakos/im/management/commands/service-list.py

b/snf-astakos-app/astakos/im/management/commands/service-list.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
......
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

  
36
from django.core.management.base import NoArgsCommand
37 34

  
35
from synnefo.webproject.management.commands import ListCommand
38 36
from astakos.im.models import Service
39 37

  
40 38

  
41
class Command(NoArgsCommand):
39
class Command(ListCommand):
42 40
    help = "List services"
43

  
44
    option_list = NoArgsCommand.option_list + (
45
        make_option('-c',
46
                    action='store_true',
47
                    dest='csv',
48
                    default=False,
49
                    help="Use pipes to separate values"),
50
    )
51

  
52
    def handle_noargs(self, **options):
53
        services = Service.objects.all().order_by('id')
54

  
55
        labels = ('id', 'name', 'API url', 'auth_token')
56
        columns = (3, 12, 70, 20)
57

  
58
        if not options['csv']:
59
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
60
            self.stdout.write(line + '\n')
61
            sep = '-' * len(line)
62
            self.stdout.write(sep + '\n')
63

  
64
        for service in services:
65
            fields = (str(service.id), service.name,
66
                      service.api_url,
67
                      service.auth_token or '')
68

  
69
            if options['csv']:
70
                line = '|'.join(fields)
71
            else:
72
                line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
73

  
74
            self.stdout.write(line + '\n')
41
    object_class = Service
42

  
43
    FIELDS = {
44
        "id": ("id", "ID"),
45
        "name": ("name", "Service Name"),
46
        "url": ("api_url", "Service API url"),
47
        "token": ("auth_token", "Authentication token"),
48
        "created": ("auth_token_created", "Token creation date"),
49
        "expires": ("auth_token_expires", "Token expiration date"),
50
    }
51

  
52
    fields = ["id", "name", "url", "token", "created", "expires"]

Also available in: Unified diff