Example #1
0
def _send_activation_email(user, project, database, base_url):
    """Send an email to the user just after we have defined their diagnosis."""
    advice_modules = {a.advice_id: a for a in _advice_modules(database)}
    advices = [a for a in project.advices if a.advice_id in advice_modules]
    if not advices:
        logging.error(  # pragma: no-cover
            'Weird: the advices that just got created do not exist in DB.'
        )  # pragma: no-cover
        return  # pragma: no-cover
    data = {
        'baseUrl':
        base_url,
        'projectId':
        project.project_id,
        'firstName':
        user.profile.name,
        'ofProjectTitle':
        french.maybe_contract_prefix(
            'de ', "d'",
            french.lower_first_letter(
                _get_job_name(project.target_job, user.profile.gender))),
        'advices': [{
            'adviceId': a.advice_id,
            'title': advice_modules[a.advice_id].title
        } for a in advices],
    }
    response = mail.send_template(
        # https://app.mailjet.com/template/168827/build
        '168827',
        user.profile,
        data,
        dry_run=not _EMAIL_ACTIVATION_ENABLED)
    if response.status_code != 200:
        logging.warning('Error while sending diagnostic email: %s\n%s',
                        response.status_code, response.text)
Example #2
0
def network_vars(user):
    """Compute vars for a given user for the network email.

    Returns:
        a dict with all vars required for the template, or None if no email
        should be sent.
    """
    if not user.projects:
        logging.info('User has no project')
        return None
    project = user.projects[0]

    registered_months_ago = _get_french_months_ago(
        user.registered_at.ToDatetime())
    if not registered_months_ago:
        logging.warning('User registered only recently (%s)',
                        user.registered_at)
        return None

    in_target_domain = _get_best_in_domain_match(
        project.target_job.job_group.rome_id)
    if not in_target_domain:
        logging.warning('Could not find a target domain (%s)',
                        project.target_job.job_group)
        return None

    worst_frustration = next(
        (f for f in (user_pb2.NO_OFFER_ANSWERS, user_pb2.MOTIVATION)
         if f in user.profile.frustrations), None)

    is_hairdresser_or_in_marseille = \
        project.target_job.job_group.rome_id.startswith('D') or \
        project.mobility.city.departement_id == '13'
    other_job_in_city = 'coiffeur à Marseille'
    if is_hairdresser_or_in_marseille:
        other_job_in_city = 'secrétaire à Lyon'

    return {
        'gender':
        user_pb2.Gender.Name(user.profile.gender),
        'firstName':
        user.profile.name,
        'registeredMonthsAgo':
        registered_months_ago,
        'inTargetDomain':
        in_target_domain,
        'frustration':
        user_pb2.Frustration.Name(worst_frustration)
        if worst_frustration else '',
        'otherJobInCity':
        other_job_in_city,
        'jobInCity':
        '%s %s' % (french.lower_first_letter(
            _genderize_job(project.target_job, user.profile.gender)),
                   french.in_city(strip_district(project.mobility.city.name))),
        'emailInUrl':
        parse.quote(user.profile.email),
        'unsubscribeLink':
        '%s/unsubscribe.html?email=%s&auth=%s' %
        (_BASE_URL, parse.quote(user.profile.email),
         parse.quote(auth.create_token(user.profile.email,
                                       role='unsubscribe'))),
    }
