from django.shortcuts import render, redirect, get_object_or_404 from .forms import PollForm from .models import Response from .choices import * # Create your views here. def index(request): form = PollForm() if request.method=='POST': form = PollForm(request.POST) print(request.POST) print("hi!") if form.is_valid(): data = form.cleaned_data decision_score = round(sum([DECISION_WEIGHT[i] for i in range(len(DECISION_QUESTIONS)) if data[f'd{i}'] == 'n/a' or data[f'd{i}'] == 'complete'])/sum(DECISION_WEIGHT)*100) it_score = round(sum([DECISION_IT_WEIGHT[i] for i in range(len(DECISION_IT_QUESTIONS)) if data[f'dit{i}'] == 'n/a' or data[f'dit{i}'] == 'complete'])/sum(DECISION_IT_WEIGHT)*100) legal_score = round(sum([DECISION_LEGAL_WEIGHT[i] for i in range(len(DECISION_LEGAL_QUESTIONS)) if data[f'dl{i}'] == 'n/a' or data[f'dl{i}'] == 'complete'])/sum(DECISION_LEGAL_WEIGHT)*100) resources_score = round(sum([RESOURCES_WEIGHT[i] for i in range(len(RESOURCES_QUESTIONS)) if data[f'r{i}'] == 'n/a' or data[f'r{i}'] == 'complete'])/sum(RESOURCES_WEIGHT)*100) impact_score = round(sum([IMPACT_WEIGHT[i] for i in range(len(IMPACT_QUESTIONS)) if data[f'i{i}'] == 'n/a' or data[f'i{i}'] == 'complete'])/sum(IMPACT_WEIGHT)*100) velocity_score = round(sum([VELOCITY_WEIGHT[i] for i in range(len(VELOCITY_QUESTIONS)) if data[f'v{i}'] == 'n/a' or data[f'v{i}'] == 'complete'])/sum(VELOCITY_WEIGHT)*100) expectations_score = round(sum([EXPECTATIONS_WEIGHT[i] for i in range(len(EXPECTATIONS_QUESTIONS)) if data[f'e{i}'] == 'n/a' or data[f'e{i}'] == 'complete'])/sum(EXPECTATIONS_WEIGHT)*100) response = Response( first_name=data['first_name'], last_name=data['last_name'], email=data['email'], expectations=expectations_score, velocity=velocity_score, impact=impact_score, resources=resources_score, decision=decision_score, decision_it=it_score, decision_legal=legal_score ) response.save() request.session['_response'] = response.id return redirect('results') context = { 'form': form } return render(request, 'drive/index.html', context=context) def results (request): responseID = request.session.get('_response') print(responseID) if responseID: return redirect('/') response = get_object_or_404(Response, id=responseID) print(response) context = { 'response': response } return render(request, 'drive/results.html', context=context)