Fix error when sorting Job list by object.
[ganeti_webmgr.git] / ganeti_web / views / tables.py
blobf8561b4373f1c7d2b130e07db5eaecf655dc796e
1 from django_tables2 import (Table, Column, LinkColumn, TemplateColumn,
2 DateTimeColumn)
3 from django_tables2.utils import A
5 from ganeti_web.templatetags.webmgr_tags import (render_storage, render_os,
6 abbreviate_fqdn,
7 format_job_op)
8 from ganeti_web.utilities import hv_prettify
11 class BaseVMTable(Table):
13 status = TemplateColumn(
14 template_name="ganeti/virtual_machine/vmfield_status.html",
15 attrs={"th": {"class": "status_indicator"}}
17 cluster = LinkColumn(
18 "cluster-detail",
19 args=[A("cluster.slug")],
20 accessor="cluster.slug",
21 verbose_name='cluster'
23 hostname = LinkColumn(
24 "instance-detail",
25 kwargs={"cluster_slug": A("cluster.slug"),
26 "instance": A("hostname")},
27 verbose_name='name',
29 owner = LinkColumn(
30 "user-detail-name",
31 args=[A("owner")],
33 node = Column(verbose_name='node', accessor="primary_node")
34 operating_system = Column(verbose_name='OS')
35 ram = Column(verbose_name='RAM')
36 disk_size = Column(verbose_name='disk space')
37 virtual_cpus = Column(verbose_name='vCPUs')
39 class Meta:
40 sequence = ("status", "hostname", "cluster", "...")
41 order_by = ("hostname")
42 empty_text = "No Virtual Machines"
44 def __init__(self, *args, **kwargs):
45 self.template = "table.html"
46 return super(BaseVMTable, self).__init__(*args, **kwargs)
48 def render_disk_size(self, value):
49 return render_storage(value)
51 def render_ram(self, value):
52 return render_storage(value)
54 def render_operating_system(self, value):
55 return render_os(value)
57 def render_node(self, value):
58 return abbreviate_fqdn(value)
61 class VMTable(BaseVMTable):
62 class Meta:
63 sequence = ("status", "hostname", "cluster", "...")
64 order_by = ("hostname")
65 empty_text = "No Virtual Machines"
68 class ClusterVMTable(BaseVMTable):
69 class Meta:
70 exclude = ("cluster",)
71 order_by = ("hostname")
72 empty_text = "This cluster has no Virtual Machines"
75 class NodeVMTable(BaseVMTable):
76 class Meta:
77 exclude = ("cluster", "node")
78 order_by = ("hostname")
79 empty_text = ("This node has no Virtual Machines "
80 "assigned to it as this role.")
83 class VMTemplateTable(Table):
85 template_name = LinkColumn(
86 "template-detail",
87 args=[A("cluster.slug"), A("template_name")],
88 verbose_name="name"
90 description = Column()
91 cluster = LinkColumn(
92 "cluster-detail",
93 args=[A("cluster.slug")],
94 accessor="cluster.slug",
95 verbose_name="cluster"
97 os = Column(verbose_name='OS')
98 memory = Column(verbose_name='RAM')
99 disk_space = Column()
100 vcpus = Column(verbose_name='vCPUs')
102 class Meta:
103 sequence = ("template_name", "description", "cluster", "os", "memory",
104 "disk_space", "vcpus")
105 order_by = ("template_name")
106 empty_text = "No Templates"
108 def render_os(self, value):
109 return render_os(value)
111 def render_memory(self, value):
112 return render_storage(value)
114 def render_disk_space(self, value):
115 return render_storage(value)
118 class ClusterTable(Table):
120 cluster = LinkColumn(
121 "cluster-detail",
122 args=[A("slug")],
123 accessor="slug",
124 verbose_name='cluster'
126 description = Column()
127 version = Column(
128 accessor="info.software_version",
129 orderable=False,
130 default="unknown"
132 hypervisor = Column(
133 accessor="info.default_hypervisor",
134 orderable=False,
135 default="unknown"
137 master_node = LinkColumn(
138 "node-detail",
139 kwargs={"cluster_slug": A("slug"),
140 "host": A("info.master")},
141 accessor="info.master",
142 orderable=False,
143 default="unknown"
145 nodes = Column(accessor="nodes.count", orderable=False)
146 vms = Column(accessor="virtual_machines.count", verbose_name='VMs',
147 orderable=False)
149 class Meta:
150 empty_text = "No Clusters"
151 sequence = ("cluster", "description", "version", "hypervisor",
152 "master_node", "nodes", "vms")
153 order_by = ("cluster")
155 def render_hypervisor(self, value):
156 return hv_prettify(value)
159 class JobTable(Table):
160 job_id = LinkColumn(
161 'job-detail',
162 args=[A("cluster.slug"), A("job_id")]
164 cluster = LinkColumn(
165 "cluster-detail",
166 args=[A("cluster.slug")],
167 accessor="cluster.slug",
168 verbose_name='cluster'
170 object = TemplateColumn(
171 template_name="ganeti/overview/object_link.html",
172 accessor="obj",
173 orderable=False # Cannot access generic fk to get obj name for sort
175 operation = Column(accessor="op", verbose_name="operation")
176 # Extra space is because it doesn't like the word status for some reason.
177 status = Column(verbose_name="status ")
178 finished = DateTimeColumn()
180 class Meta:
181 empty_text = "No jobs"
182 order_by = ("job_id")
184 def render_operation(self, value):
185 return format_job_op(value)
188 class ClusterJobTable(JobTable):
190 class Meta:
191 exclude = ("cluster",)
192 empty_text = "No jobs available for this cluster"