# 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: 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)


from django.core.management.base import BaseCommand, CommandError
from mail import models as mail_api
from mail.email import send_email
from datetime import datetime



class Command(BaseCommand):
    #args = '<data url>'
    help = 'Send all scheduled email with a date in the past'

    def handle(self, *args, **options):
        emails = mail_api.get_emails()

# 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: 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: mail/management/commands/send_scheduled_mail.py
from django.core.management.base import BaseCommand, CommandError
from mail import models as mail_api
from mail.email import send_email
from datetime import datetime




class Command(BaseCommand):
    #args = '<data url>'
    help = 'Send all scheduled email with a date in the past'

    def handle(self, *args, **options):
        emails = mail_api.get_emails()

        for email in emails: