def latest_consent(user, research_study_id):
    """Lookup latest valid consent for user

    :param user: subject of query
    :param research_study_id: limit query to respective value

    If latest consent for user is 'suspended' or 'deleted', this function
    will return None.  See ``consent_withdrawal_dates()`` for that need.

    :returns: the most recent consent based on given criteria, or None
        if no match is located

    """
    # consents are ordered desc(acceptance_date)
    for consent in user.valid_consents:
        if consent.research_study_id != research_study_id:
            continue
        if consent.status == 'consented':
            return consent

    return None