Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / project-list.py @ bf644f91

History | View | Annotate | Download (5.5 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 snf_django.management.commands import SynnefoCommand, CommandError
37

    
38
from astakos.im.models import Project, ProjectApplication
39
from django.db.models import Q
40
from snf_django.management import utils
41
from ._common import is_uuid, is_email
42

    
43

    
44
class Command(SynnefoCommand):
45
    help = """List projects and project status.
46

47
    Project status can be one of:
48
      Uninitialized        an uninitialized project,
49
                           with no pending application
50

51
      Pending              an uninitialized project, pending review
52

53
      Active               an active project
54

55
      Denied               an uninitialized project, denied by the admin
56

57
      Dismissed            a denied project, dismissed by the applicant
58

59
      Cancelled            an uninitialized project, cancelled by the applicant
60

61
      Suspended            a project suspended by the admin;
62
                           it can later be resumed
63

64
      Terminated           a terminated project; its name can be claimed
65
                           by a new project
66

67
      Deleted              an uninitialized, deleted project"""
68

    
69
    option_list = SynnefoCommand.option_list + (
70
        make_option('--new',
71
                    action='store_true',
72
                    dest='new',
73
                    default=False,
74
                    help="List only new pending uninitialized projects"),
75
        make_option('--modified',
76
                    action='store_true',
77
                    dest='modified',
78
                    default=False,
79
                    help="List only projects with pending modification"),
80
        make_option('--pending',
81
                    action='store_true',
82
                    dest='pending',
83
                    default=False,
84
                    help=("Show only projects with a pending application "
85
                          "(equiv. --modified --new)")),
86
        make_option('--deleted',
87
                    action='store_true',
88
                    dest='deleted',
89
                    default=False,
90
                    help="Also so cancelled/terminated projects"),
91
        make_option('--name',
92
                    dest='name',
93
                    help='Filter projects by name'),
94
        make_option('--owner',
95
                    dest='owner',
96
                    help='Filter projects by owner\'s email or uuid'),
97
    )
98

    
99
    def handle(self, *args, **options):
100

    
101
        flt = Q()
102
        owner = options['owner']
103
        if owner:
104
            flt &= filter_by_owner(owner)
105

    
106
        name = options['name']
107
        if name:
108
            flt &= Q(realname=name)
109

    
110
        if not options['deleted']:
111
            flt &= ~Q(state__in=Project.SKIP_STATES)
112

    
113
        pending = Q(last_application__isnull=False,
114
                    last_application__state=ProjectApplication.PENDING)
115

    
116
        if options['pending']:
117
            flt &= pending
118
        else:
119
            if options['new']:
120
                flt &= pending & Q(state=Project.UNINITIALIZED)
121
            if options['modified']:
122
                flt &= pending & Q(state__in=Project.INITIALIZED_STATES)
123

    
124
        projects = Project.objects.\
125
            select_related("last_application", "owner").filter(flt)
126

    
127
        labels = ('ProjID', 'Name', 'Owner', 'Status', 'Pending AppID')
128

    
129
        info = project_info(projects)
130
        utils.pprint_table(self.stdout, info, labels,
131
                           options["output_format"])
132

    
133

    
134
def filter_by_owner(s):
135
    if is_email(s):
136
        return Q(owner__email=s)
137
    if is_uuid(s):
138
        return Q(owner__uuid=s)
139
    raise CommandError("Expecting either email or uuid.")
140

    
141

    
142
def project_info(projects):
143
    l = []
144
    for project in projects:
145
        status = project.state_display()
146
        app = project.last_application
147
        pending_appid = app.id if app and app.state == app.PENDING else ""
148

    
149
        t = (project.uuid,
150
             project.realname,
151
             project.owner.email if project.owner else None,
152
             status,
153
             pending_appid,
154
             )
155
        l.append(t)
156
    return l