from django.conf import settings
from django.template.loader import get_template
from django.template import RequestContext, Context
from django.shortcuts import render_to_response, render
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from .mdot_rest_client.client import MDOT
from .forms import ReviewForm

import urllib
import json
from forms import ReviewForm
from models import SponsorForm, ManagerForm, AppForm, Agreement


def home(request):
    params = {"resources": MDOT().get_resources(featured=True)}
    return render(request, "mdot/home.html", params)


def developers(request):
    return render(request, "mdot/developers/home.html")


def guidelines(request):
    params = {"ux_contact": getattr(settings, "MDOT_UX_CONTACT", None)}
    return render(request, "mdot/developers/guidelines.html", params)


def process(request):
    return render(request, "mdot/developers/process.html")


def review(request):
    if request.method == 'POST':
        sponsorForm = SponsorForm(request.POST, prefix='sponsor')
        managerForm = ManagerForm(request.POST, prefix='manager')
        appForm = AppForm(request.POST, prefix='app')
        if (sponsorForm.is_valid() and managerForm.is_valid()
                and appForm.is_valid()):
            sponsor = sponsorForm.save()
            manager = managerForm.save()
            app = appForm.save(commit=False)
            app.app_sponsor = sponsor
            app.app_manager = manager
            app.save()
            # create and save agreement model
            agreement = Agreement(
                sponsor=sponsor,
                app=app,
            )
            agreement.save()

            params = {
                'service_email': getattr(settings, 'MDOT_SERVICE_EMAIL'),
                'ux_contact': getattr(settings, 'MDOT_UX_CONTACT'),
            }
            return render_to_response(
                'mdot/developers/thanks.html',
                params)

    else:
        # create forms with appropriate prefixes to
        # avoid duplicate field names
        sponsorForm = SponsorForm(prefix='sponsor')
        managerForm = ManagerForm(prefix='manager')
        appForm = AppForm(prefix='app')

        forms = {
            'sponsorform': sponsorForm,
            'managerform': managerForm,
            'appform': appForm,
        }

    # return forms to review page
    return render(request, 'mdot/developers/review.html', forms)


# -----old form commented out-----

# def review(request):
#     # if this is a POST request we need to process the form data
#     if request.method == 'POST':
#         # create a form instance and populate it with data from the request:
#         form = ReviewForm(request.POST)
#         # check whether it's valid:
#         if form.is_valid():
#             campus_audience = form.cleaned_data['campus_audience']
#             campus_need = form.cleaned_data['campus_need']
#             sponsor_name = form.cleaned_data['sponsor_name']
#             sponsor_netid = form.cleaned_data['sponsor_netid']
#             sponsor_email = form.cleaned_data['sponsor_email']
#             dev_name = form.cleaned_data['dev_name']
#             dev_email = form.cleaned_data['dev_email']
#             support_name = form.cleaned_data['support_name']
#             support_email = form.cleaned_data['support_email']
#             support_contact = form.cleaned_data['support_contact']
#             ats_review = form.cleaned_data['ats_review']
#             ux_review = form.cleaned_data['ux_review']
#             brand_review = form.cleaned_data['brand_review']
#             app_documentation = form.cleaned_data['app_documentation']
#             app_code = form.cleaned_data['app_code']
#             anything_else = form.cleaned_data['anything_else']

#             email_context = {
#                 'campus_audience': campus_audience,
#                 'campus_need': campus_need,
#                 'sponsor_name': sponsor_name,
#                 'sponsor_netid': sponsor_netid,
#                 'sponsor_email': sponsor_email,
#                 'dev_name': dev_name,
#                 'dev_email': dev_email,
#                 'support_name': support_name,
#                 'support_email': support_email,
#                 'support_contact': support_contact,
#                 'ats_review': ats_review,
#                 'ux_review': ux_review,
#                 'brand_review': brand_review,
#                 'app_documentation': app_documentation,
#                 'app_code': app_code,
#                 'anything_else': anything_else
#             }
#             try:
#                 send_mail(
#                     sponsor_name,
#                     get_template(
#                         'mdot/developers/email_plain.html')
#                     .render(email_context),
#                     sponsor_email, [getattr(settings, "MDOT_SERVICE_EMAIL")],
#                     html_message=get_template(
#                         'mdot/developers/email_html.html')
#                     .render(email_context),
#                 ),
#             except BadHeaderError:
#                 return HttpResponse('Invalid header found.')
#             params = {
#                 'service_email': getattr(settings, 'MDOT_SERVICE_EMAIL'),
#                 'ux_contact': getattr(settings, 'MDOT_UX_CONTACT'),
#             }
#             return render_to_response(
#                 'mdot/developers/thanks.html',
#                 params)
#     # if a GET (or any other method) we'll create a blank form
#     else:
#         form = ReviewForm()
#     return render(request, 'mdot/developers/review.html', {'form': form})

