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


from django.conf import settings
from groups import models as group_model
from signup import models as signup_model
from mailgun import api as mailgun_api
import random
import math
import datetime
import pytz

    for timezone, group in tz_grouping.items():
        tz_grouping[timezone] = shuffle(group)

    merge = lambda x,y: x + y
    tz_sorted_signups = reduce(merge, [ tz_grouping[timezone] for timezone in sorted(tz_grouping.keys())])

    groups = [ tz_sorted_signups[i:min(i+max_group_size, len(tz_sorted_signups))] for i in range(0,len(tz_sorted_signups), max_group_size)]

    return groups


def create_groups(groups, sequence, name_prefix="Group"):
    """ Create the groups in the backend """
    for i, group_data in enumerate(groups):
        group_address = "{0}-{1}@{2}".format(name_prefix.lower().replace(' ','-'), i+1, settings.EMAIL_DOMAIN)
        group_name = "{0} {1}: {2} to {3}".format(name_prefix, i+1, group_data[0]['tz_offset'], group_data[-1]['tz_offset'])
        print(group_name)

        group = group_model.create_group(group_address, group_name, sequence)

        for member in group_data:
            group_model.add_group_member(group['uri'], member['email'])


def do_grouping(sequence):
    groups = prepare_groups(sequence)
    create_groups(groups, sequence, 'Group {0}'.format(sequence))

    # handle singups not in group

# 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: 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: 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: grouping/models.py
from django.conf import settings
from groups import models as group_model
from signup import models as signup_model
from mailgun import api as mailgun_api
import random
import math
import datetime
import pytz


    for timezone, group in tz_grouping.items():
        tz_grouping[timezone] = shuffle(group)

    merge = lambda x,y: x + y
    tz_sorted_signups = reduce(merge, [ tz_grouping[timezone] for timezone in sorted(tz_grouping.keys())])

    groups = [ tz_sorted_signups[i:min(i+max_group_size, len(tz_sorted_signups))] for i in range(0,len(tz_sorted_signups), max_group_size)]

    return groups


def create_groups(groups, sequence, name_prefix="Group"):
    """ Create the groups in the backend """
    for i, group_data in enumerate(groups):
        group_address = "{0}-{1}@{2}".format(name_prefix.lower().replace(' ','-'), i+1, settings.EMAIL_DOMAIN)
        group_name = "{0} {1}: {2} to {3}".format(name_prefix, i+1, group_data[0]['tz_offset'], group_data[-1]['tz_offset'])
        print(group_name)

        group = group_model.create_group(group_address, group_name, sequence)

        for member in group_data:
            group_model.add_group_member(group['uri'], member['email'])


def do_grouping(sequence):
    groups = prepare_groups(sequence)
    create_groups(groups, sequence, 'Group {0}'.format(sequence))

    # handle singups not in group

    signups = signup_model.get_signups(sequence)