Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.7 kB)

1 7eadc230 Giorgos Korfiatis
# Copyright 2012 GRNET S.A. All rights reserved.
2 7eadc230 Giorgos Korfiatis
#
3 7eadc230 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 7eadc230 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 7eadc230 Giorgos Korfiatis
# conditions are met:
6 7eadc230 Giorgos Korfiatis
#
7 7eadc230 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 7eadc230 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 7eadc230 Giorgos Korfiatis
#      disclaimer.
10 7eadc230 Giorgos Korfiatis
#
11 7eadc230 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 7eadc230 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 7eadc230 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 7eadc230 Giorgos Korfiatis
#      provided with the distribution.
15 7eadc230 Giorgos Korfiatis
#
16 7eadc230 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 7eadc230 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 7eadc230 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 7eadc230 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 7eadc230 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 7eadc230 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 7eadc230 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 7eadc230 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 7eadc230 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 7eadc230 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 7eadc230 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 7eadc230 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 7eadc230 Giorgos Korfiatis
#
29 7eadc230 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 7eadc230 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 7eadc230 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 7eadc230 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 7eadc230 Giorgos Korfiatis
34 7eadc230 Giorgos Korfiatis
from optparse import make_option
35 7eadc230 Giorgos Korfiatis
from django.core.management.base import BaseCommand, CommandError
36 7eadc230 Giorgos Korfiatis
37 7eadc230 Giorgos Korfiatis
from astakos.im.functions import check_expiration
38 321384f9 Giorgos Korfiatis
from astakos.im.project_xctx import cmd_project_transaction_context
39 7eadc230 Giorgos Korfiatis
40 7eadc230 Giorgos Korfiatis
class Command(BaseCommand):
41 9d5cf81c Georgios D. Tsoukalas
    help = """
42 9d5cf81c Georgios D. Tsoukalas
    Check for and perform due administration tasks (e.g. termination)"
43 7eadc230 Giorgos Korfiatis

44 9d5cf81c Georgios D. Tsoukalas
    """
45 7eadc230 Giorgos Korfiatis
    option_list = BaseCommand.option_list + (
46 c69462a4 Giorgos Korfiatis
        make_option('--check-expired',
47 7eadc230 Giorgos Korfiatis
                    action='store_true',
48 7eadc230 Giorgos Korfiatis
                    dest='expire',
49 7eadc230 Giorgos Korfiatis
                    default=False,
50 7eadc230 Giorgos Korfiatis
                    help="Check projects for expiration"),
51 7eadc230 Giorgos Korfiatis
        make_option('--execute',
52 7eadc230 Giorgos Korfiatis
                    action='store_true',
53 7eadc230 Giorgos Korfiatis
                    dest='execute',
54 7eadc230 Giorgos Korfiatis
                    default=False,
55 7eadc230 Giorgos Korfiatis
                    help="Perform the actual operation"),
56 7eadc230 Giorgos Korfiatis
    )
57 7eadc230 Giorgos Korfiatis
58 7eadc230 Giorgos Korfiatis
59 7eadc230 Giorgos Korfiatis
    def print_expired(self, projects, execute):
60 7eadc230 Giorgos Korfiatis
        length = len(projects)
61 7eadc230 Giorgos Korfiatis
        if length == 0:
62 7eadc230 Giorgos Korfiatis
            s = 'No expired projects.\n'
63 7eadc230 Giorgos Korfiatis
        elif length == 1:
64 7eadc230 Giorgos Korfiatis
            s = '1 expired project:\n'
65 7eadc230 Giorgos Korfiatis
        else:
66 7eadc230 Giorgos Korfiatis
            s = '%d expired projects:\n' %(length,)
67 7eadc230 Giorgos Korfiatis
        self.stdout.write(s)
68 7eadc230 Giorgos Korfiatis
69 7eadc230 Giorgos Korfiatis
        if length > 0:
70 7eadc230 Giorgos Korfiatis
            labels = ('Project', 'Name', 'Status', 'Expiration date')
71 7eadc230 Giorgos Korfiatis
            columns = (10, 30, 14, 30)
72 7eadc230 Giorgos Korfiatis
73 7eadc230 Giorgos Korfiatis
            line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
74 7eadc230 Giorgos Korfiatis
            self.stdout.write(line + '\n')
75 7eadc230 Giorgos Korfiatis
            sep = '-' * len(line)
76 7eadc230 Giorgos Korfiatis
            self.stdout.write(sep + '\n')
77 7eadc230 Giorgos Korfiatis
78 7eadc230 Giorgos Korfiatis
            for project in projects:
79 7eadc230 Giorgos Korfiatis
                line = ' '.join(f.rjust(w) for f, w in zip(project, columns))
80 06d7c286 Sofia Papagiannaki
                self.stdout.write(line + '\n')
81 7eadc230 Giorgos Korfiatis
82 7eadc230 Giorgos Korfiatis
            if execute:
83 7eadc230 Giorgos Korfiatis
                self.stdout.write('%d projects have been terminated.\n' %(length,))
84 7eadc230 Giorgos Korfiatis
85 7eadc230 Giorgos Korfiatis
    def handle(self, *args, **options):
86 7eadc230 Giorgos Korfiatis
87 7eadc230 Giorgos Korfiatis
        execute = options['execute']
88 8cf9b2dd Giorgos Korfiatis
        if options['expire']:
89 8cf9b2dd Giorgos Korfiatis
            self.expire(execute=execute)
90 7eadc230 Giorgos Korfiatis
91 321384f9 Giorgos Korfiatis
    @cmd_project_transaction_context(sync=True)
92 8cf9b2dd Giorgos Korfiatis
    def expire(self, execute=False, ctx=None):
93 7eadc230 Giorgos Korfiatis
        try:
94 8cf9b2dd Giorgos Korfiatis
            projects = check_expiration(execute=execute)
95 8cf9b2dd Giorgos Korfiatis
            self.print_expired(projects, execute)
96 7eadc230 Giorgos Korfiatis
        except BaseException as e:
97 8cf9b2dd Giorgos Korfiatis
            if ctx:
98 8cf9b2dd Giorgos Korfiatis
                ctx.mark_rollback()
99 7eadc230 Giorgos Korfiatis
            raise CommandError(e)