tables.py 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

18 19
from __future__ import absolute_import

20
from django.contrib.auth.models import Group, User
21
from django.utils.translation import ugettext_lazy as _, ugettext
22 23
from django.utils.html import mark_safe

24
from django_tables2 import Table, A
25 26 27 28
from django_tables2.columns import (
    TemplateColumn, Column, LinkColumn, BooleanColumn
)
from django_sshkey.models import UserKey
29

30
from storage.models import Disk
31
from vm.models import Node, InstanceTemplate, Lease
32
from dashboard.models import ConnectCommand
33 34


35 36 37 38 39 40 41 42
class FileSizeColumn(Column):
    def render(self, value):
        from sizefield.utils import filesizeformat

        size = filesizeformat(value)
        return size


43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
class ApplianceColumn(TemplateColumn):
    def render(self, *args, **kwargs):
        value = super(ApplianceColumn, self).render(*args, **kwargs)

        abbr = '<abbr title="%s">%s</abbr>'
        appliance = kwargs['record'].get_appliance()
        if appliance is None:
            return value
        elif isinstance(appliance, InstanceTemplate):
            # Translators: [T] as Template
            title, text = ugettext("Template"), ugettext("[T]")
        else:
            # Translators: [VM] as Virtual Machine
            title, text = ugettext("Virtual machine"), ugettext("[VM]")
        return mark_safe("%s %s" % (abbr % (title, text), value))

59

60 61 62 63
class NodeListTable(Table):

    pk = Column(
        verbose_name="ID",
64
        attrs={'th': {'class': 'node-list-table-thin'}},
65 66
    )

67
    overcommit = Column(
68
        verbose_name=_("Overcommit"),
69
        attrs={'th': {'class': 'node-list-table-thin'}},
70 71
    )

72 73
    get_status_display = Column(
        verbose_name=_("Status"),
74
        attrs={'th': {'class': 'node-list-table-thin'}},
75
        order_by=("enabled", "schedule_enabled"),
76 77 78
    )

    name = TemplateColumn(
79 80
        template_name="dashboard/node-list/column-name.html",
        order_by="normalized_name"
81 82 83 84 85 86 87
    )

    priority = Column(
        attrs={'th': {'class': 'node-list-table-thin'}},
    )

    number_of_VMs = TemplateColumn(
88
        verbose_name=_("Number of VMs"),
89
        template_name='dashboard/node-list/column-vm.html',
90
        attrs={'th': {'class': 'node-list-table-thin'}},
91 92
    )

93
    monitor = TemplateColumn(
Kálmán Viktor committed
94
        verbose_name=_("Monitor"),
95 96
        template_name='dashboard/node-list/column-monitor.html',
        attrs={'th': {'class': 'node-list-table-monitor'}},
97
        orderable=False,
98 99
    )

100 101 102 103 104 105
    minion_online = BooleanColumn(
        verbose_name=_("Minion online"),
        attrs={'th': {'class': 'node-list-table-thin'}},
        orderable=False,
    )

106 107 108 109
    class Meta:
        model = Node
        attrs = {'class': ('table table-bordered table-striped table-hover '
                           'node-list-table')}
110
        fields = ('pk', 'name', 'host', 'get_status_display', 'priority',
111
                  'minion_online', 'overcommit', 'number_of_VMs', )
112

113

114
class GroupListTable(Table):
115 116 117 118 119 120 121 122 123 124 125
    pk = TemplateColumn(
        template_name='dashboard/group-list/column-id.html',
        verbose_name="ID",
        attrs={'th': {'class': 'group-list-table-thin'}},
    )

    name = TemplateColumn(
        template_name="dashboard/group-list/column-name.html"
    )

    number_of_users = TemplateColumn(
126
        orderable=False,
127
        verbose_name=_("Number of users"),
128 129 130 131 132
        template_name='dashboard/group-list/column-users.html',
        attrs={'th': {'class': 'group-list-table-admin'}},
    )

    admin = TemplateColumn(
133
        orderable=False,
134
        verbose_name=_("Admin"),
135 136 137 138 139
        template_name='dashboard/group-list/column-admin.html',
        attrs={'th': {'class': 'group-list-table-admin'}},
    )

    actions = TemplateColumn(
140
        orderable=False,
141
        verbose_name=_("Actions"),
142 143 144 145 146
        attrs={'th': {'class': 'group-list-table-thin'}},
        template_code=('{% include "dashboard/group-list/column-'
                       'actions.html" with btn_size="btn-xs" %}'),
    )

