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


from mailgun import api as mailgun_api
from signup import models as signup_model
from sequence import models as sequence_model
from groups import models as groups_model
import logging

log = logging.getLogger(__name__)


def unsubscribe_user( address ):
    """ Unsubscribe user completely from the Mechanical Mooc - all sequences
    """

    # remove from sequence group
    signups = signup_model.get_all_user_signups(address)
    for user_signup in signups:
        sequence_list = sequence_model.sequence_list_name(user_signup['sequence'])
        mailgun_api.remove_list_member(sequence_list, address)

    # remove from small groups
    groups = groups_model.get_member_groups(address)
    for group in groups:
        groups_model.remove_group_member(group['uri'], address)
        groups_model.sync_group_with_mailgun(group['uri'])

    # mark as unsubscribed in the signups
    for user_signup in signups:

# 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: 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: unsubscribe/models.py
from mailgun import api as mailgun_api
from signup import models as signup_model
from sequence import models as sequence_model
from groups import models as groups_model
import logging


log = logging.getLogger(__name__)


def unsubscribe_user( address ):
    """ Unsubscribe user completely from the Mechanical Mooc - all sequences
    """

    # remove from sequence group
    signups = signup_model.get_all_user_signups(address)
    for user_signup in signups:
        sequence_list = sequence_model.sequence_list_name(user_signup['sequence'])
        mailgun_api.remove_list_member(sequence_list, address)

    # remove from small groups
    groups = groups_model.get_member_groups(address)
    for group in groups:
        groups_model.remove_group_member(group['uri'], address)
        groups_model.sync_group_with_mailgun(group['uri'])

    # mark as unsubscribed in the signups
    for user_signup in signups:

        signup_model.delete_signup(address, user_signup['sequence'])