Exemplo n.º 1
0
def _get_find_diploma_vars(user: user_pb2.User,
                           database: Optional[
                               pymongo.database.Database] = None,
                           **unused_kwargs: Any) -> Optional[Dict[str, Any]]:
    """Compute vars for the "Prepare your application" email."""

    project = user.projects[0]
    assert database
    scoring_project = scoring.ScoringProject(project, user, database)

    if not any(s.strategy_id == 'get-diploma'
               for s in project.opened_strategies):
        return None

    trainings = scoring_project.get_trainings()[:3]

    deep_link_training_url = \
        campaign.get_deep_link_advice(user.user_id, project, 'training')

    return dict(
        campaign.get_default_coaching_email_vars(user), **{
            'deepTrainingAdviceUrl':
            deep_link_training_url,
            'inDepartement':
            scoring_project.populate_template('%inDepartement'),
            'loginUrl':
            campaign.create_logged_url(user.user_id,
                                       f'/projet/{project.project_id}'),
            'numTrainings':
            len(trainings),
            'ofJobName':
            scoring_project.populate_template('%ofJobName'),
            'trainings': [json_format.MessageToDict(t) for t in trainings],
        })
Exemplo n.º 2
0
def _make_application_mode_section(application_modes, advices, user_id):
    if not application_modes:
        return None
    best_application_mode = next((mode for mode in sorted(
        application_modes, key=lambda mode: -mode.percentage) if mode.mode),
                                 None)
    if not best_application_mode:
        return None
    application_mode_advice = ''
    if best_application_mode == job_pb2.SPONTANEOUS_APPLICATION:
        application_mode_advice = 'spontaneous-application'
    if best_application_mode == job_pb2.PERSONAL_OR_PROFESSIONAL_CONTACTS:
        application_mode_advice = next(
            (advice.advice_id
             for advice in advices if advice.advice_id.startswith('network')),
            '')
    application_mode_link = ''
    if application_mode_advice:
        application_mode_link = campaign.create_logged_url(
            user_id,
            path='/projet/0/avancer/{}'.format(application_mode_advice))
    return {
        'link': application_mode_link,
        'title': _APPLICATION_MODES_SHORT[best_application_mode.mode],
        'name': _APPLICATION_MODES[best_application_mode.mode],
        'percent': str(round(best_application_mode.percentage)),
    }
Exemplo n.º 3
0
    def test_create_logged_url(self) -> None:
        """Test the create logged url function."""

        user_id = '02499e64387edfcc2ab7a948'
        base_url = f'https://www.bob-emploi.fr/project/0/wow-baker?userId={user_id}'
        regex = re.compile(rf'^{re.escape(base_url)}&authToken=(\d+\.[a-f0-9]+)$')
        logged_url = campaign.create_logged_url(user_id, '/project/0/wow-baker')
        self.assertRegex(logged_url, regex)

        match_token = regex.match(logged_url)
        assert match_token
        token = match_token.group(1)
        self.assertTrue(auth.check_token(user_id, token, role='auth'))
Exemplo n.º 4
0
    def test_create_logged_url(self):
        """Test the create logged url function."""

        user_id = '02499e64387edfcc2ab7a948'
        regex = re.compile(r'^{}&authToken=(\d+\.[a-f0-9]+)$'.format(
            re.escape(
                'https://www.bob-emploi.fr/project/0/avancer/wow-baker?user={}'
                .format(user_id))))
        logged_url = campaign.create_logged_url(
            user_id, '/project/0/avancer/wow-baker')
        self.assertRegex(logged_url, regex)

        token = regex.match(logged_url).group(1)
        self.assertTrue(auth.check_token(user_id, token, role='auth'))