147 148 149 150
    class Meta:
        model = Group
        attrs = {'class': ('table table-bordered table-striped table-hover '
                           'group-list-table')}
151
        fields = ('pk', 'name', )
152
        order_by = ('pk', )
153 154 155


class UserListTable(Table):
156 157 158
    username = LinkColumn(
        'dashboard.views.profile',
        args=[A('username')],
159
    )
160 161 162 163 164
    profile__org_id = LinkColumn(
        'dashboard.views.profile',
        accessor='profile.org_id',
        args=[A('username')],
        verbose_name=_('Organization ID')
165
    )
166 167 168 169 170 171

    is_superuser = BooleanColumn(
        verbose_name=mark_safe(
            _('<abbr data-placement="left" title="Superuser status">SU</abbr>')
        )
    )
172
    is_active = BooleanColumn()
173 174 175

    class Meta:
        model = User
176
        template = "django_tables2/with_pagination.html"
177 178 179
        attrs = {'class': ('table table-bordered table-striped table-hover')}
        fields = ('username', 'last_name', 'first_name', 'profile__org_id',
                  'email', 'is_active', 'is_superuser')
180
        order_by = ('username', )
181 182


183
class TemplateListTable(Table):
184 185
    name = TemplateColumn(
        template_name="dashboard/template-list/column-template-name.html",
186
        attrs={'th': {'data-sort': "string"}}
187
    )
188 189
    resources = TemplateColumn(
        template_name="dashboard/template-list/column-template-resources.html",
190
        verbose_name=_("Resources"),
191
        attrs={'th': {'data-sort': "int"}},
192
        order_by=("ram_size"),
193 194 195 196
    )
    lease = TemplateColumn(
        "{{ record.lease.name }}",
        verbose_name=_("Lease"),
197 198 199 200 201 202 203
        attrs={'th': {'data-sort': "string"}}
    )
    system = Column(
        attrs={'th': {'data-sort': "string"}}
    )
    access_method = Column(
        attrs={'th': {'data-sort': "string"}}
204
    )
205 206 207 208 209
    owner = TemplateColumn(
        template_name="dashboard/template-list/column-template-owner.html",
        verbose_name=_("Owner"),
        attrs={'th': {'data-sort': "string"}}
    )
210 211 212 213
    created = TemplateColumn(
        template_name="dashboard/template-list/column-template-created.html",
        verbose_name=_("Created at"),
    )
214 215 216 217 218
    running = TemplateColumn(
        template_name="dashboard/template-list/column-template-running.html",
        verbose_name=_("Running"),
        attrs={'th': {'data-sort': "int"}},
    )
219 220
    actions = TemplateColumn(
        verbose_name=_("Actions"),
221 222
        template_name="dashboard/template-list/column-template-actions.html",
        attrs={'th': {'class': 'template-list-table-thin'}},
223
        orderable=False,
224 225 226 227 228 229
    )

    class Meta:
        model = InstanceTemplate
        attrs = {'class': ('table table-bordered table-striped table-hover'
                           ' template-list-table')}
230 231
        fields = ('name', 'resources', 'system', 'access_method', 'lease',
                  'owner', 'created', 'running', 'actions', )
232

233
        prefix = "template-"
234 235 236


class LeaseListTable(Table):
237
    name = LinkColumn(
238 239 240 241
        'dashboard.views.lease-detail',
        args=[A('pk')],
    )

