views.py 3.71 KB
Newer Older
1
from django.http import HttpResponse
2
from django.views.generic import TemplateView, DetailView
3 4 5
from django_tables2 import SingleTableView

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

7
from vm.models import Instance, InstanceTemplate, InterfaceTemplate
8
from django.core import signing
9

10 11
import json

12 13

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

16
    def get_context_data(self, **kwargs):
17 18 19 20 21
        if self.request.user.is_authenticated():
            user = self.request.user
        else:
            user = None

22
        instances = Instance.objects.filter(owner=user)
23 24
        context = super(IndexView, self).get_context_data(**kwargs)
        context.update({
25
            'instances': instances[:5],
26
            'more_instances': instances.count() - len(instances[:5])
27 28 29
        })

        context.update({
30 31
            'running_vms': instances.filter(state='RUNNING'),
            'running_vm_num': instances.filter(state='RUNNING').count(),
32 33
            'stopped_vm_num': instances.exclude(
                state__in=['RUNNING', 'NOSTATE']).count()
34 35 36
        })
        return context

37

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


56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
class TemplateDetail(DetailView):
    model = InstanceTemplate

    def get(self, request, *args, **kwargs):
        if request.is_ajax():
            template = InstanceTemplate.objects.get(pk=kwargs['pk'])
            template = {
                'num_cores': template.num_cores,
                'ram_size': template.ram_size,
                'priority': template.priority,
                'arch': template.arch,
                'description': template.description,
                'system': template.system,
                'name': template.name,
                'disks': [{'pk': d.pk, 'name': d.name}
                          for d in template.disks.all()],
                'network': [
                    {'vlan_pk': i.vlan.pk, 'vlan': i.vlan.name,
                     'managed': i.managed}
                    for i in InterfaceTemplate.objects.filter(
                        template=self.get_object()).all()
                ]
            }
            return HttpResponse(json.dumps(template),
                                content_type="application/json")
        else:
            # return super(TemplateDetail, self).get(request, *args, **kwargs)
            return HttpResponse('soon')


86
class VmList(SingleTableView):
Kálmán Viktor committed
87
    template_name = "dashboard/vm-list.html"
88 89
    model = Instance
    table_class = VmListTable
90
    table_pagination = False
91 92 93 94 95 96


class VmCreate(TemplateView):

    def get_template_names(self):
        if self.request.is_ajax():
97
            return ['dashboard/modal-wrapper.html']
98
        else:
99
            return ['dashboard/nojs-wrapper.html']
100 101 102

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
103 104 105 106
        context.update({
            'template': 'dashboard/vm-create.html',
            'box_title': 'Create a VM'
        })
107
        return self.render_to_response(context)
108 109 110 111 112 113 114 115 116

    def get_context_data(self, **kwargs):
        context = super(VmCreate, self).get_context_data(**kwargs)
        context.update({
            # TODO acl
            'templates': InstanceTemplate.objects.all()
        })

        return context