Exemplo n.º 5
0
def _get_prepare_your_application_vars(
        user: user_pb2.User, **unused_kwargs: Any) -> Optional[Dict[str, Any]]:
    """Compute vars for the "Prepare your application" email."""

    project = user.projects[0]

    deep_link_motivation_email_url = \
        campaign.get_deep_link_advice(user.user_id, project, 'motivation-email')

    return dict(campaign.get_default_coaching_email_vars(user), **{
        'deepLinkMotivationEmailUrl': deep_link_motivation_email_url,
        'hasInterviewFrustration':
        campaign.as_template_boolean(user_pb2.INTERVIEW in user.profile.frustrations),
        'hasSelfConfidenceFrustration':
        campaign.as_template_boolean(user_pb2.SELF_CONFIDENCE in user.profile.frustrations),
        'loginUrl': campaign.create_logged_url(user.user_id, f'/projet/{project.project_id}'),
    })
Exemplo n.º 6
0
def _get_jobbing_vars(user: user_pb2.User,
                      database: Optional[pymongo.database.Database] = None,
                      **unused_kwargs: Any) -> Optional[Dict[str, Any]]:
    """Compute vars for the "Jobbing" email."""

    project = user.projects[0]

    if not any(s.strategy_id == 'diploma-free-job'
               for s in project.opened_strategies):
        return None

    assert database
    scoring_project = scoring.ScoringProject(project, user, database)
    model = scoring.get_scoring_model('advice-reorient-jobbing')
    if not model:
        return None
    reorient_jobs = typing.cast(
        reorient_jobbing_pb2.JobbingReorientJobs,
        model.get_expanded_card_data(scoring_project),
    ).reorient_jobbing_jobs
    if not reorient_jobs:
        return None

    return dict(
        campaign.get_default_coaching_email_vars(user), **{
            'inDepartement':
            scoring_project.populate_template('%inDepartement'),
            'jobs': [{
                'name': job.name
            } for job in reorient_jobs],
            'loginUrl':
            campaign.create_logged_url(user.user_id,
                                       f'/projet/{project.project_id}'),
            'ofJobName':
            scoring_project.populate_template('%ofJobName'),
        })
Exemplo n.º 7
0
def _get_imt_vars(user: user_pb2.User,
                  database: Optional[pymongo.database.Database] = None,
                  **unused_kwargs: Any) -> Optional[Dict[str, Any]]:
    """Compute vars for the "IMT" email."""

    project = user.projects[0]
    assert database
    scoring_project = scoring.ScoringProject(project, user, database)

    genderized_job_name = french.lower_first_letter(
        french.genderize_job(project.target_job, user.profile.gender))

    departement_id = project.city.departement_id
    rome_id = project.target_job.job_group.rome_id
    local_diagnosis = scoring_project.local_diagnosis()
    if not local_diagnosis.HasField('imt'):
        logging.info('User market has no IMT data')
        return None
    imt = local_diagnosis.imt

    shown_sections = []

    market_stress_section = _make_market_stress_section(
        imt.yearly_avg_offers_per_10_candidates)
    if market_stress_section:
        shown_sections.append('marketStress')

    application_modes_section = _make_application_mode_section(
        scoring_project.get_best_application_mode(), project, user.user_id)
    if application_modes_section:
        shown_sections.append('applicationModes')

    departements_section = _make_departements_section(
        departement_id,
        _get_best_departements_for_job_group(rome_id, database),
        project.area_type, database)
    if departements_section:
        shown_sections.append('departements')

    employment_types_section = _make_employment_type_section(
        imt.employment_type_percentages)
    if employment_types_section:
        shown_sections.append('employmentTypes')

    months_section = _make_months_section(imt.active_months)
    if months_section:
        shown_sections.append('months')

    if len(shown_sections) < 3:
        logging.info('Only %d section(s) to be shown for user (%s).',
                     len(shown_sections), shown_sections)
        return None

    imt_link = 'http://candidat.pole-emploi.fr/marche-du-travail/statistiques?' \
        f'codeMetier={project.target_job.code_ogr}&codeZoneGeographique={departement_id}&' \
        'typeZoneGeographique=DEPARTEMENT'

    in_departement = geo.get_in_a_departement_text(database, departement_id)
    job_name_in_departement = f'{genderized_job_name} {in_departement}'

    return dict(
        campaign.get_default_coaching_email_vars(user), **{
            'applicationModes':
            _make_section(application_modes_section),
            'departements':
            _make_section(departements_section),
            'employmentType':
            _make_section(employment_types_section),
            'imtLink':
            imt_link,
            'inCity':
            french.in_city(project.city.name),
            'jobNameInDepartement':
            job_name_in_departement,
            'loginUrl':
            campaign.create_logged_url(user.user_id),
            'marketStress':
            _make_section(market_stress_section),
            'months':
            _make_section(months_section),
            'ofJobNameInDepartement':
            french.maybe_contract_prefix('de ', "d'", job_name_in_departement),
            'ofJobName':
            french.maybe_contract_prefix('de ', "d'", genderized_job_name),
        })
