Statistics
| Branch: | Tag: | Revision:

root / snf-django-lib / snf_django / management / commands / __init__.py @ 787f7372

History | View | Annotate | Download (12.8 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 django.core.management.base import BaseCommand, CommandError
37
from django.core.exceptions import FieldError
38

    
39
from snf_django.management import utils
40
from snf_django.lib.astakos import UserCache
41

    
42
import distutils
43

    
44
USER_EMAIL_FIELD = "user.email"
45

    
46

    
47
class SynnefoCommand(BaseCommand):
48
    option_list = BaseCommand.option_list + (
49
        make_option(
50
            "--output-format",
51
            dest="output_format",
52
            metavar="[pretty, csv, json]",
53
            default="pretty",
54
            choices=["pretty", "csv", "json"],
55
            help="Select the output format: pretty [the default], json, "
56
                 "csv [comma-separated output]"),
57
    )
58

    
59

    
60
class ListCommand(SynnefoCommand):
61
    """Generic *-list management command.
62

63
    Management command to handle common tasks when implementing a -list
64
    management command. This class handles the following tasks:
65

66
    * Retrieving objects from database.
67

68
    The DB model class is declared in ``object_class`` class attribute. Also,
69
    results can be filter using either the ``filters`` and ``excludes``
70
    attribute or the "--filter-by" option.
71

72
    * Display specific fields of the database objects.
73

74
    List of available fields is defined in the ``FIELDS`` class attribute,
75
    which is a dictionary mapping from field names to tuples containing the
76
    way the field is retrieved and a text help message to display. The first
77
    field of the tuple is either a string containing a chain of attribute
78
    accesses (e.g. "machine.flavor.cpu") either a callable function, taking
79
    as argument the DB object and returning a single value.
80

81
    The fields that will be displayed be default is contained in the ``fields``
82
    class attribute. The user can specify different fields using the "--fields"
83
    option.
84

85
    * Handling of user UUIDs and names.
86

87
    If the ``user_uuid_field`` is declared, then "--user" and "--display-mails"
88
    options will become available. The first one allows filtering via either
89
    a user's UUID or display name. The "--displayname" option will append
90
    the displayname of ther user with "user_uuid_field" to the output.
91

92
    * Pretty printing output to a nice table.
93

94
    """
95

    
96
    # The following fields must be handled in the ListCommand subclasses!
97

    
98
    # The django DB model
99
    object_class = None
100
    # The name of the field containg the user ID of the user, if any.
101
    user_uuid_field = None
102
    # The name of the field containg the deleted flag, if any.
103
    deleted_field = None
104
    # Dictionary with all available fields
105
    FIELDS = {}
106
    # List of fields to display by default
107
    fields = []
108
    # Default filters and excludes
109
    filters = {}
110
    excludes = {}
111
    # Order results
112
    order_by = None
113

    
114
    # Fields used only with user_user_field
115
    astakos_auth_url = None
116
    astakos_token = None
117

    
118
    # Optimize DB queries
119
    prefetch_related = []
120
    select_related = []
121

    
122
    help = "Generic List Command"
123
    option_list = SynnefoCommand.option_list + (
124
        make_option(
125
            "-o", "--output",
126
            dest="fields",
127
            help="Comma-separated list of output fields"),
128
        make_option(
129
            "--list-fields",
130
            dest="list_fields",
131
            action="store_true",
132
            default=False,
133
            help="List available output fields"),
134
        make_option(
135
            "--filter-by",
136
            dest="filter_by",
137
            metavar="FILTERS",
138
            help="Filter results. Comma separated list of key `cond` val pairs"
139
                 " that displayed entries must satisfy. e.g."
140
                 " --filter-by \"deleted=False,id>=22\"."),
141
        make_option(
142
            "--list-filters",
143
            dest="list_filters",
144
            action="store_true",
145
            default=False,
146
            help="List available filters"),
147
        make_option(
148
            "--no-headers",
149
            dest="headers",
150
            action="store_false",
151
            default=True,
152
            help="Do not display headers"),
153
    )
154

    
155
    def __init__(self, *args, **kwargs):
156
        if self.user_uuid_field:
157
            assert(self.astakos_auth_url), "astakos_auth_url attribute is "\
158
                                           "needed when user_uuid_field "\
159
                                           "is declared"
160
            assert(self.astakos_token), "astakos_token attribute is needed"\
161
                                        " when user_uuid_field is declared"
162
            self.option_list += (
163
                make_option(
164
                    "-u", "--user",
165
                    dest="user",
166
                    metavar="USER",
167
                    help="List items only for this user."
168
                         " 'USER' can be either a user UUID or a display"
169
                         " name"),
170
                make_option(
171
                    "--display-mails",
172
                    dest="display_mails",
173
                    action="store_true",
174
                    default=False,
175
                    help="Include the user's email"),
176
            )
177

    
178
        if self.deleted_field:
179
            self.option_list += (
180
                make_option(
181
                    "-d", "--deleted",
182
                    dest="deleted",
183
                    action="store_true",
184
                    help="Display only deleted items"),
185
            )
186
        super(ListCommand, self).__init__(*args, **kwargs)
187

    
188
    def handle(self, *args, **options):
189
        if len(args) > 0:
190
            raise CommandError("List commands do not accept any argument")
191

    
192
        assert(self.object_class), "object_class variable must be declared"
193

    
194
        # If an user field is declared, include the USER_EMAIL_FIELD in the
195
        # available fields
196
        if self.user_uuid_field is not None:
197
            self.FIELDS[USER_EMAIL_FIELD] =\
198
                ("_user_email", "The email of the owner")
199

    
200
        if options["list_fields"]:
201
            self.display_fields()
202
            return
203

    
204
        if options["list_filters"]:
205
            self.display_filters()
206
            return
207

    
208
        # --output option
209
        if options["fields"]:
210
            fields = options["fields"]
211
            fields = fields.split(",")
212
            self.validate_fields(fields)
213
            self.fields = options["fields"].split(",")
214

    
215
        # --display-mails option
216
        if options.get("display_mails"):
217
            self.fields.append(USER_EMAIL_FIELD)
218

    
219
        # --filter-by option
220
        if options["filter_by"]:
221
            filters, excludes = \
222
                utils.parse_queryset_filters(options["filter_by"])
223
        else:
224
            filters, excludes = ({}, {})
225

    
226
        self.filters.update(filters)
227
        self.excludes.update(excludes)
228

    
229
        # --user option
230
        user = options.get("user")
231
        if user:
232
            if "@" in user:
233
                ucache = UserCache(self.astakos_auth_url, self.astakos_token)
234
                user = ucache.get_uuid(user)
235
            self.filters[self.user_uuid_field] = user
236

    
237
        # --deleted option
238
        if self.deleted_field:
239
            deleted = options.get("deleted")
240
            if deleted:
241
                self.filters[self.deleted_field] = True
242
            else:
243
                self.filters[self.deleted_field] = False
244

    
245
        # Special handling of arguments
246
        self.handle_args(self, *args, **options)
247

    
248
        select_related = getattr(self, "select_related", [])
249
        prefetch_related = getattr(self, "prefetch_related", [])
250

    
251
        objects = self.object_class.objects
252
        try:
253
            if select_related:
254
                objects = objects.select_related(*select_related)
255
            if prefetch_related:
256
                objects = objects.prefetch_related(*prefetch_related)
257
            objects = objects.filter(**self.filters)
258
            objects = objects.exclude(**self.excludes)
259
        except FieldError as e:
260
            raise CommandError(e)
261
        except Exception as e:
262
            raise CommandError("Can not filter results: %s" % e)
263

    
264
        order_key = self.order_by if self.order_by is not None else 'pk'
265
        objects = objects.order_by(order_key)
266

    
267
        if USER_EMAIL_FIELD in self.fields:
268
            if '_user_email' in self.object_class._meta.get_all_field_names():
269
                raise RuntimeError("%s has already a 'user_mail' attribute")
270
            uuids = [getattr(obj, self.user_uuid_field) for obj in objects]
271
            ucache = UserCache(self.astakos_auth_url, self.astakos_token)
272
            ucache.fetch_names(list(set(uuids)))
273
            for obj in objects:
274
                uuid = getattr(obj, self.user_uuid_field)
275
                obj._user_email = ucache.get_name(uuid)
276

    
277
        # Special handling of DB results
278
        objects = list(objects)
279
        self.handle_db_objects(objects, **options)
280

    
281
        headers = self.fields
282
        columns = [self.FIELDS[key][0] for key in headers]
283

    
284
        table = []
285
        for obj in objects:
286
            row = []
287
            for attr in columns:
288
                if callable(attr):
289
                    row.append(attr(obj))
290
                else:
291
                    item = obj
292
                    attrs = attr.split(".")
293
                    for attr in attrs:
294
                        item = getattr(item, attr)
295
                    row.append(item)
296
            table.append(row)
297

    
298
        # Special handle of output
299
        self.handle_output(table, headers)
300

    
301
        # Print output
302
        output_format = options["output_format"]
303
        if output_format != "json" and not options["headers"]:
304
            headers = None
305
        utils.pprint_table(self.stdout, table, headers, output_format)
306

    
307
    def handle_args(self, *args, **kwargs):
308
        pass
309

    
310
    def handle_db_objects(self, objects, **options):
311
        pass
312

    
313
    def handle_output(self, table, headers):
314
        pass
315

    
316
    def display_fields(self):
317
        headers = ["Field", "Description"]
318
        table = []
319
        for field, (_, help_msg) in self.FIELDS.items():
320
            table.append((field, help_msg))
321
        utils.pprint_table(self.stdout, table, headers)
322

    
323
    def validate_fields(self, fields):
324
        for f in fields:
325
            if f not in self.FIELDS.keys():
326
                raise CommandError("Unknown field '%s'. 'Use --list-fields"
327
                                   " option to find out available fields."
328
                                   % f)
329

    
330
    def display_filters(self):
331
        headers = ["Filter", "Description", "Help"]
332
        table = []
333
        for field in self.object_class._meta.fields:
334
            table.append((field.name, field.verbose_name, field.help_text))
335
        utils.pprint_table(self.stdout, table, headers)
336

    
337

    
338
class RemoveCommand(BaseCommand):
339
    help = "Generic remove command"
340
    option_list = BaseCommand.option_list + (
341
        make_option(
342
            "-f", "--force",
343
            dest="force",
344
            action="store_true",
345
            default=False,
346
            help="Do not prompt for confirmation"),
347
    )
348

    
349
    def confirm_deletion(self, force, resource='', args=''):
350
        if force is True:
351
            return True
352

    
353
        ids = ', '.join(args)
354
        self.stdout.write("Are you sure you want to delete %s %s?"
355
                          " [Y/N] " % (resource, ids))
356
        try:
357
            answer = distutils.util.strtobool(raw_input())
358
            if answer != 1:
359
                raise CommandError("Aborting deletion")
360
        except ValueError:
361
            raise CommandError("Unaccepted input value. Please choose yes/no"
362
                               " (y/n).")