Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / project-control.py @ e872c133

History | View | Annotate | Download (5.7 kB)

1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from optparse import make_option
35

    
36
from django.core.management.base import BaseCommand, CommandError
37
from astakos.im.functions import (terminate, suspend, unsuspend,
38
                                  reinstate, check_expiration,
39
                                  approve_application, deny_application)
40
from snf_django.lib.db.transaction import commit_on_success_strict
41

    
42

    
43
class Command(BaseCommand):
44
    help = "Manage projects and applications"
45

    
46
    option_list = BaseCommand.option_list + (
47
        make_option('--approve',
48
                    dest='approve',
49
                    metavar='<application id>',
50
                    help="Approve a project application"),
51
        make_option('--deny',
52
                    dest='deny',
53
                    metavar='<application id>',
54
                    help="Deny a project application"),
55
        make_option('--terminate',
56
                    dest='terminate',
57
                    metavar='<project id>',
58
                    help="Terminate a project"),
59
        make_option('--suspend',
60
                    dest='suspend',
61
                    metavar='<project id>',
62
                    help="Suspend a project"),
63
        make_option('--unsuspend',
64
                    dest='unsuspend',
65
                    metavar='<project id>',
66
                    help="Resume a suspended project"),
67
        make_option('--reinstate',
68
                    dest='reinstate',
69
                    metavar='<project id>',
70
                    help=("Resume a terminated project; this will fail if its "
71
                          "name has been reserved by another project")),
72
        make_option('--check-expired',
73
                    action='store_true',
74
                    dest='check_expired',
75
                    default=False,
76
                    help="Check projects for expiration"),
77
        make_option('--terminate-expired',
78
                    action='store_true',
79
                    dest='terminate_expired',
80
                    default=False,
81
                    help="Terminate all expired projects"),
82
        make_option('--message', '-m',
83
                    dest='message',
84
                    metavar='<msg>',
85
                    help=("Specify reason of action, "
86
                          "e.g. when denying a project")),
87
    )
88

    
89
    @commit_on_success_strict()
90
    def handle(self, *args, **options):
91

    
92
        message = options['message']
93

    
94
        actions = {
95
            'terminate': terminate,
96
            'reinstate': reinstate,
97
            'unsuspend': unsuspend,
98
            'suspend': suspend,
99
            'approve': approve_application,
100
            'deny': lambda a: deny_application(a, reason=message),
101
            'check_expired': lambda _: self.expire(execute=False),
102
            'terminate_expired': lambda _: self.expire(execute=True),
103
        }
104

    
105
        opts = [(key, value)
106
                for (key, value) in options.items()
107
                if key in actions and value]
108

    
109
        if len(opts) != 1:
110
            raise CommandError("Specify exactly one operation.")
111

    
112
        key, value = opts[0]
113
        action = actions[key]
114
        try:
115
            action(value)
116
        except BaseException as e:
117
            raise CommandError(e)
118

    
119
    def print_expired(self, projects, execute):
120
        length = len(projects)
121
        if length == 0:
122
            s = 'No expired projects.\n'
123
        elif length == 1:
124
            s = '1 expired project:\n'
125
        else:
126
            s = '%d expired projects:\n' % (length,)
127
        self.stdout.write(s)
128

    
129
        if length > 0:
130
            labels = ('Project', 'Name', 'Status', 'Expiration date')
131
            columns = (10, 30, 14, 30)
132

    
133
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
134
            self.stdout.write(line + '\n')
135
            sep = '-' * len(line)
136
            self.stdout.write(sep + '\n')
137

    
138
            for project in projects:
139
                line = ' '.join(f.rjust(w) for f, w in zip(project, columns))
140
                self.stdout.write(line + '\n')
141

    
142
            if execute:
143
                self.stdout.write('%d projects have been terminated.\n' % (
144
                    length,))
145

    
146
    def expire(self, execute=False):
147
        projects = check_expiration(execute=execute)
148
        self.print_expired(projects, execute)