Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.3 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 ListCommand
37

    
38
from astakos.im.models import Project, ProjectApplication
39
from ._common import is_uuid
40

    
41

    
42
class Command(ListCommand):
43
    help = """List projects and project status.
44

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

49
      Pending              an uninitialized project, pending review
50

51
      Active               an active project
52

53
      Denied               an uninitialized project, denied by the admin
54

55
      Dismissed            a denied project, dismissed by the applicant
56

57
      Cancelled            an uninitialized project, cancelled by the applicant
58

59
      Suspended            a project suspended by the admin;
60
                           it can later be resumed
61

62
      Terminated           a terminated project; its name can be claimed
63
                           by a new project
64

65
      Deleted              an uninitialized, deleted project"""
66

    
67
    object_class = Project
68
    select_related = ["last_application", "owner"]
69

    
70
    option_list = ListCommand.option_list + (
71
        make_option('--new',
72
                    action='store_true',
73
                    dest='new',
74
                    default=False,
75
                    help="List only new pending uninitialized projects"),
76
        make_option('--modified',
77
                    action='store_true',
78
                    dest='modified',
79
                    default=False,
80
                    help="List only projects with pending modification"),
81
        make_option('--pending',
82
                    action='store_true',
83
                    dest='pending',
84
                    default=False,
85
                    help=("Show only projects with a pending application "
86
                          "(equiv. --modified --new)")),
87
        make_option('--deleted',
88
                    action='store_true',
89
                    dest='deleted',
90
                    default=False,
91
                    help="Also so cancelled/terminated projects"),
92
    )
93

    
94
    def get_owner(project):
95
        return project.owner.email if project.owner else None
96

    
97
    def get_status(project):
98
        return project.state_display()
99

    
100
    def get_pending_app(project):
101
        app = project.last_application
102
        return app.id if app and app.state == app.PENDING else ""
103

    
104
    FIELDS = {
105
        "id": ("uuid", "Project ID"),
106
        "name": ("realname", "Project Name"),
107
        "owner": (get_owner, "Project Owner"),
108
        "status": (get_status, "Project Status"),
109
        "pending_app": (get_pending_app,
110
                        "An application pending for the project"),
111
    }
112

    
113
    fields = ["id", "name", "owner", "status", "pending_app"]
114

    
115
    def handle_args(self, *args, **options):
116
        try:
117
            name_filter = self.filters.pop("name")
118
            self.filters["realname"] = name_filter
119
        except KeyError:
120
            pass
121

    
122
        try:
123
            owner_filter = self.filters.pop("owner")
124
            if owner_filter is not None:
125
                if is_uuid(owner_filter):
126
                    self.filters["owner__uuid"] = owner_filter
127
                else:
128
                    self.filters["owner__email"] = owner_filter
129
        except KeyError:
130
            pass
131

    
132
        if not options['deleted']:
133
            self.excludes["state__in"] = Project.SKIP_STATES
134

    
135
        if options["pending"]:
136
            self.filter_pending()
137
        else:
138
            if options['new']:
139
                self.filter_pending()
140
                self.filters["state"] = Project.UNINITIALIZED
141
            if options['modified']:
142
                self.filter_pending()
143
                self.filters["state__in"] = Project.INITIALIZED_STATES
144

    
145
    def filter_pending(self):
146
        self.filters["last_application__state"] = ProjectApplication.PENDING