<commit_before>import os
from datetime import datetime, timedelta

from django.conf import settings
from django.core.mail import send_mail

import basket
import waffle
from celery import chain, group, shared_task, Task
from celery.task import task
from celery.exceptions import MaxRetriesExceededError
from elasticutils.contrib.django import get_es
from elasticutils.utils import chunked

from mozillians.common.utils import akismet_spam_check
from mozillians.common.templatetags.helpers import get_object_or_none
from mozillians.users.managers import PUBLIC


BASKET_TASK_RETRY_DELAY = 120  # 2 minutes
BASKET_TASK_MAX_RETRIES = 2  # Total 1+2 = 3 tries
BASKET_URL = getattr(settings, 'BASKET_URL', False)
BASKET_API_KEY = os.environ.get('BASKET_API_KEY', getattr(settings, 'BASKET_API_KEY', False))
BASKET_VOUCHED_NEWSLETTER = getattr(settings, 'BASKET_VOUCHED_NEWSLETTER', False)
BASKET_NDA_NEWSLETTER = getattr(settings, 'BASKET_NDA_NEWSLETTER', False)
BASKET_ENABLED = all([
    BASKET_URL,
    BASKET_API_KEY,
    BASKET_VOUCHED_NEWSLETTER,
    BASKET_NDA_NEWSLETTER
])

INCOMPLETE_ACC_MAX_DAYS = 7
MOZILLIANS_NEWSLETTERS = [BASKET_NDA_NEWSLETTER, BASKET_VOUCHED_NEWSLETTER]


class DebugBasketTask(Task):
    """Base Error Handing Abstract class for all the Basket Tasks."""
    abstract = True

    def on_failure(self, exc, task_id, args, kwargs, einfo):
        # Fallback to ADMINS emails when BASKET_MANAGERS not defined
        basket_managers = getattr(settings, 'BASKET_MANAGERS', None)
        recipients_list = basket_managers or [addr for (_, addr) in settings.ADMINS]

        subject = '[Mozillians - ET] Traceback {0}'.format(task_id)

        body = """
        Something terrible happened for task {task_id}

        Here is the error message:

        exception: {exc}

        args: {args}

        kwargs: {kwargs}

        exception info: {einfo}

        """.format(task_id=task_id, exc=exc, args=args, kwargs=kwargs, einfo=einfo)

        send_mail(subject, body, settings.FROM_NOREPLY, recipients_list, fail_silently=False)


@shared_task(bind=True, base=DebugBasketTask, default_retry_delay=BASKET_TASK_RETRY_DELAY,
             max_retries=BASKET_TASK_MAX_RETRIES)
def lookup_user_task(self, email):
    """Task responsible for getting information about a user in basket."""

    # We need to return always a dictionary for the next task
    result = {}

    try:
        result = basket.lookup_user(email=email)
    except MaxRetriesExceededError as exc:
        raise exc
    except basket.BasketException as exc:
        if not exc[0] == u'User not found':
            raise self.retry(exc=exc)
    return result


@shared_task(bind=True, base=DebugBasketTask, default_retry_delay=BASKET_TASK_RETRY_DELAY,
             max_retries=BASKET_TASK_MAX_RETRIES)
def subscribe_user_task(self, result, email='', newsletters=[], sync='N', trigger_welcome='N'):
    """Subscribes a user to basket newsletters.

    The email to subscribe is provided either from the result of the lookup task
    or from the profile of the user.
    """

    if not result and not email:
        return None

    newsletters_to_subscribe = []
    if result.get('status') == 'ok':
        # This is used when we want to subscribe a different email
        # than the one in the lookup (eg when a user changes emails)<commit_msg>Return basket result when lookup returns `User not found`<commit_after>import os
from datetime import datetime, timedelta

from django.conf import settings
from django.core.mail import send_mail

import basket
import waffle
from celery import chain, group, shared_task, Task
from celery.task import task
from celery.exceptions import MaxRetriesExceededError
from elasticutils.contrib.django import get_es
from elasticutils.utils import chunked

from mozillians.common.utils import akismet_spam_check
from mozillians.common.templatetags.helpers import get_object_or_none
from mozillians.users.managers import PUBLIC


BASKET_TASK_RETRY_DELAY = 120  # 2 minutes
BASKET_TASK_MAX_RETRIES = 2  # Total 1+2 = 3 tries
BASKET_URL = getattr(settings, 'BASKET_URL', False)
BASKET_API_KEY = os.environ.get('BASKET_API_KEY', getattr(settings, 'BASKET_API_KEY', False))
BASKET_VOUCHED_NEWSLETTER = getattr(settings, 'BASKET_VOUCHED_NEWSLETTER', False)
BASKET_NDA_NEWSLETTER = getattr(settings, 'BASKET_NDA_NEWSLETTER', False)
BASKET_ENABLED = all([
    BASKET_URL,
    BASKET_API_KEY,
    BASKET_VOUCHED_NEWSLETTER,
    BASKET_NDA_NEWSLETTER
])

INCOMPLETE_ACC_MAX_DAYS = 7
MOZILLIANS_NEWSLETTERS = [BASKET_NDA_NEWSLETTER, BASKET_VOUCHED_NEWSLETTER]


class DebugBasketTask(Task):
    """Base Error Handing Abstract class for all the Basket Tasks."""
    abstract = True

    def on_failure(self, exc, task_id, args, kwargs, einfo):
        # Fallback to ADMINS emails when BASKET_MANAGERS not defined
        basket_managers = getattr(settings, 'BASKET_MANAGERS', None)
        recipients_list = basket_managers or [addr for (_, addr) in settings.ADMINS]

        subject = '[Mozillians - ET] Traceback {0}'.format(task_id)

        body = """
        Something terrible happened for task {task_id}

        Here is the error message:

        exception: {exc}

        args: {args}

        kwargs: {kwargs}

        exception info: {einfo}

        """.format(task_id=task_id, exc=exc, args=args, kwargs=kwargs, einfo=einfo)

        send_mail(subject, body, settings.FROM_NOREPLY, recipients_list, fail_silently=False)


@shared_task(bind=True, base=DebugBasketTask, default_retry_delay=BASKET_TASK_RETRY_DELAY,
             max_retries=BASKET_TASK_MAX_RETRIES)
def lookup_user_task(self, email):
    """Task responsible for getting information about a user in basket."""

    # We need to return always a dictionary for the next task
    result = {}

    try:
        result = basket.lookup_user(email=email)
    except MaxRetriesExceededError as exc:
        raise exc
    except basket.BasketException as exc:
        if not exc[0] == u'User not found':
            raise self.retry(exc=exc)
        result = exc.result
    return result


@shared_task(bind=True, base=DebugBasketTask, default_retry_delay=BASKET_TASK_RETRY_DELAY,
             max_retries=BASKET_TASK_MAX_RETRIES)
def subscribe_user_task(self, result, email='', newsletters=[], sync='N', trigger_welcome='N'):
    """Subscribes a user to basket newsletters.

    The email to subscribe is provided either from the result of the lookup task
    or from the profile of the user.
    """

    if not result and not email:
        return None

    newsletters_to_subscribe = []
    if result.get('status') == 'ok':
        # This is used when we want to subscribe a different email
        # than the one in the lookup (eg when a user changes emails)
{'fraction_non_alphanumeric': 0.078908335805399, 'fraction_numerical': 0.003263126668644319, 'mean_word_length': 4.035847647498133, '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': '6405745', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2214, 'n_words': 597}