<commit_before>import logging

import mailchimp

from django.conf import settings
from django.core.mail import send_mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.core.urlresolvers import reverse

from oauth2_provider.models import AccessToken
from social.apps.django_app.default.models import UserSocialAuth

from common.utils import full_url
from .models import Member

logger = logging.getLogger(__name__)


@receiver(post_save, sender=Member)
def member_post_save_cb(sender, instance, created, raw, update_fields,
                        **kwargs):
    """
    Subscribe or unsubscribe a user from Mailchimp.
    """
    if raw:
        return

    if not settings.MAILCHIMP_API_KEY:
        logger.warn('User changed email preference but no Mailchimp API key '
                    'has been specified, set MAILCHIMP_API_KEY.')

        return

    mc = mailchimp.Mailchimp(settings.MAILCHIMP_API_KEY)

    try:
        address = instance.primary_email.email
    except AttributeError:
        # We're not sure why the callback is firing an extra time, before
        # SignupView.create_account runs (when email not yet saved).
        return

    if instance.newsletter:
        try:
            mc.lists.subscribe(settings.MAILCHIMP_NEWSLETTER_LIST,
                               {'email': address},
                               double_optin=False,
                               update_existing=True)
        except mailchimp.ListAlreadySubscribedError:
            logger.warn('"%s" already subscribed', address)
        except mailchimp.Error as e:
            logger.error('A Mailchimp error occurred: %s, %s', e.__class__, e)
    else:
        try:
            mc.lists.unsubscribe(settings.MAILCHIMP_NEWSLETTER_LIST,
                                 {'email': address},
                                 send_goodbye=False,
                                 send_notify=False)
        except (mailchimp.ListNotSubscribedError,
                mailchimp.EmailNotExistsError):
            logger.warn('"%s" not subscribed', address)
        except mailchimp.Error as e:
            logger.error('A Mailchimp error occurred: %s, %s', e.__class__, e)


def send_connection_email(user, connection_name):
    """
    Email a user to notify them of a new connection.
    """
    params = {
        'user_name': user.member.name,
        'connection_name': connection_name,
        'is_public_data_participant':
            user.member.public_data_participant.enrolled,
        'public_data_sharing_url': full_url(reverse('public-data:home')),
        'research_data_url': full_url(reverse('my-member-research-data')),
    }

    plain = render_to_string('email/notify-connection.txt', params)
    html = render_to_string('email/notify-connection.html', params)

    send_mail('Open Humans Notification: {} Connected'.format(connection_name),
              plain,
              settings.DEFAULT_FROM_EMAIL,
              [user.member.primary_email.email],
              html_message=html)


@receiver(post_save, sender=AccessToken)
def access_token_post_save_cb(sender, instance, created, raw, update_fields,
                              **kwargs):
    """
    Email a user to notify them of any new incoming connections.
    """
    if raw or not created:
        return

    # only notify the user the first time they connect
    if AccessToken.objects.filter(application=instance.application,
                                  user=instance.user).count() > 1:
        return<commit_msg>🔧 change message subject capitalization<commit_after>import logging

import mailchimp

from django.conf import settings
from django.core.mail import send_mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.core.urlresolvers import reverse

from oauth2_provider.models import AccessToken
from social.apps.django_app.default.models import UserSocialAuth

from common.utils import full_url
from .models import Member

logger = logging.getLogger(__name__)


@receiver(post_save, sender=Member)
def member_post_save_cb(sender, instance, created, raw, update_fields,
                        **kwargs):
    """
    Subscribe or unsubscribe a user from Mailchimp.
    """
    if raw:
        return

    if not settings.MAILCHIMP_API_KEY:
        logger.warn('User changed email preference but no Mailchimp API key '
                    'has been specified, set MAILCHIMP_API_KEY.')

        return

    mc = mailchimp.Mailchimp(settings.MAILCHIMP_API_KEY)

    try:
        address = instance.primary_email.email
    except AttributeError:
        # We're not sure why the callback is firing an extra time, before
        # SignupView.create_account runs (when email not yet saved).
        return

    if instance.newsletter:
        try:
            mc.lists.subscribe(settings.MAILCHIMP_NEWSLETTER_LIST,
                               {'email': address},
                               double_optin=False,
                               update_existing=True)
        except mailchimp.ListAlreadySubscribedError:
            logger.warn('"%s" already subscribed', address)
        except mailchimp.Error as e:
            logger.error('A Mailchimp error occurred: %s, %s', e.__class__, e)
    else:
        try:
            mc.lists.unsubscribe(settings.MAILCHIMP_NEWSLETTER_LIST,
                                 {'email': address},
                                 send_goodbye=False,
                                 send_notify=False)
        except (mailchimp.ListNotSubscribedError,
                mailchimp.EmailNotExistsError):
            logger.warn('"%s" not subscribed', address)
        except mailchimp.Error as e:
            logger.error('A Mailchimp error occurred: %s, %s', e.__class__, e)


def send_connection_email(user, connection_name):
    """
    Email a user to notify them of a new connection.
    """
    params = {
        'user_name': user.member.name,
        'connection_name': connection_name,
        'is_public_data_participant':
            user.member.public_data_participant.enrolled,
        'public_data_sharing_url': full_url(reverse('public-data:home')),
        'research_data_url': full_url(reverse('my-member-research-data')),
    }

    plain = render_to_string('email/notify-connection.txt', params)
    html = render_to_string('email/notify-connection.html', params)

    send_mail('Open Humans notification: {} connected'.format(connection_name),
              plain,
              settings.DEFAULT_FROM_EMAIL,
              [user.member.primary_email.email],
              html_message=html)


@receiver(post_save, sender=AccessToken)
def access_token_post_save_cb(sender, instance, created, raw, update_fields,
                              **kwargs):
    """
    Email a user to notify them of any new incoming connections.
    """
    if raw or not created:
        return

    # only notify the user the first time they connect
    if AccessToken.objects.filter(application=instance.application,
                                  user=instance.user).count() > 1:
        return
{'fraction_non_alphanumeric': 0.08284923928077455, 'fraction_numerical': 0.0005532503457814661, 'mean_word_length': 2.3900609470229726, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6307056', 'n_tokens_mistral': 1979, 'n_tokens_neox': 1934, 'n_words': 483}