# 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: 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: groups/models.py
# def group_uri2id( group_uri ):
# def group_id2uri( group_id ):
# def create_group( address, description, sequence ):
# def _group2json(group_db):
# def get_group( group_uri ):
# def get_groups( sequence=None ):
# def get_member_groups( member_email ):
# def add_group_member( group_uri, member_email ):
# def remove_group_member( group_uri, member_email ):
# def sync_group_with_mailgun( group_uri ):
# 
# Path: classphoto/group_photo.py
# def combine_photos(bios, width=6, avatar_size=(120,120)):
# def make_group_photo(group_uri):
# def upload_to_s3(filename, bucket):
# def make_group_photos(sequence):


from django.conf import settings
from django.template.loader import render_to_string
from signup import models as signup_api
from sequence import models as sequence_api
from groups import models as group_api
from classphoto import group_photo
import json
import mailgun
def send_group_photos(sequence):

    context = {
        'mooc_title': settings.MOOC_TITLE,
        'mooc_domain': settings.MOOC_DOMAIN,
        'sequence': sequence,
    }

    urls = group_photo.make_group_photos(sequence)

    for group_uri, image_url in urls.items():
        group = group_api.get_group(group_uri)
        context['image_url'] = image_url
        subject = render_to_string(
            'classphoto/emails/sequence_group_photo-subject.txt', context
        ).strip()
        text_body = render_to_string(
            'classphoto/emails/sequence_group_photo.txt', context
        ).strip()
        html_body = render_to_string(
            'classphoto/emails/sequence_group_photo.html', context
        ).strip()

        mailgun.api.send_email(
            group['address'],
            settings.DEFAULT_FROM_EMAIL,
            subject,
            text_body,
            html_body,
            tags=['group_photo'],

# 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: 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: groups/models.py
# def group_uri2id( group_uri ):
# def group_id2uri( group_id ):
# def create_group( address, description, sequence ):
# def _group2json(group_db):
# def get_group( group_uri ):
# def get_groups( sequence=None ):
# def get_member_groups( member_email ):
# def add_group_member( group_uri, member_email ):
# def remove_group_member( group_uri, member_email ):
# def sync_group_with_mailgun( group_uri ):
# 
# Path: classphoto/group_photo.py
# def combine_photos(bios, width=6, avatar_size=(120,120)):
# def make_group_photo(group_uri):
# def upload_to_s3(filename, bucket):
# def make_group_photos(sequence):


# Path: classphoto/emails.py
from django.conf import settings
from django.template.loader import render_to_string
from signup import models as signup_api
from sequence import models as sequence_api
from groups import models as group_api
from classphoto import group_photo
import json
import mailgun

def send_group_photos(sequence):

    context = {
        'mooc_title': settings.MOOC_TITLE,
        'mooc_domain': settings.MOOC_DOMAIN,
        'sequence': sequence,
    }

    urls = group_photo.make_group_photos(sequence)

    for group_uri, image_url in urls.items():
        group = group_api.get_group(group_uri)
        context['image_url'] = image_url
        subject = render_to_string(
            'classphoto/emails/sequence_group_photo-subject.txt', context
        ).strip()
        text_body = render_to_string(
            'classphoto/emails/sequence_group_photo.txt', context
        ).strip()
        html_body = render_to_string(
            'classphoto/emails/sequence_group_photo.html', context
        ).strip()

        mailgun.api.send_email(
            group['address'],
            settings.DEFAULT_FROM_EMAIL,
            subject,
            text_body,
            html_body,
            tags=['group_photo'],

            campaign_id=sequence_api.sequence_campaign(sequence)