Debian Codegraph @

adjutant_ui/content/quota/forms.py

raw
1 # Copyright 2016 Catalyst IT Ltd
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import logging
16
17 from django.urls import reverse # noqa
18 from django.utils.translation import gettext_lazy as _
19
20 from horizon import exceptions
21 from horizon import forms
22 from horizon import messages
23
24 from adjutant_ui.api import adjutant
25
26
27 LOG = logging.getLogger(__name__)
28
29
30 class UpdateQuotaForm(forms.SelfHandlingForm):
31 region = forms.CharField(label=_("Region"))
32 region.widget.attrs['readonly'] = True
33 size = forms.ChoiceField(label=_("Size"))
34 size.widget.attrs['onchange'] = 'updateSizeTable()'
35
36 failure_url = 'horizon:management:quota:index'
37 submit_url = 'horizon:management:quota:update'
38 success_url = "horizon:management:quota:index"
39
40 def __init__(self, *args, **kwargs):
41 size_choices = kwargs.pop('size_choices')
42 super(UpdateQuotaForm, self).__init__(*args, **kwargs)
43 self.fields['size'].choices = size_choices
44
45 def handle(self, request, data):
46 try:
47 response = adjutant.update_quotas(request, data['size'],
48 regions=[data['region']])
49 if response.status_code == 202:
50 messages.success(request, _('Task created and may require '
51 'admin approval.'))
52 elif response.status_code == 400:
53 messages.error(request, _('Failed to update quota. You may'
54 ' have usage over the new values '
55 'that you are attempting to update'
56 ' the quota to.'))
57 else:
58 messages.error(request, _('Failed to update quota.'))
59 return True
60 except Exception:
61 msg = _('Failed to update quota.')
62 url = reverse('horizon:management:quota:index')
63 exceptions.handle(request, msg, redirect=url)
64 return False