242
    suspend_interval_seconds = TemplateColumn(
243 244 245
        "{{ record.get_readable_suspend_time }}"
    )

246
    delete_interval_seconds = TemplateColumn(
247 248 249 250 251
        "{{ record.get_readable_delete_time }}"
    )

    actions = TemplateColumn(
        verbose_name=_("Actions"),
252 253
        template_name="dashboard/template-list/column-lease-actions.html",
        orderable=False,
254 255 256 257 258 259
    )

    class Meta:
        model = Lease
        attrs = {'class': ('table table-bordered table-striped table-hover'
                           ' lease-list-table')}
260 261
        fields = ('name', 'suspend_interval_seconds',
                  'delete_interval_seconds', )
262
        prefix = "lease-"
263
        empty_text = _("No available leases.")
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291


class UserKeyListTable(Table):
    name = LinkColumn(
        'dashboard.views.userkey-detail',
        args=[A('pk')],
        verbose_name=_("Name"),
        attrs={'th': {'data-sort': "string"}}
    )

    fingerprint = Column(
        verbose_name=_("Fingerprint"),
        attrs={'th': {'data-sort': "string"}}
    )

    created = Column(
        verbose_name=_("Created at"),
        attrs={'th': {'data-sort': "string"}}
    )

    actions = TemplateColumn(
        verbose_name=_("Actions"),
        template_name="dashboard/userkey-list/column-userkey-actions.html",
        orderable=False,
    )

    class Meta:
        model = UserKey
292 293
        attrs = {'class': ('table table-bordered table-striped table-hover'),
                 'id': "profile-key-list-table"}
294
        fields = ('name', 'fingerprint', 'created', 'actions')
295 296
        prefix = "key-"
        empty_text = _("You haven't added any public keys yet.")
297 298 299


class ConnectCommandListTable(Table):
300 301 302
    name = LinkColumn(
        'dashboard.views.connect-command-detail',
        args=[A('pk')],
303 304
        attrs={'th': {'data-sort': "string"}}
    )
305 306
    access_method = Column(
        verbose_name=_("Access method"),
307 308 309 310 311 312 313 314
        attrs={'th': {'data-sort': "string"}}
    )
    template = Column(
        verbose_name=_("Template"),
        attrs={'th': {'data-sort': "string"}}
    )
    actions = TemplateColumn(
        verbose_name=_("Actions"),
315 316
        template_name=("dashboard/connect-command-list/column-command"
                       "-actions.html"),
317 318 319 320 321
        orderable=False,
    )

    class Meta:
        model = ConnectCommand
322 323 324
        attrs = {'class': ('table table-bordered table-striped table-hover'),
                 'id': "profile-command-list-table"}
        fields = ('name', 'access_method',  'template', 'actions')
325
        prefix = "cmd-"
326 327 328 329
        empty_text = _(
            "You don't have any custom connection commands yet. You can "
            "specify commands to be displayed on VM detail pages instead of "
            "the defaults.")
330 331 332


class DiskListTable(Table):
333 334 335 336 337
    pk = LinkColumn(
        'dashboard.views.disk-detail',
        args=[A('pk')],
        verbose_name=_("ID"),
    )
338
    size = FileSizeColumn()
339 340 341 342 343 344 345 346
    appliance = ApplianceColumn(
        template_name="dashboard/storage/column-appliance.html",
        verbose_name=_("Appliance"),
        orderable=False,
    )
    is_ready = BooleanColumn(
        verbose_name=_("ready")
    )
347 348 349 350 351

    class Meta:
        model = Disk
        attrs = {'class': "table table-bordered table-striped table-hover",
                 'id': "disk-list-table"}
352
        fields = ("pk", "appliance", "filename", "size", "is_ready")
353 354
        prefix = "disk-"
        order_by = ("-pk", )
355 356
        per_page = 15
        empty_text = _("No disk found.")