Merge "Fix error in Booking cleanup task"
[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 booking.models import Booking
18 from account.models import Lab
19
20 from resource_inventory.models import *
21 from workflow.views import *
22 from workflow.workflow_manager import *
23
24
25 def lab_list_view(request):
26     labs = Lab.objects.all()
27     context = {"labs": labs}
28
29     return render(request, "dashboard/lab_list.html", context)
30
31
32 def lab_detail_view(request, lab_name):
33     user = None
34     if request.user.is_authenticated:
35         user = request.user
36
37     lab = get_object_or_404(Lab, name=lab_name)
38
39     images = Image.objects.filter(from_lab=lab).filter(public=True)
40     if user:
41         images = images | Image.objects.filter(from_lab=lab).filter(owner=user)
42
43     return render(request, "dashboard/lab_detail.html",
44                   {'title': "Lab Overview",
45                    'lab': lab,
46                    'hostprofiles': lab.hostprofiles.all(),
47                    'images': images})
48
49
50 def host_profile_detail_view(request):
51
52     return render(request, "dashboard/host_profile_detail.html",
53                    {'title': "Host Types",
54                    })
55
56
57 def landing_view(request):
58     manager = None
59     manager_detected = False
60     if 'manager_session' in request.session:
61
62         try:
63             manager = ManagerTracker.managers[request.session['manager_session']]
64
65
66         except KeyError as e:
67             pass
68
69     if manager is not None:
70         #no manager detected, don't display continue button
71         manager_detected = True
72
73     if request.method == 'GET':
74         return render(request, 'dashboard/landing.html', {'manager': manager_detected, 'title': "Welcome!"})
75
76     if request.method == 'POST':
77         try:
78             create = request.POST['create']
79
80             if manager is not None:
81                 del manager
82
83             mgr_uuid = create_session(create, request=request,)
84             request.session['manager_session'] = mgr_uuid
85             return HttpResponseRedirect('/wf/')
86
87         except KeyError as e:
88             pass
89
90
91 class LandingView(TemplateView):
92     template_name = "dashboard/landing.html"
93
94     def get_context_data(self, **kwargs):
95         context = super(LandingView, self).get_context_data(**kwargs)
96
97         hosts = []
98
99         for host_profile in HostProfile.objects.all():
100             name = host_profile.name
101             description = host_profile.description
102             in_labs = host_profile.labs
103
104             interfaces = host_profile.interfaceprofile
105             storage = host_profile.storageprofile
106             cpu = host_profile.cpuprofile
107             ram = host_profile.ramprofile
108
109             host = (name, description, in_labs, interfaces, storage, cpu, ram)
110             hosts.append(host)
111
112         context.update({'hosts': hosts})
113
114         return context