Revision f72ba65d snf-astakos-app/astakos/im/management/commands/resource-modify.py

b/snf-astakos-app/astakos/im/management/commands/resource-modify.py
37 37

  
38 38
from snf_django.management import utils
39 39
from astakos.im.models import Resource
40
from astakos.im.register import update_resource
40
from astakos.im.register import update_resources
41 41
from ._common import show_resource_value, style_options, check_style, units
42 42

  
43 43

  
......
139 139
        else:
140 140
            resources = [self.get_resource(resource_name)]
141 141

  
142
        updates = []
142 143
        for resource in resources:
143 144
            limit = config.get(resource.name)
144 145
            if limit is not None:
145
                self.change_resource_limit(resource, limit)
146
                limit = self.parse_limit(limit)
147
                updates.append((resource, limit))
148
        if updates:
149
            update_resources(updates)
146 150

  
147 151
    def change_interactive(self, resource_name, _placeholder):
148 152
        if resource_name is None:
......
150 154
        else:
151 155
            resources = [self.get_resource(resource_name)]
152 156

  
157
        updates = []
153 158
        for resource in resources:
154 159
            self.stdout.write("Resource '%s' (%s)\n" %
155 160
                              (resource.name, resource.desc))
......
166 171
                        value = units.parse(response)
167 172
                    except units.ParseError:
168 173
                        continue
169
                    update_resource(resource, value)
174
                    updates.append((resource, value))
170 175
                    break
176
        if updates:
177
            self.stdout.write("Updating...\n")
178
            update_resources(updates)
179

  
180
    def parse_limit(self, limit):
181
        try:
182
            if isinstance(limit, (int, long)):
183
                return limit
184
            if isinstance(limit, basestring):
185
                return units.parse(limit)
186
            raise units.ParseError()
187
        except units.ParseError:
188
            m = ("Limit should be an integer, optionally followed by a unit,"
189
                 " or 'inf'.")
190
            raise CommandError(m)
171 191

  
172 192
    def change_resource_limit(self, resource, limit):
173
        if not isinstance(limit, (int, long)):
174
            try:
175
                limit = units.parse(limit)
176
            except units.ParseError:
177
                m = ("Limit should be an integer, optionally followed "
178
                     "by a unit.")
179
                raise CommandError(m)
180
            update_resource(resource, limit)
193
        limit = self.parse_limit(limit)
194
        update_resources([(resource, limit)])

Also available in: Unified diff