views.py 1.49 KB
Newer Older
1
from django.views.generic import TemplateView, DetailView
2 3 4
from django_tables2 import SingleTableView

from tables import VmListTable
Kálmán Viktor committed
5

6
from vm.models import Instance
7
from django.core import signing
8 9 10


class IndexView(TemplateView):
Kálmán Viktor committed
11
    template_name = "dashboard/index.html"
12

13
    def get_context_data(self, **kwargs):
14 15 16 17 18
        if self.request.user.is_authenticated():
            user = self.request.user
        else:
            user = None

19
        instances = Instance.objects.filter(owner=user)
20 21
        context = super(IndexView, self).get_context_data(**kwargs)
        context.update({
22 23 24 25 26
            'instances': instances[:5],
        })

        context.update({
            'more_instances': instances.count() - len(instances[:5])
27 28 29
        })
        return context

30

31
class VmDetailView(DetailView):
Kálmán Viktor committed
32
    template_name = "dashboard/vm-detail.html"
33 34 35 36 37 38 39 40 41 42 43 44 45 46
    queryset = Instance.objects.all()

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        instance = context['instance']
        if instance.node:
            port = instance.vnc_port
            host = instance.node.host.ipv4
            value = signing.dumps({'host': host,
                                   'port': port}, key='asdasd')
            context.update({
                'vnc_url': '%s' % value
            })
        return context
Kálmán Viktor committed
47 48


49
class VmList(SingleTableView):
Kálmán Viktor committed
50
    template_name = "dashboard/vm-list.html"
51 52
    model = Instance
    table_class = VmListTable
53
    table_pagination = False