Move default template for tables out of template into table base class
[ganeti_webmgr.git] / ganeti_web / views / tables.py
blob77cd0375cced6b1d092da8e94cc72b23c67d9dd6
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 BaseTable(Table):
12 def __init__(self, *args, **kwargs):
13 kwargs['template'] = kwargs.get("template", "table.html")
14 return super(BaseTable, self).__init__(*args, **kwargs)
17 class BaseVMTable(BaseTable):
19 status = TemplateColumn(
20 template_name="ganeti/virtual_machine/vmfield_status.html",
21 attrs={"th": {"class": "status_indicator"}}
23 cluster = LinkColumn(
24 "cluster-detail",
25 args=[A("cluster.slug")],
26 accessor="cluster.slug",
27 verbose_name='cluster'
29 hostname = LinkColumn(
30 "instance-detail",
31 kwargs={"cluster_slug": A("cluster.slug"),
32 "instance": A("hostname")},
33 verbose_name='name',
35 owner = LinkColumn(
36 "user-detail-name",
37 args=[A("owner")],
39 node = Column(verbose_name='node', accessor="primary_node")
40 operating_system = Column(verbose_name='OS')
41 ram = Column(verbose_name='RAM')
42 disk_size = Column(verbose_name='disk space')
43 virtual_cpus = Column(verbose_name='vCPUs')
45 class Meta:
46 sequence = ("status", "hostname", "cluster", "...")
47 order_by = ("hostname")
48 empty_text = "No Virtual Machines"
50 def render_disk_size(self, value):
51 return render_storage(value)
53 def render_ram(self, value):
54 return render_storage(value)
56 def render_operating_system(self, value):
57 return render_os(value)
59 def render_node(self, value):
60 return abbreviate_fqdn(value)
63 class VMTable(BaseVMTable):
64 class Meta:
65 sequence = ("status", "hostname", "cluster", "...")
66 order_by = ("hostname")
67 empty_text = "No Virtual Machines"
70 class ClusterVMTable(BaseVMTable):
71 class Meta:
72 exclude = ("cluster",)
73 order_by = ("hostname")
74 empty_text = "This cluster has no Virtual Machines"
77 class NodeVMTable(BaseVMTable):
78 class Meta:
79 exclude = ("cluster", "node")
80 order_by = ("hostname")
81 empty_text = ("This node has no Virtual Machines "
82 "assigned to it as this role.")
85 class VMTemplateTable(BaseTable):
87 template_name = LinkColumn(
88 "template-detail",
89 args=[A("cluster.slug"), A("template_name")],
90 verbose_name="name"
92 description = Column()
93 cluster = LinkColumn(
94 "cluster-detail",
95 args=[A("cluster.slug")],
96 accessor="cluster.slug",
97 verbose_name="cluster"
99 os = Column(verbose_name='OS')
100 memory = Column(verbose_name='RAM')
101 disk_space = Column()
102 vcpus = Column(verbose_name='vCPUs')
104 class Meta:
105 sequence = ("template_name", "description", "cluster", "os", "memory",
106 "disk_space", "vcpus")
107 order_by = ("template_name")
108 empty_text = "No Templates"
110 def render_os(self, value):
111 return render_os(value)
113 def render_memory(self, value):
114 return render_storage(value)
116 def render_disk_space(self, value):
117 return render_storage(value)
120 class ClusterTable(BaseTable):
122 cluster = LinkColumn(
123 "cluster-detail",
124 args=[A("slug")],
125 accessor="slug",
126 verbose_name='cluster'
128 description = Column()
129 version = Column(
130 accessor="info.software_version",
131 orderable=False,
132 default="unknown"
134 hypervisor = Column(
135 accessor="info.default_hypervisor",
136 orderable=False,
137 default="unknown"
139 master_node = LinkColumn(
140 "node-detail",
141 kwargs={"cluster_slug": A("slug"),
142 "host": A("info.master")},
143 accessor="info.master",
144 orderable=False,
145 default="unknown"
147 nodes = Column(accessor="nodes.count", orderable=False)
148 vms = Column(accessor="virtual_machines.count", verbose_name='VMs',
149 orderable=False)
151 class Meta:
152 empty_text = "No Clusters"
153 sequence = ("cluster", "description", "version", "hypervisor",
154 "master_node", "nodes", "vms")
155 order_by = ("cluster")
157 def render_hypervisor(self, value):
158 return hv_prettify(value)
161 class JobTable(BaseTable):
162 job_id = LinkColumn(
163 'job-detail',
164 args=[A("cluster.slug"), A("job_id")]
166 cluster = LinkColumn(
167 "cluster-detail",
168 args=[A("cluster.slug")],
169 accessor="cluster.slug",
170 verbose_name='cluster'
172 object = TemplateColumn(
173 template_name="ganeti/overview/object_link.html",
174 accessor="obj",
175 orderable=False # Cannot access generic fk to get obj name for sort
177 operation = Column(accessor="op", verbose_name="operation")
178 # Extra space is because it doesn't like the word status for some reason.
179 status = Column(verbose_name="status ")
180 finished = DateTimeColumn()
182 class Meta:
183 empty_text = "No jobs"
184 order_by = ("job_id")
186 def render_operation(self, value):
187 return format_job_op(value)
190 class ClusterJobTable(JobTable):
192 class Meta:
193 exclude = ("cluster",)
194 empty_text = "No jobs available for this cluster"