views.py 2.3 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
            'instances': instances[:5],
23
            'more_instances': instances.count() - len(instances[:5])
24 25
        })

26
        stopped_vm_states = ['PAUSED', 'SHUTDOWN', 'SHUTOFF']
27
        context.update({
28 29 30 31
            'running_vms': instances.filter(state='RUNNING'),
            'running_vm_num': instances.filter(state='RUNNING').count(),
            'stopped_vm_num': instances.filter(
                state__in=stopped_vm_states).count()
32 33 34
        })
        return context

35

36
class VmDetailView(DetailView):
Kálmán Viktor committed
37
    template_name = "dashboard/vm-detail.html"
38 39 40 41 42 43 44 45 46 47 48 49 50 51
    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
52 53


54
class VmList(SingleTableView):
Kálmán Viktor committed
55
    template_name = "dashboard/vm-list.html"
56 57
    model = Instance
    table_class = VmListTable
58
    table_pagination = False
59 60 61 62 63 64 65 66 67 68 69 70 71 72


class VmCreate(TemplateView):

    def get_template_names(self):
        if self.request.is_ajax():
            return ['dashboard/vm-create.html']
        else:
            return ['dashboard/ajax-wrapper.html']

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        if not request.is_ajax():
            context.update({
73 74
                'template': 'dashboard/vm-create.html',
                'box_title': 'Create a VM'
75 76
            })
        return self.render_to_response(context)