# Path: mail/models.py
# def id2uri(id):
# def uri2id(uri):
# def save_email(subject, text_body, html_body, sequence, audience, tags):
# def update_email(uri, subject, text_body, html_body, sequence, audience, tags):
# def mark_sent(uri):
# def delete_email(uri):
# def _2json(email_db):
# def get_email(uri):
# def get_emails():
# def schedule_email(uri, scheduled_datetime):
# 
# Path: mailgun/api.py
# def call_mailgun(method, api_sub_url, data, params=None):
# def send_email(to_email, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None):
# def send_mass_email(to_emails, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None, recipient_variables=None):
# def create_campaign(campaign_id, campaign_name):
# def create_list(address, name=None, description=None, access_level=None):
# def update_list(address, name=None, description=None, access_level=None):
# def add_list_member(list_address, member_address):
# def remove_list_member(list_address, member_address):
# def get_list(list_address):
# def get_list_members(list_address):
# def get_unsubscribes(address):
# def delete_all_unsubscribes(address):
# def get_list_stats(list_address):
# def get_logs(limit=100, skip=0):
# def fetch_all_logs():
# def get_events(limit, **kwargs):
# def get_all_events(**kwargs):
# def get_campaign_events(campaign_id, event, recipient=None, limit=None, page=None, count=None):
# def _get_campaign_action(campaign_id, action, group_by, limit=None, page=None, count=None):
# def get_campaign_opens(campaign_id, group_by, limit=None, page=None, count=None):
# def get_campaign_clicks(campaign_id, group_by, limit=None, page=None, count=None):
# 
# Path: mail/email.py
# def send_email( email_uri ):
#     """ Send the email to the intended target audience """
#     email = mail_api.get_email(email_uri)
# 
#     if email['audience'] == 'groups':
#         to_addresses = [g['address'] for g in group_api.get_groups(email['sequence'])]
#     elif email['audience'] == 'individuals':
#         to_addresses = [sequence_api.sequence_list_name(email['sequence'])]
# 
#     context = {'email': email, 'mooc_domain': settings.MOOC_DOMAIN}
# 
#     text_body = render_to_string('mail/email.txt', context)
#     html_body = render_to_string('mail/email.html', context)
# 
#     batch_size = 1000
# 
#     for i in range(0, len(to_addresses), batch_size):
#         mailgun_api.send_mass_email(
#             to_addresses[i:i+batch_size],
#             settings.DEFAULT_FROM_EMAIL,
#             email['subject'],
#             text_body,
#             html_body,
#             email['tags'].split(','),
#             sequence_api.sequence_campaign(email['sequence'])
#         )
#     mail_api.mark_sent(email_uri)
# 
# Path: sequence/models.py
# def sequence_list_name( sequence_number ):
# def sequence_campaign( sequence_number ):
# def sequence2dict( sequence_db ):
# def create_sequence( start_date, signup_close_date ):
# def get_all_sequences( ):
# def get_current_sequence( ):
# def get_current_sequence_number( ):


from django import http
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.conf import settings
from django.contrib.auth.decorators import login_required
from mail import models as mail_api
from mailgun import api as mailgun_api
from mail.email import send_email
from sequence import models as sequence_model
import bleach
import datetime
import re
import requests
        'mail/compose.html',
        context,
        context_instance=RequestContext(request)
    )


@login_required
def send_preview( request ):
    """ ajax view to send preview email """
    if request.method == 'POST':
        subject = request.POST.get('subject')
        html_body = _rewrite_links(request.POST.get('body_text'))
        text_body = _text_from_html(html_body)
        to_email = request.POST.get('test_email')
        mailgun_api.send_email(to_email, settings.DEFAULT_FROM_EMAIL, subject, text_body, html_body)
        return http.HttpResponse('')
    raise Exception()


@login_required
def send( request, id ):
    #TODO should require a POST
    email_uri = mail_api.id2uri(id)
    send_email(email_uri)
    return http.HttpResponseRedirect(reverse('mail_schedule'))


@login_required
def delete( request, id ):
    #TODO should require a POST

