# Path: signup/db.py
# class UserSignup(models.Model):
# 
# Path: signup/emails.py
# def send_welcome_emails(emails):
# 
# 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: 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( ):


import random
import string
import json
from datetime import datetime
from signup import db
from signup import emails
from mailgun import api as mailgun_api
from sequence import models as sequence_model

def get_new_signups( ):
    """ get signups where the welcome email hasn't been sent yet """
    signups = db.UserSignup.objects.filter(date_tasks_handled__isnull=True, date_deleted__isnull=True)
    return [_signup2json(signup) for signup in signups]


def handle_new_signups( ):
    """ Send welcome email to new users.
        Add them to a general mailing list. 
        Update db when done. """
    signups = db.UserSignup.objects.filter(date_tasks_handled__isnull=True, date_deleted__isnull=True)[:500]
    while len(signups):
        #TODO emails.send_welcome_emails([signup.email for signup in signups])
        for signup in signups:
            add_user_to_global_list(signup.email, signup.sequence)
            #make sure new signups aren't in the mailgun blocked list
            mailgun_api.delete_all_unsubscribes(signup.email)

        db.UserSignup.objects.filter(id__in=signups.values('id')).update(date_tasks_handled=datetime.utcnow())
        signups = db.UserSignup.objects.filter(date_tasks_handled__isnull=True, date_deleted__isnull=True)[:500]


def add_user_to_global_list( email, sequence ):
    """ add user to email list that gets all emails """
    signup_db = db.UserSignup.objects.get(
        email__iexact=email, date_deleted__isnull=True, sequence=sequence
    )
    if signup_db.sequence:
        list_name = sequence_model.sequence_list_name(signup_db.sequence)

# Path: signup/db.py
# class UserSignup(models.Model):
# 
# Path: signup/emails.py
# def send_welcome_emails(emails):
# 
# 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: 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: signup/models.py
import random
import string
import json
from datetime import datetime
from signup import db
from signup import emails
from mailgun import api as mailgun_api
from sequence import models as sequence_model


def get_new_signups( ):
    """ get signups where the welcome email hasn't been sent yet """
    signups = db.UserSignup.objects.filter(date_tasks_handled__isnull=True, date_deleted__isnull=True)
    return [_signup2json(signup) for signup in signups]


def handle_new_signups( ):
    """ Send welcome email to new users.
        Add them to a general mailing list. 
        Update db when done. """
    signups = db.UserSignup.objects.filter(date_tasks_handled__isnull=True, date_deleted__isnull=True)[:500]
    while len(signups):
        #TODO emails.send_welcome_emails([signup.email for signup in signups])
        for signup in signups:
            add_user_to_global_list(signup.email, signup.sequence)
            #make sure new signups aren't in the mailgun blocked list
            mailgun_api.delete_all_unsubscribes(signup.email)

        db.UserSignup.objects.filter(id__in=signups.values('id')).update(date_tasks_handled=datetime.utcnow())
        signups = db.UserSignup.objects.filter(date_tasks_handled__isnull=True, date_deleted__isnull=True)[:500]


def add_user_to_global_list( email, sequence ):
    """ add user to email list that gets all emails """
    signup_db = db.UserSignup.objects.get(
        email__iexact=email, date_deleted__isnull=True, sequence=sequence
    )
    if signup_db.sequence:
        list_name = sequence_model.sequence_list_name(signup_db.sequence)

        mailgun_api.add_list_member(list_name, email)