Debian Codegraph @

adjutant_ui/content/quota/tables.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 json
16
17 from django.utils.translation import gettext_lazy as _
18 from django.utils.translation import ngettext_lazy
19
20 from horizon import exceptions
21 from horizon import tables
22
23 from openstack_dashboard.dashboards.admin.defaults.tables import (
24 get_compute_quota_name, get_volume_quota_name, get_network_quota_name
25 )
26
27 from adjutant_ui.api import adjutant
28
29
30 def to_caps(value):
31 return value.title()
32
33
34 def display_as_percent(value):
35 if value == "-":
36 return value
37 return '{:.1%}'.format(value)
38
39
40 def get_quota_name(quota):
41 if quota.service == "nova":
42 return get_compute_quota_name(quota)
43 if quota.service == "cinder":
44 return get_volume_quota_name(quota)
45 if quota.service == "neutron":
46 return get_network_quota_name(quota)
47
48 return quota.name.replace("_", " ").title()
49
50
51 class UpdateQuota(tables.LinkAction):
52 name = "update"
53 verbose_name = _("Update Quota")
54 url = "horizon:management:quota:update"
55 classes = ("ajax-modal",)
56 icon = "edit"
57
58
59 class CancelQuotaTask(tables.DeleteAction):
60 help_text = _("This will cancel the selected quota update.")
61
62 @staticmethod
63 def action_present(count):
64 return ngettext_lazy(
65 u"Cancel Quota Update",
66 u"Cancel Quota Updates",
67 count
68 )
69
70 @staticmethod
71 def action_past(count):
72 return ngettext_lazy(
73 u"Cancelled Quota Update",
74 u"Cancelled Quota Updates",
75 count
76 )
77
78 def delete(self, request, obj_id):
79 result = adjutant.task_cancel(request, obj_id)
80 if not result or result.status_code not in [200, 202]:
81 exception = exceptions.NotAvailable()
82 exception._safe_message = False
83 raise exception
84
85 def allowed(self, request, task=None):
86 if task:
87 return task.status == "Awaiting Approval"
88 return True
89
90
91 class ViewRegion(tables.LinkAction):
92 name = "view_region"
93 verbose_name = _("View Region")
94 url = "horizon:management:quota:region_detail"
95
96
97 class ViewSize(tables.LinkAction):
98 name = "view_size"
99 verbose_name = _("View Size")
100 url = "horizon:management:quota:size_detail"
101
102
103 class UpdateQuotaRow(tables.Row):
104 def load_cells(self, resource=None):
105 super(UpdateQuotaRow, self).load_cells(resource)
106 resource = self.datum
107 if resource.important is False:
108 self.attrs['hide'] = True
109 self.attrs['style'] = 'display: none;'
110
111 self.attrs['size_blob'] = json.dumps(self.datum.size_blob)
112
113
114 class RegionQuotaDetailTable(tables.DataTable):
115 service = tables.Column("service", verbose_name=_("Service"),
116 filters=(adjutant.get_service_type, ))
117 name = tables.Column(get_quota_name, verbose_name=_("Resource Name"),)
118 value = tables.Column("current_quota", verbose_name=_("Resource Quota"), )
119 usage = tables.Column("current_usage", verbose_name=_("Current Usage"))
120 percent = tables.Column("percent", verbose_name=_("Percentage of Use"),
121 filters=(display_as_percent, ))
122
123
124 class QuotaDetailUsageTable(tables.DataTable):
125 service = tables.Column("service", verbose_name=_("Service"),
126 filters=(adjutant.get_service_type, ))
127 name = tables.Column(get_quota_name, verbose_name=_("Resource Name"),)
128 value = tables.Column("value", verbose_name=_("Quota Value"), )
129 current_quota = tables.Column("current_quota",
130 verbose_name=_("Current Quota "
131 "(Current Region)"), )
132
133
134 class RegionOverviewTable(tables.DataTable):
135 region = tables.Column("region", verbose_name=_("Region Name"),
136 link=("horizon:management:quota:region_detail"))
137 quota_size = tables.Column("quota_size",
138 verbose_name=_("Current Quota Size"),
139 filters=(to_caps, ))
140 preapproved_quotas = tables.Column(
141 "preapproved_quotas", filters=(to_caps, ),
142 verbose_name=_("Preapproved Quota Sizes *"))
143
144 class Meta(object):
145 name = "region_overview"
146 row_actions = (UpdateQuota, ViewRegion)
147 verbose_name = _("Current Quotas")
148 hidden_title = False
149
150
151 class QuotaTasksTable(tables.DataTable):
152 quota_size = tables.Column(
153 "size",
154 verbose_name=_("Proposed Size"),
155 filters=(to_caps, ))
156 regions = tables.Column("regions", verbose_name=_("For Regions"))
157 user = tables.Column("user", verbose_name=_("Requested By"))
158 created = tables.Column("created", verbose_name=_("Requested On"))
159 valid = tables.Column("valid", verbose_name=_("Valid"))
160 stats = tables.Column("status", verbose_name=_("Status"))
161
162 class Meta(object):
163 name = "quota_tasks"
164 row_actions = (CancelQuotaTask, )
165 verbose_name = _("Previous Quota Changes")
166 hidden_title = False
167
168
169 class SizeOverviewTable(tables.DataTable):
170 id = tables.Column("id", hidden=True)
171 size = tables.Column("name", verbose_name=_("Size Name"),
172 filters=(to_caps, ))
173
174 class Meta(object):
175 name = "size_overview"
176 row_actions = (ViewSize, )
177 verbose_name = _("Quota Sizes")
178 hidden_title = False
179
180
181 class ChangeSizeDisplayTable(tables.DataTable):
182 service = tables.Column("service", verbose_name=_("Service"),
183 filters=(adjutant.get_service_type, ),
184 hidden=True)
185 name = tables.Column(get_quota_name, verbose_name=_("Resource"),)
186 current_quota = tables.Column("current_quota",
187 verbose_name=_("Current Quota"), )
188 usage = tables.Column("current_usage", verbose_name=_("Current Usage"))
189 value = tables.Column("value", verbose_name=_("New Quota Value"), )
190
191 class Meta(object):
192 name = 'change_size'
193 row_class = UpdateQuotaRow