Exemplo n.º 8
0
def imt_vars(user, database):
    """Compute vars for the "IMT" email."""

    if not user.projects:
        logging.info('User has no project')
        return None
    project = user.projects[0]

    genderized_job_name = french.lower_first_letter(
        french.genderize_job(project.target_job, user.profile.gender))

    departement_id = project.mobility.city.departement_id
    rome_id = project.target_job.job_group.rome_id
    diagnosis_key = '{}:{}'.format(departement_id, rome_id)
    local_diagnosis = _LOCAL_DIAGNOSIS.get_collection(database).get(
        diagnosis_key)
    if not local_diagnosis:
        logging.info('User market does not exist')
        return None
    imt = local_diagnosis.imt
    if not imt:
        logging.info('User market has no IMT data')
        return None

    shown_sections = 0

    market_stress_section = _make_market_stress_section(
        imt.yearly_avg_offers_per_10_candidates)
    if market_stress_section:
        shown_sections += 1

    application_modes_section = _make_application_mode_section(
        campaign.get_application_modes(rome_id, database), project.advices,
        user.user_id)
    if application_modes_section:
        shown_sections += 1

    departements_section = _make_departements_section(
        project.mobility.city.departement_id,
        _get_best_departements_for_job_group(rome_id, database),
        project.mobility.area_type, database)
    if departements_section:
        shown_sections += 1

    employment_types_section = _make_employment_type_section(
        sorted(imt.employment_type_percentages, key=lambda e: e.percentage))
    if employment_types_section:
        shown_sections += 1

    months_section = _make_months_section(imt.active_months)
    if months_section:
        shown_sections += 1

    if shown_sections < 3:
        logging.info('Only %d section(s) to be shown for user.',
                     shown_sections)
        return None

    imt_link = 'http://candidat.pole-emploi.fr/marche-du-travail/statistiques?' + \
        'codeMetier={}&codeZoneGeographique={}&typeZoneGeographique=DEPARTEMENT'.format(
            project.target_job.code_ogr, departement_id)

    job_name_in_departement = '{} {}'.format(
        genderized_job_name,
        geo.get_in_a_departement_text(database,
                                      project.mobility.city.departement_id))

    return dict(
        campaign.get_default_vars(user), **{
            'applicationModes':
            _make_section(application_modes_section),
            'departements':
            _make_section(departements_section),
            'employmentType':
            _make_section(employment_types_section),
            'imtLink':
            imt_link,
            'inCity':
            french.in_city(project.mobility.city.name),
            'jobNameInDepartement':
            job_name_in_departement,
            'loginUrl':
            campaign.create_logged_url(user.user_id),
            'marketStress':
            _make_section(market_stress_section),
            'months':
            _make_section(months_section),
            'ofJobNameInDepartement':
            french.maybe_contract_prefix('de ', "d'", job_name_in_departement),
            'ofJobName':
            french.maybe_contract_prefix('de ', "d'", genderized_job_name),
            'showPs':
            campaign.as_template_boolean(
                _can_go_to_arles_hotellerie_event(rome_id, project.mobility)),
            'statusUpdateUrl':
            campaign.get_status_update_link(user.user_id, user.profile),
        })
Exemplo n.º 9
0
def _account_deletion_notice_vars(user: user_pb2.User,
                                  **unused_kwargs: Any) -> Dict[str, str]:
    return dict(campaign.get_default_vars(user),
                loginUrl=campaign.create_logged_url(user.user_id))