<commit_before>import mailchimp
from website import settings
from framework.tasks import app


def get_mailchimp_api():
    if not settings.MAILCHIMP_API_KEY:
        raise RuntimeError("An API key is required to connect to Mailchimp.")
    return mailchimp.Mailchimp(settings.MAILCHIMP_API_KEY)


def get_list_id_from_name(list_name):
    m = get_mailchimp_api()
    mailing_list = m.lists.list(filters={'list_name': list_name})
    return mailing_list['data'][0]['id']


def get_list_name_from_id(list_id):
    m = get_mailchimp_api()
    mailing_list = m.lists.list(filters={'list_id': list_id})
    return mailing_list['data'][0]['name']

@app.task
def subscribe(list_name, username):
    m = get_mailchimp_api()
    list_id = get_list_id_from_name(list_name=list_name)
    m.lists.subscribe(id=list_id, email={'email': username}, double_optin=False, update_existing=True)


@app.task
def unsubscribe(list_name, username):
    m = get_mailchimp_api()
    list_id = get_list_id_from_name(list_name=list_name)
    m.lists.unsubscribe(id=list_id, email={'email': username})


subscribe_mailchimp = (
    subscribe.delay
    if settings.USE_CELERY
    else subscribe)

unsubscribe_mailchimp = (
    unsubscribe.delay
    if settings.USE_CELERY
    else unsubscribe
)<commit_msg>Add docstring to mailchimp_utils.unsubscribe<commit_after>import mailchimp
from website import settings
from framework.tasks import app


def get_mailchimp_api():
    if not settings.MAILCHIMP_API_KEY:
        raise RuntimeError("An API key is required to connect to Mailchimp.")
    return mailchimp.Mailchimp(settings.MAILCHIMP_API_KEY)


def get_list_id_from_name(list_name):
    m = get_mailchimp_api()
    mailing_list = m.lists.list(filters={'list_name': list_name})
    return mailing_list['data'][0]['id']


def get_list_name_from_id(list_id):
    m = get_mailchimp_api()
    mailing_list = m.lists.list(filters={'list_id': list_id})
    return mailing_list['data'][0]['name']

@app.task
def subscribe(list_name, username):
    m = get_mailchimp_api()
    list_id = get_list_id_from_name(list_name=list_name)
    m.lists.subscribe(id=list_id, email={'email': username}, double_optin=False, update_existing=True)


@app.task
def unsubscribe(list_name, username):
    """ Unsubscribe a user from a mailchimp mailing list given its name.

        :param str list_name: mailchimp mailing list name
        :param str username: current user's email

        A ListNotSubscribed error will be raised if a user
        not subscribed to the list tries to unsubscribe again.
    """
    m = get_mailchimp_api()
    list_id = get_list_id_from_name(list_name=list_name)
    m.lists.unsubscribe(id=list_id, email={'email': username})


subscribe_mailchimp = (
    subscribe.delay
    if settings.USE_CELERY
    else subscribe)

unsubscribe_mailchimp = (
    unsubscribe.delay
    if settings.USE_CELERY
    else unsubscribe
)
{'fraction_non_alphanumeric': 0.09862068965517241, 'fraction_numerical': 0.001379310344827586, 'mean_word_length': 4.611218568665377, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4410395', 'n_tokens_mistral': 990, 'n_tokens_neox': 979, 'n_words': 207}