Merge "Fixing Network Models"
[pharos-tools.git] / dashboard / src / dashboard / views.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11
12 from django.shortcuts import get_object_or_404
13 from django.views.generic import TemplateView
14 from django.shortcuts import render
15 from django.http import HttpResponseRedirect
16
17 from account.models import Lab
18
19 from resource_inventory.models import Image, HostProfile
20 from workflow.views import create_session
21 from workflow.workflow_manager import ManagerTracker
22
23
24 def lab_list_view(request):
25     labs = Lab.objects.all()
26     context = {"labs": labs}
27
28     return render(request, "dashboard/lab_list.html", context)
29
30
31 def lab_detail_view(request, lab_name):
32     user = None
33     if request.user.is_authenticated:
34         user = request.user
35
36     lab = get_object_or_404(Lab, name=lab_name)
37
38     images = Image.objects.filter(from_lab=lab).filter(public=True)
39     if user:
40         images = images | Image.objects.filter(from_lab=lab).filter(owner=user)
41
42     return render(
43         request,
44         "dashboard/lab_detail.html",
45         {
46             'title': "Lab Overview",
47             'lab': lab,
48             'hostprofiles': lab.hostprofiles.all(),
49             'images': images,
50         }
51     )
52
53
54 def host_profile_detail_view(request):
55
56     return render(
57         request,
58         "dashboard/host_profile_detail.html",
59         {
60             'title': "Host Types",
61         }
62     )
63
64
65 def landing_view(request):
66     manager = None
67     manager_detected = False
68     if 'manager_session' in request.session:
69
70         try:
71             manager = ManagerTracker.managers[request.session['manager_session']]
72
73         except KeyError:
74             pass
75
76     if manager is not None:
77         # no manager detected, don't display continue button
78         manager_detected = True
79
80     if request.method == 'GET':
81         return render(request, 'dashboard/landing.html', {'manager': manager_detected, 'title': "Welcome to the Lab as a Service Dashboard"})
82
83     if request.method == 'POST':
84         try:
85             create = request.POST['create']
86
87             if manager is not None:
88                 del manager
89
90             mgr_uuid = create_session(create, request=request,)
91             request.session['manager_session'] = mgr_uuid
92             return HttpResponseRedirect('/wf/')
93
94         except KeyError:
95             pass
96
97
98 class LandingView(TemplateView):
99     template_name = "dashboard/landing.html"
100
101     def get_context_data(self, **kwargs):
102         context = super(LandingView, self).get_context_data(**kwargs)
103
104         hosts = []
105
106         for host_profile in HostProfile.objects.all():
107             name = host_profile.name
108             description = host_profile.description
109             in_labs = host_profile.labs
110
111             interfaces = host_profile.interfaceprofile
112             storage = host_profile.storageprofile
113             cpu = host_profile.cpuprofile
114             ram = host_profile.ramprofile
115
116             host = (name, description, in_labs, interfaces, storage, cpu, ram)
117             hosts.append(host)
118
119         context.update({'hosts': hosts})
120
121         return context