Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / resource-import.py @ a3e3917f

History | View | Annotate | Download (3.3 kB)

1 b1ea24f3 Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 b1ea24f3 Giorgos Korfiatis
#
3 b1ea24f3 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 b1ea24f3 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 b1ea24f3 Giorgos Korfiatis
# conditions are met:
6 b1ea24f3 Giorgos Korfiatis
#
7 b1ea24f3 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 b1ea24f3 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 b1ea24f3 Giorgos Korfiatis
#      disclaimer.
10 b1ea24f3 Giorgos Korfiatis
#
11 b1ea24f3 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 b1ea24f3 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 b1ea24f3 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 b1ea24f3 Giorgos Korfiatis
#      provided with the distribution.
15 b1ea24f3 Giorgos Korfiatis
#
16 b1ea24f3 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 b1ea24f3 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 b1ea24f3 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 b1ea24f3 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 b1ea24f3 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 b1ea24f3 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 b1ea24f3 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 b1ea24f3 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 b1ea24f3 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 b1ea24f3 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 b1ea24f3 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 b1ea24f3 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 b1ea24f3 Giorgos Korfiatis
#
29 b1ea24f3 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 b1ea24f3 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 b1ea24f3 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 b1ea24f3 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 b1ea24f3 Giorgos Korfiatis
34 b1ea24f3 Giorgos Korfiatis
from optparse import make_option
35 b1ea24f3 Giorgos Korfiatis
36 a3e3917f Giorgos Korfiatis
from django.db import transaction
37 b1ea24f3 Giorgos Korfiatis
from django.core.management.base import BaseCommand, CommandError
38 b1ea24f3 Giorgos Korfiatis
from django.utils import simplejson as json
39 b1ea24f3 Giorgos Korfiatis
40 38807757 Giorgos Korfiatis
from astakos.im.register import add_resource, RegisterException
41 c25f5ed0 Giorgos Korfiatis
from ._common import read_from_file
42 b1ea24f3 Giorgos Korfiatis
43 b1ea24f3 Giorgos Korfiatis
44 b1ea24f3 Giorgos Korfiatis
class Command(BaseCommand):
45 bd1f667b Giorgos Korfiatis
    help = "Register resources"
46 b1ea24f3 Giorgos Korfiatis
47 b1ea24f3 Giorgos Korfiatis
    option_list = BaseCommand.option_list + (
48 b1ea24f3 Giorgos Korfiatis
        make_option('--json',
49 b1ea24f3 Giorgos Korfiatis
                    dest='json',
50 b1ea24f3 Giorgos Korfiatis
                    metavar='<json.file>',
51 781c7b4b Giorgos Korfiatis
                    help="Load resource definitions from a json file"),
52 b1ea24f3 Giorgos Korfiatis
    )
53 b1ea24f3 Giorgos Korfiatis
54 b1ea24f3 Giorgos Korfiatis
    def handle(self, *args, **options):
55 b1ea24f3 Giorgos Korfiatis
56 b1ea24f3 Giorgos Korfiatis
        json_file = options['json']
57 781c7b4b Giorgos Korfiatis
        if not json_file:
58 781c7b4b Giorgos Korfiatis
            m = "Expecting option --json."
59 9747707e Giorgos Korfiatis
            raise CommandError(m)
60 9747707e Giorgos Korfiatis
61 781c7b4b Giorgos Korfiatis
        else:
62 c25f5ed0 Giorgos Korfiatis
            data = read_from_file(json_file)
63 c25f5ed0 Giorgos Korfiatis
            m = 'Input should be a JSON list.'
64 c25f5ed0 Giorgos Korfiatis
            try:
65 c25f5ed0 Giorgos Korfiatis
                data = json.loads(data)
66 c25f5ed0 Giorgos Korfiatis
            except json.JSONDecodeError:
67 c25f5ed0 Giorgos Korfiatis
                raise CommandError(m)
68 c25f5ed0 Giorgos Korfiatis
            if not isinstance(data, list):
69 c25f5ed0 Giorgos Korfiatis
                raise CommandError(m)
70 bd1f667b Giorgos Korfiatis
        self.add_resources(data)
71 9747707e Giorgos Korfiatis
72 a3e3917f Giorgos Korfiatis
    @transaction.commit_on_success
73 bd1f667b Giorgos Korfiatis
    def add_resources(self, resources):
74 bd1f667b Giorgos Korfiatis
        output = []
75 9747707e Giorgos Korfiatis
        for resource in resources:
76 3a527b3a Giorgos Korfiatis
            if not isinstance(resource, dict):
77 3a527b3a Giorgos Korfiatis
                raise CommandError("Malformed resource dict.")
78 bd1f667b Giorgos Korfiatis
            try:
79 bd1f667b Giorgos Korfiatis
                r, exists = add_resource(resource)
80 38807757 Giorgos Korfiatis
            except RegisterException as e:
81 bd1f667b Giorgos Korfiatis
                raise CommandError(e.message)
82 73c02f75 Giorgos Korfiatis
            name = r.name
83 3a527b3a Giorgos Korfiatis
            if exists:
84 3a527b3a Giorgos Korfiatis
                m = "Resource '%s' updated in database.\n" % (name)
85 3a527b3a Giorgos Korfiatis
            else:
86 3a527b3a Giorgos Korfiatis
                m = ("Resource '%s' created in database with default "
87 3a527b3a Giorgos Korfiatis
                     "quota limit 0.\n" % (name))
88 bd1f667b Giorgos Korfiatis
            output.append(m)
89 bd1f667b Giorgos Korfiatis
90 bd1f667b Giorgos Korfiatis
        for line in output:
91 bd1f667b Giorgos Korfiatis
            self.stdout.write(line)