# Path: mail/models.py
# def id2uri(id):
# def uri2id(uri):
# def save_email(subject, text_body, html_body, sequence, audience, tags):
# def update_email(uri, subject, text_body, html_body, sequence, audience, tags):
# def mark_sent(uri):
# def delete_email(uri):
# def _2json(email_db):
# def get_email(uri):
# def get_emails():
# def schedule_email(uri, scheduled_datetime):
# 
# Path: mailgun/api.py
# def call_mailgun(method, api_sub_url, data, params=None):
# def send_email(to_email, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None):
# def send_mass_email(to_emails, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None, recipient_variables=None):
# def create_campaign(campaign_id, campaign_name):
# def create_list(address, name=None, description=None, access_level=None):
# def update_list(address, name=None, description=None, access_level=None):
# def add_list_member(list_address, member_address):
# def remove_list_member(list_address, member_address):
# def get_list(list_address):
# def get_list_members(list_address):
# def get_unsubscribes(address):
# def delete_all_unsubscribes(address):
# def get_list_stats(list_address):
# def get_logs(limit=100, skip=0):
# def fetch_all_logs():
# def get_events(limit, **kwargs):
# def get_all_events(**kwargs):
# def get_campaign_events(campaign_id, event, recipient=None, limit=None, page=None, count=None):
# def _get_campaign_action(campaign_id, action, group_by, limit=None, page=None, count=None):
# def get_campaign_opens(campaign_id, group_by, limit=None, page=None, count=None):
# def get_campaign_clicks(campaign_id, group_by, limit=None, page=None, count=None):
# 
# Path: mail/email.py
# def send_email( email_uri ):
#     """ Send the email to the intended target audience """
#     email = mail_api.get_email(email_uri)
# 
#     if email['audience'] == 'groups':
#         to_addresses = [g['address'] for g in group_api.get_groups(email['sequence'])]
#     elif email['audience'] == 'individuals':
#         to_addresses = [sequence_api.sequence_list_name(email['sequence'])]
# 
#     context = {'email': email, 'mooc_domain': settings.MOOC_DOMAIN}
# 
#     text_body = render_to_string('mail/email.txt', context)
#     html_body = render_to_string('mail/email.html', context)
# 
#     batch_size = 1000
# 
#     for i in range(0, len(to_addresses), batch_size):
#         mailgun_api.send_mass_email(
#             to_addresses[i:i+batch_size],
#             settings.DEFAULT_FROM_EMAIL,
#             email['subject'],
#             text_body,
#             html_body,
#             email['tags'].split(','),
#             sequence_api.sequence_campaign(email['sequence'])
#         )
#     mail_api.mark_sent(email_uri)
# 
# Path: sequence/models.py
# def sequence_list_name( sequence_number ):
# def sequence_campaign( sequence_number ):
# def sequence2dict( sequence_db ):
# def create_sequence( start_date, signup_close_date ):
# def get_all_sequences( ):
# def get_current_sequence( ):
# def get_current_sequence_number( ):


# Path: mail/views.py
from django import http
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.conf import settings
from django.contrib.auth.decorators import login_required
from mail import models as mail_api
from mailgun import api as mailgun_api
from mail.email import send_email
from sequence import models as sequence_model
import bleach
import datetime
import re
import requests

        'mail/compose.html',
        context,
        context_instance=RequestContext(request)
    )


@login_required
def send_preview( request ):
    """ ajax view to send preview email """
    if request.method == 'POST':
        subject = request.POST.get('subject')
        html_body = _rewrite_links(request.POST.get('body_text'))
        text_body = _text_from_html(html_body)
        to_email = request.POST.get('test_email')
        mailgun_api.send_email(to_email, settings.DEFAULT_FROM_EMAIL, subject, text_body, html_body)
        return http.HttpResponse('')
    raise Exception()


@login_required
def send( request, id ):
    #TODO should require a POST
    email_uri = mail_api.id2uri(id)
    send_email(email_uri)
    return http.HttpResponseRedirect(reverse('mail_schedule'))


@login_required
def delete( request, id ):
    #TODO should require a POST

    email_uri = mail_api.id2uri(id)