# 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 create_signup( email, questions ):
    """ Add signup to the current sequence """
    sequence = sequence_model.get_current_sequence_number()
    if db.UserSignup.objects.filter(email__iexact=email, sequence=sequence).exists():
        raise Exception('Signup already exists')
    invite_code=''.join([
        random.choice(string.letters+string.digits) for i in range(32)
    ])
    now = datetime.utcnow()
    signup = db.UserSignup(
        email=email,
        invite_code=invite_code,
        questions=json.dumps(questions),
        sequence=sequence,
        date_added=now,
        date_updated=now
    )
    signup.save()
    return _signup2json(signup)


def update_signup( email, questions ):
    """ Update the signup if it exists for the current sequence. If the signup was previously delete it will be undeleted """

# 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 create_signup( email, questions ):
    """ Add signup to the current sequence """
    sequence = sequence_model.get_current_sequence_number()
    if db.UserSignup.objects.filter(email__iexact=email, sequence=sequence).exists():
        raise Exception('Signup already exists')
    invite_code=''.join([
        random.choice(string.letters+string.digits) for i in range(32)
    ])
    now = datetime.utcnow()
    signup = db.UserSignup(
        email=email,
        invite_code=invite_code,
        questions=json.dumps(questions),
        sequence=sequence,
        date_added=now,
        date_updated=now
    )
    signup.save()
    return _signup2json(signup)


def update_signup( email, questions ):
    """ Update the signup if it exists for the current sequence. If the signup was previously delete it will be undeleted """

    sequence = sequence_model.get_current_sequence_number()