Statistics
| Branch: | Tag: | Revision:

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

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.db import transaction
37
from django.core.management.base import CommandError
38

    
39
from astakos.im.functions import (terminate, suspend, unsuspend,
40
                                  reinstate, check_expiration,
41
                                  approve_application, deny_application)
42
from snf_django.management.commands import SynnefoCommand
43

    
44

    
45
class Command(SynnefoCommand):
46
    help = "Manage projects and applications"
47

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

    
91
    @transaction.commit_on_success
92
    def handle(self, *args, **options):
93

    
94
        message = options['message']
95

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

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

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

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

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

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

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

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

    
144
            if execute:
145
                self.stderr.write('%d projects have been terminated.\n' % (
146
                    length,))
147

    
148
    def expire(self, execute=False):
149
        projects = check_expiration(execute=execute)
150
        self.print_expired(projects, execute)