| 1 |
# Copyright (c) 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 |
from django.urls import reverse |
| 16 |
from django.urls import reverse_lazy |
| 17 |
from django.utils.translation import gettext_lazy as _ |
| 18 |
|
| 19 |
from horizon import exceptions |
| 20 |
from horizon import forms |
| 21 |
from horizon import tables as horizon_tables |
| 22 |
|
| 23 |
from adjutant_ui.api import adjutant |
| 24 |
from adjutant_ui.content.quota import forms as quota_forms |
| 25 |
from adjutant_ui.content.quota import tables as quota_tables |
| 26 |
|
| 27 |
|
| 28 |
class IndexView(horizon_tables.MultiTableView): |
| 29 |
page_title = _("Quota Management") |
| 30 |
table_classes = (quota_tables.RegionOverviewTable, |
| 31 |
quota_tables.SizeOverviewTable, |
| 32 |
quota_tables.QuotaTasksTable) |
| 33 |
template_name = 'management/quota/index.html' |
| 34 |
|
| 35 |
def get_region_overview_data(self): |
| 36 |
try: |
| 37 |
return adjutant.region_quotas_get(self.request) |
| 38 |
except Exception: |
| 39 |
exceptions.handle(self.request, _('Failed to list quota sizes.')) |
| 40 |
return [] |
| 41 |
|
| 42 |
def get_size_overview_data(self): |
| 43 |
try: |
| 44 |
return adjutant.quota_sizes_get(self.request) |
| 45 |
except Exception: |
| 46 |
exceptions.handle(self.request, _('Failed to list quota sizes.')) |
| 47 |
return [] |
| 48 |
|
| 49 |
def get_quota_tasks_data(self): |
| 50 |
try: |
| 51 |
return adjutant.quota_tasks_get(self.request) |
| 52 |
except Exception: |
| 53 |
exceptions.handle(self.request, _('Failed to list quota tasks.')) |
| 54 |
return [] |
| 55 |
|
| 56 |
|
| 57 |
class RegionDetailView(horizon_tables.DataTableView): |
| 58 |
table_class = quota_tables.RegionQuotaDetailTable |
| 59 |
template_name = 'management/quota/region_detail.html' |
| 60 |
page_title = _("{{ region }} Quota Details") |
| 61 |
|
| 62 |
def get_data(self): |
| 63 |
try: |
| 64 |
return adjutant.quota_details_get(self.request, |
| 65 |
self.kwargs['region']) |
| 66 |
except Exception: |
| 67 |
exceptions.handle(self.request, _('Failed to list quota sizes.')) |
| 68 |
return [] |
| 69 |
|
| 70 |
def get_context_data(self, **kwargs): |
| 71 |
context = super(RegionDetailView, self).get_context_data(**kwargs) |
| 72 |
context['region'] = self.kwargs['region'] |
| 73 |
return context |
| 74 |
|
| 75 |
|
| 76 |
class QuotaSizeView(horizon_tables.DataTableView): |
| 77 |
table_class = quota_tables.QuotaDetailUsageTable |
| 78 |
template_name = 'management/quota/size_detail.html' |
| 79 |
page_title = _("{{ size | title }} Quota Details") |
| 80 |
|
| 81 |
def get_data(self): |
| 82 |
try: |
| 83 |
return adjutant.size_details_get(self.request, |
| 84 |
size=self.kwargs['size']) |
| 85 |
except Exception: |
| 86 |
exceptions.handle(self.request, _('Failed to list quota size.')) |
| 87 |
return [] |
| 88 |
|
| 89 |
def get_context_data(self, **kwargs): |
| 90 |
# request.user.services_region |
| 91 |
context = super(QuotaSizeView, self).get_context_data(**kwargs) |
| 92 |
context['size'] = self.kwargs['size'] |
| 93 |
# NOTE(callumdickinson): Necessary because the title filter |
| 94 |
# is overloaded in the title block. |
| 95 |
context['size_titlecase'] = self.kwargs['size'].title() |
| 96 |
return context |
| 97 |
|
| 98 |
|
| 99 |
class RegionUpdateView(forms.ModalFormView, horizon_tables.MultiTableView): |
| 100 |
form_class = quota_forms.UpdateQuotaForm |
| 101 |
table_classes = (quota_tables.ChangeSizeDisplayTable, ) |
| 102 |
submit_url = 'horizon:management:quota:update' |
| 103 |
context_object_name = 'ticket' |
| 104 |
template_name = 'management/quota/update.html' |
| 105 |
success_url = reverse_lazy("horizon:management:quota:index") |
| 106 |
page_title = _("Update Quota") |
| 107 |
|
| 108 |
def get_change_size_data(self): |
| 109 |
try: |
| 110 |
return adjutant.quota_details_get(self.request, |
| 111 |
region=self.kwargs['region']) |
| 112 |
except Exception: |
| 113 |
exceptions.handle(self.request, _('Failed to list quota sizes.')) |
| 114 |
return [] |
| 115 |
|
| 116 |
def get_object(self): |
| 117 |
return adjutant.region_quotas_get(self.request, |
| 118 |
region=self.kwargs['region'])[0] |
| 119 |
|
| 120 |
def get_context_data(self, **kwargs): |
| 121 |
context = super(RegionUpdateView, self).get_context_data(**kwargs) |
| 122 |
context['region'] = self.get_object() |
| 123 |
args = (self.kwargs['region'],) |
| 124 |
context['submit_url'] = reverse(self.submit_url, args=args) |
| 125 |
context['form'] = self.get_form() |
| 126 |
return context |
| 127 |
|
| 128 |
def get_form_kwargs(self): |
| 129 |
kwargs = super(RegionUpdateView, self).get_form_kwargs() |
| 130 |
sizes = adjutant.quota_sizes_get( |
| 131 |
self.request, region=self.kwargs['region']) |
| 132 |
kwargs['size_choices'] = [] |
| 133 |
|
| 134 |
region = self.get_object() |
| 135 |
for size in sizes: |
| 136 |
if region.quota_size == size.name: |
| 137 |
continue |
| 138 |
if size.name not in region.preapproved_quotas: |
| 139 |
kwargs['size_choices'].append( |
| 140 |
[size.id, "%s (requires approval)" % size.name.title()]) |
| 141 |
else: |
| 142 |
kwargs['size_choices'].append([size.id, size.name.title()]) |
| 143 |
return kwargs |
| 144 |
|
| 145 |
def get_initial(self): |
| 146 |
region = self.get_object() |
| 147 |
data = {'id': region.id, |
| 148 |
'region': region.region, |
| 149 |
'quota_size': region.quota_size, |
| 150 |
'preapproved_quotas': region.preapproved_quotas |
| 151 |
} |
| 152 |
return data |
| 153 |
|
| 154 |
def post(self, request, *args, **kwargs): |
| 155 |
# NOTE(amelia): The multitableview overides the form view post |
| 156 |
# this reinstates it. |
| 157 |
form = self.get_form() |
| 158 |
if form.is_valid(): |
| 159 |
return self.form_valid(form) |
| 160 |
else: |
| 161 |
return self.form_invalid(form) |