Lab as a Service 2.0
[laas.git] / src / workflow / views.py
1 ##############################################################################
2 # Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 from django.http import HttpResponse, HttpRequest, HttpResponseGone
12 from django.urls import reverse
13 from django.shortcuts import render, redirect
14 from django import forms
15
16 import uuid
17
18 from workflow.forms import *
19 from workflow.workflow_manager import *
20
21 import logging
22 logger = logging.getLogger(__name__)
23
24
25 def attempt_auth(request):
26     try:
27         manager = ManagerTracker.managers[request.session['manager_session']]
28
29         return manager
30
31     except KeyError:
32         return None
33
34 def delete_session(request):
35     try:
36         manager = ManagerTracker.managers[request.session['manager_session']]
37         del ManagerTracker.managers[request.session['manager_session']]
38         return HttpResponse('')
39     except KeyError:
40         return None
41
42 def step_view(request):
43     manager = attempt_auth(request)
44     if not manager:
45         #no manager found, redirect to "lost" page
46         return no_workflow(request)
47     if request.GET.get('step') is not None:
48         manager.goto(int(request.GET.get('step')))
49     return manager.render(request)
50
51 def manager_view(request):
52     manager = attempt_auth(request)
53
54     if not manager:
55         return HttpResponseGone("No session found that relates to current request")
56
57     if request.method == 'GET': #no need for this statement if only intercepting post requests
58
59         #return general context for viewport page
60         return manager.status(request)
61
62     if request.method == 'POST':
63         if request.POST.get('add') is not None:
64             logger.debug("add found")
65             target_id = None
66             if 'target' in request.POST:
67                 target_id=int(request.POST.get('target'))
68             manager.add_workflow(workflow_type=int(request.POST.get('add')), target_id=target_id)
69         elif request.POST.get('edit') is not None and request.POST.get('edit_id') is not None:
70             logger.debug("edit found")
71             manager.add_workflow(workflow_type=request.POST.get('edit'), edit_object=int(request.POST.get('edit_id')))
72         elif request.POST.get('cancel') is not None:
73             mgr = ManagerTracker.managers[request.session['manager_session']]
74             del ManagerTracker.managers[request.session['manager_session']]
75             del mgr
76
77     return manager.status(request)
78
79 def viewport_view(request):
80     if not request.user.is_authenticated:
81         return login(request)
82
83     manager = attempt_auth(request)
84     if manager is None:
85         return no_workflow(request)
86
87     if  request.method == 'GET':
88         return render(request, 'workflow/viewport-base.html')
89     else:
90         pass
91
92 def create_session(wf_type, request):
93     wf = int(wf_type)
94     smgr = SessionManager(request=request)
95     smgr.add_workflow(workflow_type=wf, target_id=request.POST.get("target"))
96     manager_uuid = uuid.uuid4().hex
97     ManagerTracker.getInstance().managers[manager_uuid] = smgr
98
99     return manager_uuid
100
101 def no_workflow(request):
102
103     logger.debug("There is no active workflow")
104
105     return render(request, 'workflow/no_workflow.html', {'title': "Not Found"})
106
107 def login(request):
108     return render(request, "dashboard/login.html", {'title': 'Authentication Required'})