# 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: mailgun/utils.py
# def parse_timestamp(timestamp_text):
#      return datetime.datetime.strptime(timestamp_text, '%a, %d %b %Y %H:%M:%S GMT')
# 
# 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: 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: data/utils.py
# def write_to_csv(data, filename):
#     """ data should be a list of dicts. All dicts should have the same keys! """
#     if len(data) == 0:
#         return
#     with open(filename, 'w') as f:
#         writer = unicodecsv.writer(f)
#         writer.writerow(data[0].keys())
#         for elem in data:
#             writer.writerow(elem.values())
# 
# Path: data/emails.py
# def export_emails(prefix=''):
#     messages = get_emails()
#     timestamp = datetime.now().date().isoformat()
#     mb = mailbox.mbox('{0}_emails_{1}.mbox'.format(prefix, timestamp))
#     for msg in messages:
#         mb.add(msg)
#     mb.close()
#     csv_data = process_messages(messages)
#     write_to_csv(csv_data, '{0}_emails_{1}.csv'.format(prefix, timestamp))


from mailgun import api as mailgun_api
from mailgun.utils import parse_timestamp
from signup import models as signup_api
from groups import models as groups_api
from data.utils import write_to_csv
from data.emails import export_emails
from django.conf import settings
from datetime import datetime
import sqlite3
import unicodecsv
                row = [ user[key] for key in user_keys ]
                row += [ event[key] for key in event_keys ]
                writer.writerow(row)


def get_user_mail_activity(logs, email):
    logs = mailgun_api.get_logs(limit=0)
    user_emails = filter(event_filter, logs)


def dump_data(prefix=''):
    sequences = [(4, 'sequence_4_campaign'), (5, 'sequence_5_campaign')]
    #sequences = [(1, 'sequence_1_campaign')]
    user_keys = ['email', 'date_created', 'group_id', 'group_size']
    event_keys = ['event', 'timestamp', 'tags', 'link']
    for sequence, campaign_id in sequences:
        users = signup_api.get_signups_for_archiving(sequence)
        for user in users:
            user['group_size'] = 0
            user['group_id'] = None
            user_groups = groups_api.get_member_groups(user['email'])
            if len(user_groups) == 1:
                user['group_id'] = user_groups[0]['address']
                user['group_size'] = len(user_groups[0]['members'])
            user.update(user['questions'])
            del user['questions']

        # open file for writing data
        timestamp = datetime.now().date().isoformat()
        filename = '_'.join([prefix, 'sequence', str(sequence), 'users', timestamp]).strip('_') + '.csv'

# 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: mailgun/utils.py
# def parse_timestamp(timestamp_text):
#      return datetime.datetime.strptime(timestamp_text, '%a, %d %b %Y %H:%M:%S GMT')
# 
# 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: 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: data/utils.py
# def write_to_csv(data, filename):
#     """ data should be a list of dicts. All dicts should have the same keys! """
#     if len(data) == 0:
#         return
#     with open(filename, 'w') as f:
#         writer = unicodecsv.writer(f)
#         writer.writerow(data[0].keys())
#         for elem in data:
#             writer.writerow(elem.values())
# 
# Path: data/emails.py
# def export_emails(prefix=''):
#     messages = get_emails()
#     timestamp = datetime.now().date().isoformat()
#     mb = mailbox.mbox('{0}_emails_{1}.mbox'.format(prefix, timestamp))
#     for msg in messages:
#         mb.add(msg)
#     mb.close()
#     csv_data = process_messages(messages)
#     write_to_csv(csv_data, '{0}_emails_{1}.csv'.format(prefix, timestamp))


# Path: data/models.py
from mailgun import api as mailgun_api
from mailgun.utils import parse_timestamp
from signup import models as signup_api
from groups import models as groups_api
from data.utils import write_to_csv
from data.emails import export_emails
from django.conf import settings
from datetime import datetime
import sqlite3
import unicodecsv

                row = [ user[key] for key in user_keys ]
                row += [ event[key] for key in event_keys ]
                writer.writerow(row)


def get_user_mail_activity(logs, email):
    logs = mailgun_api.get_logs(limit=0)
    user_emails = filter(event_filter, logs)


def dump_data(prefix=''):
    sequences = [(4, 'sequence_4_campaign'), (5, 'sequence_5_campaign')]
    #sequences = [(1, 'sequence_1_campaign')]
    user_keys = ['email', 'date_created', 'group_id', 'group_size']
    event_keys = ['event', 'timestamp', 'tags', 'link']
    for sequence, campaign_id in sequences:
        users = signup_api.get_signups_for_archiving(sequence)
        for user in users:
            user['group_size'] = 0
            user['group_id'] = None
            user_groups = groups_api.get_member_groups(user['email'])
            if len(user_groups) == 1:
                user['group_id'] = user_groups[0]['address']
                user['group_size'] = len(user_groups[0]['members'])
            user.update(user['questions'])
            del user['questions']

        # open file for writing data
        timestamp = datetime.now().date().isoformat()
        filename = '_'.join([prefix, 'sequence', str(sequence), 'users', timestamp]).strip('_') + '.csv'

        write_to_csv(users, filename)