Statistics
| Branch: | Tag: | Revision:

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

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