Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / project-admin-checks.py @ 9d5cf81c

History | View | Annotate | Download (3.7 kB)

1
# Copyright 2012 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
from django.core.management.base import BaseCommand, CommandError
36

    
37
from astakos.im.functions import check_expiration
38
from astakos.im.project_xctx import cmd_project_transaction_context
39

    
40
class Command(BaseCommand):
41
    help = """
42
    Check for and perform due administration tasks (e.g. termination)"
43

44
    """
45
    option_list = BaseCommand.option_list + (
46
        make_option('--check-expired',
47
                    action='store_true',
48
                    dest='expire',
49
                    default=False,
50
                    help="Check projects for expiration"),
51
        make_option('--execute',
52
                    action='store_true',
53
                    dest='execute',
54
                    default=False,
55
                    help="Perform the actual operation"),
56
    )
57

    
58

    
59
    def print_expired(self, projects, execute):
60
        length = len(projects)
61
        if length == 0:
62
            s = 'No expired projects.\n'
63
        elif length == 1:
64
            s = '1 expired project:\n'
65
        else:
66
            s = '%d expired projects:\n' %(length,)
67
        self.stdout.write(s)
68

    
69
        if length > 0:
70
            labels = ('Project', 'Name', 'Status', 'Expiration date')
71
            columns = (10, 30, 14, 30)
72

    
73
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
74
            self.stdout.write(line + '\n')
75
            sep = '-' * len(line)
76
            self.stdout.write(sep + '\n')
77

    
78
            for project in projects:
79
                line = ' '.join(f.rjust(w) for f, w in zip(project, columns))
80
                self.stdout.write(line + '\n')
81

    
82
            if execute:
83
                self.stdout.write('%d projects have been terminated.\n' %(length,))
84

    
85
    def handle(self, *args, **options):
86

    
87
        execute = options['execute']
88
        if options['expire']:
89
            self.expire(execute=execute)
90

    
91
    @cmd_project_transaction_context(sync=True)
92
    def expire(self, execute=False, ctx=None):
93
        try:
94
            projects = check_expiration(execute=execute)
95
            self.print_expired(projects, execute)
96
        except BaseException as e:
97
            if ctx:
98
                ctx.mark_rollback()
99
            raise CommandError(e)