def delete_respondent_by_email(email, session):
    """
    Delete a Respondent by its email

    :param email: Id of Respondent to delete
    :type email: str
    :return: On success it returns None, on failure will raise one of many different exceptions
    """
    logger.info("Starting to delete respondent", email=obfuscate_email(email))

    # We need to get the respondent to make sure they exist, but also because the id (not the party_uuid...for
    # some reason) of the respondent is needed for the later deletion steps.
    respondent = get_single_respondent_by_email(email, session)

    session.query(Enrolment).filter(
        Enrolment.respondent_id == respondent.id).delete()
    session.query(BusinessRespondent).filter(
        BusinessRespondent.respondent_id == respondent.id).delete()
    session.query(PendingEnrolment).filter(
        PendingEnrolment.respondent_id == respondent.id).delete()
    session.query(Respondent).filter(
        Respondent.email_address == email).delete()

    logger.info("Deleted user, about to commit",
                email=obfuscate_email(email),
                party_uuid=str(respondent.party_uuid),
                id=respondent.id)
Esempio n. 2
0
def disable_all_respondent_enrolments(respondent_email, session):
    """Disables all enrolments for a respondent ,  returns the count of the removed enrolments"""

    obfuscated_email = obfuscate_email(respondent_email)

    logger.info('Disabling all enrolments for respondent',
                email=obfuscated_email)

    removed_enrolments_count = 0

    # raises errors if none or multiple, unusual import to avoid circular references
    respondent = get_single_respondent_by_email(respondent_email, session)

    enrolments = query_all_non_disabled_enrolments_respondent(
        respondent.id, session)

    for enrolment in enrolments:
        _change_respondent_enrolment_status(respondent=respondent,
                                            survey_id=enrolment.survey_id,
                                            business_id=enrolment.business_id,
                                            status='DISABLED',
                                            session=session)
        removed_enrolments_count += 1

    logger.info('Completed disabling respondent enrolments',
                email=obfuscated_email,
                removed_enrolment_count=removed_enrolments_count)

    return removed_enrolments_count
Esempio n. 3
0
def get_single_respondent_by_email(email, session):
    """gets a single respondent based on an email address"""
    try:
        respondent = query_single_respondent_by_email(email, session)
    except NoResultFound:
        logger.error("Respondent with email does not exist",
                     email=obfuscate_email(email))
        raise NotFound("Respondent with email does not exist")
    except MultipleResultsFound:
        logger.error("Multiple respondents found for email",
                     email=obfuscate_email(email))
        raise UnprocessableEntity("Multiple users found, unable to proceed")
    logger.info("Found respondent",
                email=obfuscate_email(respondent.email_address),
                party_uuid=respondent.party_uuid,
                id=respondent.id)
    return respondent
    def test_obfuscate_email(self):
        """Test obfuscate_email correctly changes inputted emails"""

        testAddresses = {
            "*****@*****.**": "e*****e@e*********m",
            "*****@*****.**": "p****x@d**********k",
            "*****@*****.**": "f********e@p**********k",
            "*****@*****.**": "m*********n@g*******m",
            "*****@*****.**": "a***********e@e*********m",
            "*****@*****.**":
            "j**************6@l****************k",
            "[email protected]": "m**?@e*********m",
            "*****@*****.**": "m@m***m",
            "joe.bloggs": "j********s",
            "joe.bloggs@": "j********s",
            "@gmail.com": "@g*******m"
        }

        for test in testAddresses:
            self.assertEqual(obfuscate_email(test), testAddresses[test])
Esempio n. 5
0
def query_respondent_by_names_and_emails(first_name, last_name, email, page,
                                         limit, session):
    """
    returns respondents which match first_name, last_name and email, ignoring case in all cases
    if any parameter is empty then it is ignored

    :param first_name: only return respondents whose first name starts with this first_name
    :param last_name: only return respondents whose last name starts with this last_name
    :param email: only return respondents whose email address contains starts with this email
    :param page: return this page of a result set
    :param limit: max number of records per page
    :param session:
    """

    logger.info('Querying respondents by names and/or email',
                email=obfuscate_email(email),
                page=page,
                limit=limit)

    conditions = []

    if first_name:
        conditions.append(Respondent.first_name.ilike(f"{first_name}%"))
    if last_name:
        conditions.append(Respondent.last_name.ilike(f"{last_name}%"))
    if email:
        conditions.append(Respondent.email_address.ilike(f"%{email}%"))

    offset = (page - 1) * limit

    filtered_records = session.query(Respondent).filter(and_(*conditions))

    total_count = filtered_records.count()

    return filtered_records.order_by(
        Respondent.last_name.asc()).offset(offset).limit(limit), total_count