@@ -2029,36 +2029,10 @@
   # 
-create forms with appropriat
+us
 e pr
@@ -2040,26 +2040,16 @@
 fixes to
-%0A        #
  avoid d
@@ -2389,16 +2389,16 @@
 ew page%0A
+
     retu
@@ -2458,3243 +2458,4 @@
 ms)%0A
-%0A%0A# -----old form commented out-----%0A%0A# def review(request):%0A#     # if this is a POST request we need to process the form data%0A#     if request.method == 'POST':%0A#         # create a form instance and populate it with data from the request:%0A#         form = ReviewForm(request.POST)%0A#         # check whether it's valid:%0A#         if form.is_valid():%0A#             campus_audience = form.cleaned_data%5B'campus_audience'%5D%0A#             campus_need = form.cleaned_data%5B'campus_need'%5D%0A#             sponsor_name = form.cleaned_data%5B'sponsor_name'%5D%0A#             sponsor_netid = form.cleaned_data%5B'sponsor_netid'%5D%0A#             sponsor_email = form.cleaned_data%5B'sponsor_email'%5D%0A#             dev_name = form.cleaned_data%5B'dev_name'%5D%0A#             dev_email = form.cleaned_data%5B'dev_email'%5D%0A#             support_name = form.cleaned_data%5B'support_name'%5D%0A#             support_email = form.cleaned_data%5B'support_email'%5D%0A#             support_contact = form.cleaned_data%5B'support_contact'%5D%0A#             ats_review = form.cleaned_data%5B'ats_review'%5D%0A#             ux_review = form.cleaned_data%5B'ux_review'%5D%0A#             brand_review = form.cleaned_data%5B'brand_review'%5D%0A#             app_documentation = form.cleaned_data%5B'app_documentation'%5D%0A#             app_code = form.cleaned_data%5B'app_code'%5D%0A#             anything_else = form.cleaned_data%5B'anything_else'%5D%0A%0A#             email_context = %7B%0A#                 'campus_audience': campus_audience,%0A#                 'campus_need': campus_need,%0A#                 'sponsor_name': sponsor_name,%0A#                 'sponsor_netid': sponsor_netid,%0A#                 'sponsor_email': sponsor_email,%0A#                 'dev_name': dev_name,%0A#                 'dev_email': dev_email,%0A#                 'support_name': support_name,%0A#                 'support_email': support_email,%0A#                 'support_contact': support_contact,%0A#                 'ats_review': ats_review,%0A#                 'ux_review': ux_review,%0A#                 'brand_review': brand_review,%0A#                 'app_documentation': app_documentation,%0A#                 'app_code': app_code,%0A#                 'anything_else': anything_else%0A#             %7D%0A#             try:%0A#                 send_mail(%0A#                     sponsor_name,%0A#                     get_template(%0A#                         'mdot/developers/email_plain.html')%0A#                     .render(email_context),%0A#                     sponsor_email, %5Bgetattr(settings, %22MDOT_SERVICE_EMAIL%22)%5D,%0A#                     html_message=get_template(%0A#                         'mdot/developers/email_html.html')%0A#                     .render(email_context),%0A#                 ),%0A#             except BadHeaderError:%0A#                 return HttpResponse('Invalid header found.')%0A#             params = %7B%0A#                 'service_email': getattr(settings, 'MDOT_SERVICE_EMAIL'),%0A#                 'ux_contact': getattr(settings, 'MDOT_UX_CONTACT'),%0A#             %7D%0A#             return render_to_response(%0A#                 'mdot/developers/thanks.html',%0A#                 params)%0A#     # if a GET (or any other method) we'll create a blank form%0A#     else:%0A#         form = ReviewForm()%0A#     return render(request, 'mdot/developers/review.html', %7B'form': form%7D)%0A

delete old implementation code