# Path: mailgun/utils.py
# def verify_webhook(token, timestamp, signature):
# def parse_timestamp(timestamp_text):
# 
# Path: signup/models.py
# def create_signup( email, questions ):
# def update_signup( email, questions ):
# def create_or_update_signup( email, questions ):
# def delete_signup( email, sequence ):
# def _signup2json( signup_db ):
# def get_signup( email, sequence ):
# def get_all_user_signups( email ):
# def get_signup_by_invite_code( invite_code ):
# def get_signups( sequence ):
# def get_signups_for_archiving( sequence ):
# def get_new_signups( ):
# def handle_new_signups( ):
# def add_user_to_global_list( email, sequence ):
# 
# Path: unsubscribe/models.py
# def unsubscribe_user( address ):
# 
# Path: unsubscribe/emails.py
# def send_unsubscribe_confirmation(signup):
#     context = {
#         'email': signup['email'],
#         'key': signup['key'],
#         'mooc_title': settings.MOOC_TITLE,
#         'mooc_domain': settings.MOOC_DOMAIN
#     }
#     subject = render_to_string('unsubscribe/emails/confirm-subject.txt', context).strip()
#     text_body = render_to_string('unsubscribe/emails/confirm.txt', context).strip()
#     html_body = render_to_string('unsubscribe/emails/confirm.html', context).strip()
# 
#     mailgun.api.send_email(signup['email'], settings.DEFAULT_FROM_EMAIL,
#         subject, text_body, html_body, tags=['user_link']
#     )


from django import http
from django.views.decorators.csrf import csrf_exempt
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib import messages
from django.core.urlresolvers import reverse
from mailgun import utils
from signup import models as signup_model
from unsubscribe import models as unsubscribe_model
from unsubscribe.emails import send_unsubscribe_confirmation
import logging


log = logging.getLogger(__name__)


def unsubscribe(request):
    context = {}
    if request.method == 'POST':
        email = request.POST.get('email')
        try:
            # TODO
            signups = signup_model.get_all_user_signups(email)
            if len(signups) == 0:
                raise Exception()
            send_unsubscribe_confirmation(signups[0])
            messages.info(request, u'You will shortly receive an email to confirm that you wish to unsubscribe')
            return http.HttpResponseRedirect(reverse('home'))
        except:
            context['error'] = 'Could not find signup.'

    return render_to_response('unsubscribe/unsubscribe.html', context, context_instance=RequestContext(request))


def confirm(request, key):
    try:

# Path: mailgun/utils.py
# def verify_webhook(token, timestamp, signature):
# def parse_timestamp(timestamp_text):
# 
# Path: signup/models.py
# def create_signup( email, questions ):
# def update_signup( email, questions ):
# def create_or_update_signup( email, questions ):
# def delete_signup( email, sequence ):
# def _signup2json( signup_db ):
# def get_signup( email, sequence ):
# def get_all_user_signups( email ):
# def get_signup_by_invite_code( invite_code ):
# def get_signups( sequence ):
# def get_signups_for_archiving( sequence ):
# def get_new_signups( ):
# def handle_new_signups( ):
# def add_user_to_global_list( email, sequence ):
# 
# Path: unsubscribe/models.py
# def unsubscribe_user( address ):
# 
# Path: unsubscribe/emails.py
# def send_unsubscribe_confirmation(signup):
#     context = {
#         'email': signup['email'],
#         'key': signup['key'],
#         'mooc_title': settings.MOOC_TITLE,
#         'mooc_domain': settings.MOOC_DOMAIN
#     }
#     subject = render_to_string('unsubscribe/emails/confirm-subject.txt', context).strip()
#     text_body = render_to_string('unsubscribe/emails/confirm.txt', context).strip()
#     html_body = render_to_string('unsubscribe/emails/confirm.html', context).strip()
# 
#     mailgun.api.send_email(signup['email'], settings.DEFAULT_FROM_EMAIL,
#         subject, text_body, html_body, tags=['user_link']
#     )


# Path: unsubscribe/views.py
from django import http
from django.views.decorators.csrf import csrf_exempt
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib import messages
from django.core.urlresolvers import reverse
from mailgun import utils
from signup import models as signup_model
from unsubscribe import models as unsubscribe_model
from unsubscribe.emails import send_unsubscribe_confirmation
import logging



log = logging.getLogger(__name__)


def unsubscribe(request):
    context = {}
    if request.method == 'POST':
        email = request.POST.get('email')
        try:
            # TODO
            signups = signup_model.get_all_user_signups(email)
            if len(signups) == 0:
                raise Exception()
            send_unsubscribe_confirmation(signups[0])
            messages.info(request, u'You will shortly receive an email to confirm that you wish to unsubscribe')
            return http.HttpResponseRedirect(reverse('home'))
        except:
            context['error'] = 'Could not find signup.'

    return render_to_response('unsubscribe/unsubscribe.html', context, context_instance=RequestContext(request))


def confirm(request, key):
    try:

        su = signup_model.get_signup_by_invite_code(key)