Revision 7eadc230

b/snf-astakos-app/astakos/im/functions.py
702 702

  
703 703
    application_approve_notify(application)
704 704

  
705
def check_expiration(execute=False):
706
    objects = Project.objects
707
    expired = objects.expired_projects()
708
    if execute:
709
        for project in expired:
710
            terminate(project.id)
711

  
712
    return [project.expiration_info() for project in expired]
713

  
705 714
def terminate(project_id):
706 715
    project = get_project_for_update(project_id)
707 716
    checkAlive(project)
b/snf-astakos-app/astakos/im/management/commands/project-admin-checks.py
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
from django.db import transaction
37

  
38
from astakos.im.functions import check_expiration
39

  
40
@transaction.commit_manually
41
class Command(BaseCommand):
42
    args = "<project id>"
43
    help = "Update project state"
44

  
45
    option_list = BaseCommand.option_list + (
46
        make_option('--expire',
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.encode('utf8') + '\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

  
89
        try:
90
            if options['expire']:
91
                projects = check_expiration(execute=execute)
92
                self.print_expired(projects, execute)
93
        except BaseException as e:
94
            transaction.rollback()
95
            raise CommandError(e)
96
        else:
97
            transaction.commit()
b/snf-astakos-app/astakos/im/models.py
1556 1556
    def reactivating_projects(self):
1557 1557
        return self.filter(state=Project.APPROVED, is_active=False)
1558 1558

  
1559
    def expired_projects(self):
1560
        q = (~Q(state=Project.TERMINATED) &
1561
              Q(application__end_date__lt=datetime.now()))
1562
        return self.filter(q)
1563

  
1564

  
1559 1565
class Project(models.Model):
1560 1566

  
1561 1567
    application                 =   models.OneToOneField(
......
1608 1614
    def state_display(self):
1609 1615
        return self.STATE_DISPLAY.get(self.state, _('Unknown'))
1610 1616

  
1617
    def expiration_info(self):
1618
        return (str(self.id), self.name, self.state_display(),
1619
                str(self.application.end_date))
1620

  
1611 1621
    def is_deactivated(self, reason=None):
1612 1622
        if reason is not None:
1613 1623
            return self.state == reason

Also available in: Unified diff