Example #3
0
def spontaneous_vars(user, previous_email_campaign_id):
    """Compute vars for a given user for the spontaneous email.

    Returns:
        a dict with all vars required for the template, or None if no email
        should be sent.
    """
    if not user.projects:
        logging.info('User has no project')
        return None
    project = user.projects[0]

    job_group_info = _JOB_GROUPS_INFO.get_collection(_DB).get(
        project.target_job.job_group.rome_id)

    def _should_use_spontaneous(modes):
        return any(mode.mode == job_pb2.SPONTANEOUS_APPLICATION
                   and mode.percentage > 20 for mode in modes.modes)

    application_modes = job_group_info.application_modes
    if not any(
            _should_use_spontaneous(modes)
            for modes in application_modes.values()):
        return None

    registered_months_ago = _get_french_months_ago(
        user.registered_at.ToDatetime())
    if not registered_months_ago:
        logging.warning('User registered only recently (%s)',
                        user.registered_at)
        return None

    has_read_previous_email = previous_email_campaign_id and any(
        email.campaign_id == previous_email_campaign_id and email.status in
        (user_pb2.EMAIL_SENT_OPENED, user_pb2.EMAIL_SENT_CLICKED)
        for email in user.emails_sent)

    def _get_rome_value(fieldname):
        return _ROME_INFO.get_value(project.target_job.job_group.rome_id,
                                    fieldname)

    contact_mode = _get_rome_value('contact_mode')
    if not contact_mode:
        logging.error('There is no contact mode for the job group "%s"',
                      project.target_job.job_group.rome_id)
        return None

    in_a_workplace = _get_rome_value('in_a_workplace') or ''
    if not in_a_workplace and contact_mode != 'BY_EMAIL':
        logging.error(
            'There is no "in_a_workplace" field for the job group "%s".',
            project.target_job.job_group.rome_id)
        return None

    like_your_workplace = _get_rome_value('like_your_workplace') or ''
    if in_a_workplace and not like_your_workplace:
        logging.error(
            'There is no "like_your_workplace" field for the job group "%s".',
            project.target_job.job_group.rome_id)
        return None

    to_the_workplace = _get_rome_value('to_the_workplace')
    if not to_the_workplace:
        to_the_workplace = "à l'entreprise"

    some_companies = _get_rome_value('some_companies')
    if not some_companies:
        some_companies = 'des entreprises'

    what_i_love_about = _get_rome_value('what_i_love_about') or ''
    if user.profile.gender == user_pb2.FEMININE:
        what_i_love_about_feminine = _get_rome_value(
            'what_i_love_about_feminine')
        if what_i_love_about_feminine:
            what_i_love_about = what_i_love_about_feminine
    if not what_i_love_about and contact_mode == 'BY_EMAIL':
        logging.error(
            'There is no "What I love about" field for the job group "%s".',
            project.target_job.job_group.rome_id)
        return None

    why_specific_company = _get_rome_value('why_specific_company')
    if not why_specific_company:
        logging.error(
            'There is no "Why this specific company" field for the job group "%s".',
            project.target_job.job_group.rome_id)
        return None

    various_companies = _get_rome_value('various_companies') or ''

    if project.weekly_applications_estimate == project_pb2.SOME:
        weekly_application_count = '5'
    elif project.weekly_applications_estimate > project_pb2.SOME:
        weekly_application_count = '15'
    else:
        weekly_application_count = ''

    return {
        'applicationComplexity':
        job_pb2.ApplicationProcessComplexity.Name(
            job_group_info.application_complexity),
        'contactMode':
        contact_mode,
        'deepLinkLBB':
        'https://labonneboite.pole-emploi.fr/entreprises/commune/{}/rome/'
        '{}?utm_medium=web&utm_source=bob&utm_campaign=bob-email'.format(
            project.mobility.city.city_id,
            project.target_job.job_group.rome_id),
        'emailInUrl':
        parse.quote(user.profile.email),
        'experienceAsText':
        _EXPERIENCE_AS_TEXT.get(project.seniority, 'peu'),
        'firstName':
        user.profile.name,
        'gender':
        user_pb2.Gender.Name(user.profile.gender),
        'hasReadPreviousEmail':
        'True' if has_read_previous_email else '',
        'inWorkPlace':
        in_a_workplace,
        'jobName':
        french.lower_first_letter(
            _genderize_job(project.target_job, user.profile.gender)),
        'lastName':
        user.profile.last_name,
        'likeYourWorkplace':
        like_your_workplace,
        'registeredMonthsAgo':
        registered_months_ago,
        'someCompanies':
        some_companies,
        'toTheWorkPlace':
        to_the_workplace,
        'unsubscribeLink':
        '{}/unsubscribe.html?email={}&auth={}'.format(
            _BASE_URL, parse.quote(user.profile.email),
            parse.quote(
                auth.create_token(user.profile.email, role='unsubscribe'))),
        'variousCompanies':
        various_companies,
        'weeklyApplicationOptions':
        weekly_application_count,
        'whatILoveAbout':
        what_i_love_about,
        'whySpecificCompany':
        why_specific_company,
    }
Example #4
0
 def test_empty(self):
     """Empty string."""
     sentence = french.lower_first_letter('')
     self.assertEqual('', sentence)
Example #5
0
 def test_one_letter(self):
     """Only one letter."""
     sentence = french.lower_first_letter('T')
     self.assertEqual('t', sentence)
Example #6
0
 def test_all_uppercase(self):
     """All upper case."""
     sentence = french.lower_first_letter('THIS IS ALL UPPERCASE')
     self.assertEqual('tHIS IS ALL UPPERCASE', sentence)