Exemplo n.º 1
0
def filter_election(query, kwargs, candidate_column, cycle_column=None, year_column=None):
    if not kwargs.get('office'):
        return query
    if isinstance(kwargs['cycle'], list):
        if len(kwargs['cycle']) != 1:
            raise exceptions.ApiError(
                'Must include exactly one argument "cycle"',
                status_code=422,
            )
        kwargs['cycle'] = kwargs['cycle'][0]
    utils.check_election_arguments(kwargs)
    query = query.join(
        models.CandidateHistory,
        candidate_column == models.CandidateHistory.candidate_id,
    ).filter(
        models.CandidateHistory.two_year_period == kwargs['cycle'],
        models.CandidateHistory.office == kwargs['office'][0].upper(),
    )
    if cycle_column:
        query = query.filter(cycle_column == kwargs['cycle'])
    elif year_column:
        query = query.filter(year_column.in_(kwargs['cycle'], kwargs['cycle'] - 1))
    else:
        raise ValueError('Must provide `cycle_column` or `year_column`')
    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
Exemplo n.º 2
0
def filter_election(query,
                    kwargs,
                    candidate_column,
                    cycle_column=None,
                    year_column=None):
    if not kwargs['office']:
        return query
    if isinstance(kwargs['cycle'], list):
        if len(kwargs['cycle']) != 1:
            raise exceptions.ApiError(
                'Must include exactly one argument "cycle"',
                status_code=422,
            )
        kwargs['cycle'] = kwargs['cycle'][0]
    utils.check_election_arguments(kwargs)
    query = query.join(
        models.CandidateHistory,
        candidate_column == models.CandidateHistory.candidate_id,
    ).filter(
        models.CandidateHistory.two_year_period == kwargs['cycle'],
        models.CandidateHistory.office == kwargs['office'][0].upper(),
    )
    if cycle_column:
        query = query.filter(cycle_column == kwargs['cycle'])
    elif year_column:
        query = query.filter(
            year_column.in_(kwargs['cycle'], kwargs['cycle'] - 1))
    else:
        raise ValueError('Must provide `cycle_column` or `year_column`')
    if kwargs['state']:
        query = query.filter(models.CandidateHistory.state == kwargs['state'])
    if kwargs['district']:
        query = query.filter(
            models.CandidateHistory.district == kwargs['district'])
    return query
Exemplo n.º 3
0
 def get(self, **kwargs):
     utils.check_election_arguments(kwargs)
     aggregates = self._get_aggregates(kwargs).subquery()
     expenditures = self._get_expenditures(kwargs).subquery()
     return db.session.query(
         aggregates.c.count,
         aggregates.c.receipts,
         aggregates.c.disbursements,
         expenditures.c.independent_expenditures,
     ).first()._asdict()
Exemplo n.º 4
0
 def get(self, **kwargs):
     utils.check_election_arguments(kwargs)
     aggregates = self._get_aggregates(kwargs).subquery()
     expenditures = self._get_expenditures(kwargs).subquery()
     return db.session.query(
         aggregates.c.count,
         aggregates.c.receipts,
         aggregates.c.disbursements,
         expenditures.c.independent_expenditures,
     ).first()._asdict()
Exemplo n.º 5
0
 def _get_records(self, kwargs):
     utils.check_election_arguments(kwargs)
     totals_model = office_totals_map[kwargs['office']]
     pairs = self._get_pairs(totals_model, kwargs).subquery()
     aggregates = self._get_aggregates(pairs).subquery()
     filings = self._get_filings(pairs).subquery()
     return db.session.query(
         aggregates,
         filings,
     ).join(
         filings,
         aggregates.c.candidate_id == filings.c.candidate_id,
     )
Exemplo n.º 6
0
 def _get_records(self, kwargs):
     utils.check_election_arguments(kwargs)
     totals_model = office_totals_map[kwargs['office']]
     pairs = self._get_pairs(totals_model, kwargs).subquery()
     aggregates = self._get_aggregates(pairs).subquery()
     filings = self._get_filings(pairs).subquery()
     return db.session.query(
         aggregates,
         filings,
     ).join(
         filings,
         aggregates.c.candidate_id == filings.c.candidate_id,
     )
Exemplo n.º 7
0
    def build_query(self, **kwargs):
        utils.check_election_arguments(kwargs)
        totals_model = office_totals_map[kwargs['office']]
        basicPairs = self._get_basicPairs(totals_model, kwargs).subquery()
        pairs = self._get_pairs(basicPairs).subquery()
        aggregates = self._get_aggregates(pairs).subquery()
        candAggregates = self._get_candAggregates(aggregates).subquery()

        final_query = db.session.query(
            candAggregates,
            BaseConcreteCommittee.name.label('candidate_pcc_name')).outerjoin(
                BaseConcreteCommittee, candAggregates.c.candidate_pcc_id ==
                BaseConcreteCommittee.committee_id).distinct()
        return final_query
Exemplo n.º 8
0
 def _get_records(self, kwargs):
     utils.check_election_arguments(kwargs)
     totals_model = office_totals_map[kwargs["office"]]
     pairs = self._get_pairs(totals_model, kwargs).subquery()
     aggregates = self._get_aggregates(pairs).subquery()
     filings = self._get_filings(pairs).subquery()
     outcomes = self._get_outcomes(kwargs).subquery()
     return (
         db.session.query(
             aggregates, filings, sa.case([(outcomes.c.cand_id != None, True)], else_=False).label("won")  # noqa
         )
         .join(filings, aggregates.c.candidate_id == filings.c.candidate_id)
         .outerjoin(outcomes, aggregates.c.candidate_id == outcomes.c.cand_id)
     )
Exemplo n.º 9
0
 def build_query(self, **kwargs):
     utils.check_election_arguments(kwargs)
     totals_model = office_totals_map[kwargs['office']]
     pairs = self._get_pairs(totals_model, kwargs).subquery()
     aggregates = self._get_aggregates(pairs).subquery()
     latest = self._get_latest(pairs).subquery()
     return db.session.query(
         aggregates, latest,
         BaseConcreteCommittee.name.label('candidate_pcc_name')).outerjoin(
             latest,
             aggregates.c.candidate_id == latest.c.candidate_id,
         ).outerjoin(
             BaseConcreteCommittee, aggregates.c.candidate_pcc_id ==
             BaseConcreteCommittee.committee_id).distinct()
Exemplo n.º 10
0
 def filter_election(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
Exemplo n.º 11
0
 def filter_election(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
Exemplo n.º 12
0
def filter_election(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
Exemplo n.º 13
0
def filter_election(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
Exemplo n.º 14
0
 def _get_records(self, kwargs):
     utils.check_election_arguments(kwargs)
     totals_model = office_totals_map[kwargs['office']]
     pairs = self._get_pairs(totals_model, kwargs).subquery()
     aggregates = self._get_aggregates(pairs).subquery()
     outcomes = self._get_outcomes(kwargs).subquery()
     latest = self._get_latest(pairs).subquery()
     return db.session.query(
         aggregates,
         latest,
         sa.case(
             [(outcomes.c.cand_id != None, True)],  # noqa
             else_=False,
         ).label('won'),
     ).outerjoin(
         latest,
         aggregates.c.candidate_id == latest.c.candidate_id,
     ).outerjoin(
         outcomes,
         aggregates.c.candidate_id == outcomes.c.cand_id,
     ).distinct()