Merge tag '0.10.2'
[ganeti_webmgr.git] / ganeti_web / views / tables.py
blob0d7a08a057eff14c6c6cabfdf0f7dd355b185b50
1 from django.utils.safestring import mark_safe
3 from django_tables2 import (Table, Column, LinkColumn, TemplateColumn,
4 DateTimeColumn)
5 from django_tables2.utils import A
7 from ganeti_web.templatetags.webmgr_tags import (render_storage, render_os,
8 abbreviate_fqdn,
9 format_job_op)
10 from ganeti_web.utilities import hv_prettify
13 class BaseTable(Table):
14 def __init__(self, *args, **kwargs):
15 kwargs['template'] = kwargs.get("template", "table.html")
16 return super(BaseTable, self).__init__(*args, **kwargs)
19 class BaseVMTable(BaseTable):
21 status = TemplateColumn(
22 template_name="ganeti/virtual_machine/vmfield_status.html",
23 attrs={"th": {"class": "status_indicator"}}
25 cluster = LinkColumn(
26 "cluster-detail",
27 args=[A("cluster.slug")],
28 accessor="cluster.slug",
29 verbose_name='cluster'
31 hostname = LinkColumn(
32 "instance-detail",
33 kwargs={"cluster_slug": A("cluster.slug"),
34 "instance": A("hostname")},
35 verbose_name='name',
37 owner = Column()
38 node = Column(verbose_name='node', accessor="primary_node")
39 operating_system = Column(verbose_name='OS')
40 ram = Column(verbose_name='RAM')
41 disk_size = Column(verbose_name='disk space')
42 virtual_cpus = Column(verbose_name='vCPUs')
44 class Meta:
45 sequence = ("status", "hostname", "cluster", "...")
46 order_by = ("hostname")
47 empty_text = "No Virtual Machines"
49 def render_disk_size(self, value):
50 return render_storage(value)
52 def render_ram(self, value):
53 return render_storage(value)
55 def render_operating_system(self, value):
56 return render_os(value)
58 def render_node(self, value):
59 return abbreviate_fqdn(value)
61 def render_owner(self, value):
62 url = value.get_absolute_url()
63 url_str = '<a href="{}">{}</a>'
64 return mark_safe(url_str.format(url, value))
67 class VMTable(BaseVMTable):
68 class Meta:
69 sequence = ("status", "hostname", "cluster", "...")
70 order_by = ("hostname")
71 empty_text = "No Virtual Machines"
74 class ClusterVMTable(BaseVMTable):
75 class Meta:
76 exclude = ("cluster",)
77 order_by = ("hostname")
78 empty_text = "This cluster has no Virtual Machines"
81 class NodeVMTable(BaseVMTable):
82 class Meta:
83 exclude = ("cluster", "node")
84 order_by = ("hostname")
85 empty_text = ("This node has no Virtual Machines "
86 "assigned to it as this role.")
89 class VMTemplateTable(BaseTable):
91 template_name = LinkColumn(
92 "template-detail",
93 args=[A("cluster.slug"), A("template_name")],
94 verbose_name="name"
96 description = Column()
97 cluster = LinkColumn(
98 "cluster-detail",
99 args=[A("cluster.slug")],
100 accessor="cluster.slug",
101 verbose_name="cluster"
103 os = Column(verbose_name='OS')
104 memory = Column(verbose_name='RAM')
105 disk_space = Column()
106 vcpus = Column(verbose_name='vCPUs')
108 class Meta:
109 sequence = ("template_name", "description", "cluster", "os", "memory",
110 "disk_space", "vcpus")
111 order_by = ("template_name")
112 empty_text = "No Templates"
114 def render_os(self, value):
115 return render_os(value)
117 def render_memory(self, value):
118 return render_storage(value)
120 def render_disk_space(self, value):
121 return render_storage(value)
124 class ClusterTable(BaseTable):
126 cluster = LinkColumn(
127 "cluster-detail",
128 args=[A("slug")],
129 accessor="slug",
130 verbose_name='cluster'
132 description = Column()
133 version = Column(
134 accessor="info.software_version",
135 orderable=False,
136 default="unknown"
138 hypervisor = Column(
139 accessor="info.default_hypervisor",
140 orderable=False,
141 default="unknown"
143 master_node = LinkColumn(
144 "node-detail",
145 kwargs={"cluster_slug": A("slug"),
146 "host": A("info.master")},
147 accessor="info.master",
148 orderable=False,
149 default="unknown"
151 nodes = Column(accessor="nodes.count", orderable=False)
152 vms = Column(accessor="virtual_machines.count", verbose_name='VMs',
153 orderable=False)
155 class Meta:
156 empty_text = "No Clusters"
157 sequence = ("cluster", "description", "version", "hypervisor",
158 "master_node", "nodes", "vms")
159 order_by = ("cluster")
161 def render_hypervisor(self, value):
162 return hv_prettify(value)
165 class JobTable(BaseTable):
166 job_id = LinkColumn(
167 'job-detail',
168 args=[A("cluster.slug"), A("job_id")]
170 cluster = LinkColumn(
171 "cluster-detail",
172 args=[A("cluster.slug")],
173 accessor="cluster.slug",
174 verbose_name='cluster'
176 object = TemplateColumn(
177 template_name="ganeti/overview/object_link.html",
178 accessor="obj",
179 orderable=False # Cannot access generic fk to get obj name for sort
181 operation = Column(accessor="op", verbose_name="operation")
182 # Extra space is because it doesn't like the word status for some reason.
183 status = Column(verbose_name="status ")
184 finished = DateTimeColumn()
186 class Meta:
187 empty_text = "No jobs"
188 order_by = ("job_id")
190 def render_operation(self, value):
191 return format_job_op(value)
194 class ClusterJobTable(JobTable):
196 class Meta:
197 exclude = ("cluster",)
198 empty_text = "No jobs available for this cluster"