| 1 |
# Copyright 2014 Intel Corporation |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from django.http import JsonResponse |
| 17 |
from django.urls import reverse |
| 18 |
from django.urls import reverse_lazy |
| 19 |
from django.utils.translation import gettext_lazy as _ |
| 20 |
from horizon import exceptions |
| 21 |
from horizon import forms |
| 22 |
from horizon import tables |
| 23 |
from horizon import tabs |
| 24 |
from horizon.utils import memoized |
| 25 |
from horizon import views |
| 26 |
|
| 27 |
from blazar_dashboard import api |
| 28 |
from blazar_dashboard import conf |
| 29 |
from blazar_dashboard.content.leases import forms as project_forms |
| 30 |
from blazar_dashboard.content.leases import tables as project_tables |
| 31 |
from blazar_dashboard.content.leases import tabs as project_tabs |
| 32 |
|
| 33 |
|
| 34 |
class IndexView(tables.DataTableView): |
| 35 |
table_class = project_tables.LeasesTable |
| 36 |
template_name = 'project/leases/index.html' |
| 37 |
|
| 38 |
def get_data(self): |
| 39 |
try: |
| 40 |
leases = api.client.lease_list(self.request) |
| 41 |
except Exception: |
| 42 |
leases = [] |
| 43 |
msg = _('Unable to retrieve lease information.') |
| 44 |
exceptions.handle(self.request, msg) |
| 45 |
return leases |
| 46 |
|
| 47 |
|
| 48 |
class CalendarView(views.APIView): |
| 49 |
template_name = 'project/leases/calendar.html' |
| 50 |
|
| 51 |
titles = { |
| 52 |
"host": _("Host Calendar"), |
| 53 |
} |
| 54 |
|
| 55 |
def get_data(self, request, context, *args, **kwargs): |
| 56 |
if context["resource_type"] not in self.titles: |
| 57 |
raise exceptions.NotFound |
| 58 |
context["calendar_title"] = self.titles[context["resource_type"]] |
| 59 |
return context |
| 60 |
|
| 61 |
|
| 62 |
def calendar_data_view(request, resource_type): |
| 63 |
api_mapping = { |
| 64 |
"host": api.client.reservation_calendar, |
| 65 |
} |
| 66 |
attribute_mapping = { |
| 67 |
"host": conf.host_reservation.get('calendar_attribute'), |
| 68 |
} |
| 69 |
data = {} |
| 70 |
if resource_type not in api_mapping: |
| 71 |
raise exceptions.NotFound |
| 72 |
resources, reservations = api_mapping[resource_type](request) |
| 73 |
data['resources'] = resources |
| 74 |
data['reservations'] = reservations |
| 75 |
# Which attribute to use to determine calendar rows |
| 76 |
data['row_attr'] = attribute_mapping[resource_type] |
| 77 |
return JsonResponse(data) |
| 78 |
|
| 79 |
|
| 80 |
class DetailView(tabs.TabView): |
| 81 |
tab_group_class = project_tabs.LeaseDetailTabs |
| 82 |
template_name = 'project/leases/detail.html' |
| 83 |
|
| 84 |
|
| 85 |
class CreateView(forms.ModalFormView): |
| 86 |
form_class = project_forms.CreateForm |
| 87 |
template_name = 'project/leases/create.html' |
| 88 |
success_url = reverse_lazy('horizon:project:leases:index') |
| 89 |
modal_id = "create_lease_modal" |
| 90 |
modal_header = _("Create Lease") |
| 91 |
submit_label = _("Create Lease") |
| 92 |
submit_url = reverse_lazy('horizon:project:leases:create') |
| 93 |
|
| 94 |
|
| 95 |
class UpdateView(forms.ModalFormView): |
| 96 |
form_class = project_forms.UpdateForm |
| 97 |
template_name = 'project/leases/update.html' |
| 98 |
success_url = reverse_lazy('horizon:project:leases:index') |
| 99 |
modal_header = _("Update Lease") |
| 100 |
|
| 101 |
def get_initial(self): |
| 102 |
initial = super(UpdateView, self).get_initial() |
| 103 |
|
| 104 |
initial['lease'] = self.get_object() |
| 105 |
if initial['lease']: |
| 106 |
initial['lease_id'] = initial['lease'].id |
| 107 |
initial['name'] = initial['lease'].name |
| 108 |
|
| 109 |
return initial |
| 110 |
|
| 111 |
def get_context_data(self, **kwargs): |
| 112 |
context = super(UpdateView, self).get_context_data(**kwargs) |
| 113 |
context['lease'] = self.get_object() |
| 114 |
return context |
| 115 |
|
| 116 |
@memoized.memoized_method |
| 117 |
def get_object(self): |
| 118 |
lease_id = self.kwargs['lease_id'] |
| 119 |
try: |
| 120 |
lease = api.client.lease_get(self.request, lease_id) |
| 121 |
except Exception: |
| 122 |
msg = _("Unable to retrieve lease.") |
| 123 |
redirect = reverse('horizon:project:leases:index') |
| 124 |
exceptions.handle(self.request, msg, redirect=redirect) |
| 125 |
return lease |