Debian Codegraph @

blazar_dashboard/content/leases/tables.py

raw
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 datetime import datetime
17 from datetime import timezone
18 from functools import partial
19
20 from django.template import defaultfilters as django_filters
21 from django.utils.translation import gettext_lazy as _
22 from django.utils.translation import ngettext_lazy
23 from horizon import tables
24 from horizon.utils import filters
25
26 from blazar_dashboard import api
27 from blazar_dashboard import conf
28
29
30 class CreateLease(tables.LinkAction):
31 name = "create"
32 verbose_name = _("Create Lease")
33 url = "horizon:project:leases:create"
34 classes = ("ajax-modal",)
35 icon = "plus"
36
37
38 class UpdateLease(tables.LinkAction):
39 name = "update"
40 verbose_name = _("Update Lease")
41 url = "horizon:project:leases:update"
42 classes = ("btn-create", "ajax-modal")
43
44 def allowed(self, request, lease):
45 if datetime.strptime(lease.end_date, '%Y-%m-%dT%H:%M:%S.%f').\
46 replace(tzinfo=timezone.utc) > datetime.now(timezone.utc):
47 return True
48 return False
49
50
51 class ViewHostReservationCalendar(tables.LinkAction):
52 name = "calendar"
53 verbose_name = _("Host Calendar")
54 url = "calendar/host/"
55 classes = ("btn-default", )
56 icon = "calendar"
57
58
59 class DeleteLease(tables.DeleteAction):
60 name = "delete"
61 data_type_singular = _("Lease")
62 data_type_plural = _("Leases")
63 classes = ('btn-danger', 'btn-terminate')
64
65 @staticmethod
66 def action_present(count):
67 return ngettext_lazy(
68 "Delete Lease",
69 "Delete Leases",
70 count
71 )
72
73 @staticmethod
74 def action_past(count):
75 return ngettext_lazy(
76 "Deleted Lease",
77 "Deleted Leases",
78 count
79 )
80
81 def delete(self, request, lease_id):
82 api.client.lease_delete(request, lease_id)
83
84
85 class LeasesTable(tables.DataTable):
86 name = tables.Column("name", verbose_name=_("Lease name"),
87 link="horizon:project:leases:detail",)
88 start_date = tables.Column("start_date", verbose_name=_("Start date"),
89 filters=(filters.parse_isotime,
90 partial(django_filters.date,
91 arg='Y-m-d H:i T')),)
92 end_date = tables.Column("end_date", verbose_name=_("End date"),
93 filters=(filters.parse_isotime,
94 partial(django_filters.date,
95 arg='Y-m-d H:i T')),)
96 status = tables.Column("status", verbose_name=_("Status"),)
97 degraded = tables.Column("degraded", verbose_name=_("Degraded"),
98 filters=(django_filters.yesno,
99 django_filters.capfirst),)
100
101 class Meta(object):
102 name = "leases"
103 verbose_name = _("Leases")
104 table_actions = [CreateLease, DeleteLease, ]
105 if conf.host_reservation.get('enabled'):
106 table_actions.insert(0, ViewHostReservationCalendar)
107 row_actions = (UpdateLease, DeleteLease, )