views.py 3.85 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 firewall.models import Vlan
9
from storage.models import Disk
10
from django.core import signing
11

12 13
import json

14 15

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

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

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

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

39

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


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


class VmCreate(TemplateView):

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

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

    def get_context_data(self, **kwargs):
        context = super(VmCreate, self).get_context_data(**kwargs)
113
        # TODO acl
114
        context.update({
115
            'templates': InstanceTemplate.objects.all(),
116 117
            'vlans': Vlan.objects.all(),
            'disks': Disk.objects.all()
118 119 120
        })

        return context