def METHOD_NAME(query, kwargs, candidate_column, cycle_column=None, year_column=None):
    if not kwargs.get('office'):
        return query
    utils.check_election_arguments(kwargs)
    cycle = get_cycle(kwargs)
    query = query.join(
        models.CandidateHistory,
        candidate_column == models.CandidateHistory.candidate_id,
    ).filter(
        models.CandidateHistory.two_year_period == cycle,
        models.CandidateHistory.office == kwargs['office'][0].upper(),
    )
    if kwargs.get('state'):
        query = query.filter(models.CandidateHistory.state == kwargs['state'])
    if kwargs.get('district'):
        query = query.filter(models.CandidateHistory.district == kwargs['district'])
    return query