project-list.py 5.54 KB
Newer Older
1
# Copyright 2012-2014 GRNET S.A. All rights reserved.
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

from optparse import make_option

36
from snf_django.management.commands import ListCommand
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
37

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

Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
41

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

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

49
      Pending              an uninitialized project, pending review
50 51 52

      Active               an active project

53
      Denied               an uninitialized project, denied by the admin
54 55 56

      Dismissed            a denied project, dismissed by the applicant

57
      Cancelled            an uninitialized project, cancelled by the applicant
58 59 60 61 62

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

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

      Deleted              an uninitialized, deleted project"""
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
66

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

    option_list = ListCommand.option_list + (
71 72 73 74
        make_option('--new',
                    action='store_true',
                    dest='new',
                    default=False,
75
                    help="List only new pending uninitialized projects"),
76 77 78 79 80 81 82 83 84 85 86
        make_option('--modified',
                    action='store_true',
                    dest='modified',
                    default=False,
                    help="List only projects with pending modification"),
        make_option('--pending',
                    action='store_true',
                    dest='pending',
                    default=False,
                    help=("Show only projects with a pending application "
                          "(equiv. --modified --new)")),
87
        make_option('--deleted',
88
                    action='store_true',
89
                    dest='deleted',
90
                    default=False,
91 92 93 94 95
                    help="Also show cancelled/terminated projects"),
        make_option('--base-projects',
                    action='store_true',
                    default=False,
                    help="Also show base projects"),
Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
96
    )
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
97

98 99
    def get_owner(project):
        return project.owner.email if project.owner else None
100

101 102
    def get_status(project):
        return project.state_display()
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    def get_pending_app(project):
        app = project.last_application
        return app.id if app and app.state == app.PENDING else ""

    FIELDS = {
        "id": ("uuid", "Project ID"),
        "name": ("realname", "Project Name"),
        "owner": (get_owner, "Project Owner"),
        "status": (get_status, "Project Status"),
        "pending_app": (get_pending_app,
                        "An application pending for the project"),
    }

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

    def handle_args(self, *args, **options):
        try:
            name_filter = self.filters.pop("name")
            self.filters["realname"] = name_filter
        except KeyError:
            pass

        try:
            owner_filter = self.filters.pop("owner")
            if owner_filter is not None:
                if is_uuid(owner_filter):
                    self.filters["owner__uuid"] = owner_filter
                else:
                    self.filters["owner__email"] = owner_filter
        except KeyError:
            pass
135

136
        if not options['deleted']:
137
            self.excludes["state__in"] = Project.SKIP_STATES
138

139 140 141
        if not options['base_projects']:
            self.excludes["is_base"] = True

142 143
        if options["pending"]:
            self.filter_pending()
144 145
        else:
            if options['new']:
146 147
                self.filter_pending()
                self.filters["state"] = Project.UNINITIALIZED
148
            if options['modified']:
149 150
                self.filter_pending()
                self.filters["state__in"] = Project.INITIALIZED_STATES
151

152 153
    def filter_pending(self):
        self.filters["last_application__state"] = ProjectApplication.PENDING