LFID login for both projects
[laas.git] / 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.db.models import Q
16 from datetime import datetime
17 import pytz
18
19 from account.models import Lab
20 from booking.models import Booking
21
22 from resource_inventory.models import Image, ResourceProfile, ResourceQuery
23 from workflow.workflow_manager import ManagerTracker
24
25 import os
26
27
28 def lab_list_view(request):
29     labs = Lab.objects.all()
30     context = {"labs": labs, 'title': 'Labs'}
31
32     return render(request, "dashboard/lab_list.html", context)
33
34
35 def lab_detail_view(request, lab_name):
36     user = None
37     if request.user.is_authenticated:
38         user = request.user
39
40     lab = get_object_or_404(Lab, name=lab_name)
41
42     images = Image.objects.filter(from_lab=lab).filter(public=True)
43     if user:
44         images = images | Image.objects.filter(from_lab=lab).filter(owner=user)
45
46     hosts = ResourceQuery.filter(lab=lab)
47
48     return render(
49         request,
50         "dashboard/lab_detail.html",
51         {
52             'title': "Lab Overview",
53             'lab': lab,
54             'hostprofiles': ResourceProfile.objects.filter(labs=lab),
55             'images': images,
56             'hosts': hosts
57         }
58     )
59
60
61 def host_profile_detail_view(request):
62
63     return render(
64         request,
65         "dashboard/host_profile_detail.html",
66         {
67             'title': "Host Types",
68         }
69     )
70
71
72 def landing_view(request):
73     manager = ManagerTracker.managers.get(request.session.get('manager_session'))
74     user = request.user
75     if not user.is_anonymous:
76         bookings = Booking.objects.filter(
77             Q(owner=user) | Q(collaborators=user),
78             end__gte=datetime.now(pytz.utc)
79         )
80     else:
81         bookings = None
82
83     LFID = True if os.environ['AUTH_SETTING'] == 'LFID' else False
84     return render(
85         request,
86         'dashboard/landing.html',
87         {
88             'manager': manager is not None,
89             'title': "Welcome to the Lab as a Service Dashboard",
90             'bookings': bookings,
91             'LFID': LFID
92         }
93     )
94
95
96 class LandingView(TemplateView):
97     template_name = "dashboard/landing.html"
98
99     def get_context_data(self, **kwargs):
100         context = super(LandingView, self).get_context_data(**kwargs)
101
102         hosts = []
103
104         for host_profile in ResourceProfile.objects.all():
105             name = host_profile.name
106             description = host_profile.description
107             in_labs = host_profile.labs
108
109             interfaces = host_profile.interfaceprofile
110             storage = host_profile.storageprofile
111             cpu = host_profile.cpuprofile
112             ram = host_profile.ramprofile
113
114             host = (name, description, in_labs, interfaces, storage, cpu, ram)
115             hosts.append(host)
116
117         context.update({'hosts': hosts})
118
119         return context