def METHOD_NAME(self, query, kwargs):
    if not kwargs['office']:
        return query
    utils.check_election_arguments(kwargs)
    query = query.join(
        models.CandidateHistory,
        models.ScheduleE.candidate_id == models.CandidateHistory.candidate_id,
    ).filter(
        models.CandidateHistory.two_year_period == kwargs['cycle'],
        models.CandidateHistory.office == kwargs['office'][0].upper(),
        models.ScheduleE.report_year.in_([kwargs['cycle'], kwargs['cycle'] - 1]),
    )
    if kwargs['state']:
        query = query.filter(models.CandidateHistory.state == kwargs['state'])
    if kwargs['district']:
        query = query.filter(models.CandidateHistory.district == kwargs['district'])
    return query