Exemple #1
0
class ElectionsListView(utils.Resource):

    model = ElectionsList
    schema = schemas.ElectionsListSchema
    page_schema = schemas.ElectionsListPageSchema

    filter_multi_fields = [
        ('cycle', ElectionsList.cycle),
    ]

    @use_kwargs(args.paging)
    @use_kwargs(args.elections_list)
    @use_kwargs(args.make_multi_sort_args(default=[
        'sort_order',
        'district',
    ]))
    @marshal_with(schemas.ElectionsListPageSchema())
    def get(self, **kwargs):
        query = self._get_elections(kwargs)
        return utils.fetch_page(query, kwargs, model=ElectionsList, multi=True)

    def _get_elections(self, kwargs):
        """Get elections from ElectionsList model."""
        query = db.session.query(ElectionsList)
        if kwargs.get('office'):
            values = [each[0].upper() for each in kwargs['office']]
            query = query.filter(ElectionsList.office.in_(values))
        if kwargs.get('state'):
            query = query.filter(
                sa.or_(
                    ElectionsList.state.in_(kwargs['state']),
                    ElectionsList.office == 'P',
                ))
        if kwargs.get('district'):
            query = query.filter(
                sa.or_(
                    ElectionsList.district.in_(kwargs['district']),
                    ElectionsList.office.in_(['P', 'S']),
                ), )
        if kwargs.get('zip'):
            query = self._filter_zip(query, kwargs)

        return filters.filter_multi(query, kwargs, self.filter_multi_fields)

    def _filter_zip(self, query, kwargs):
        """Filter query by zip codes."""
        districts = db.session.query(ZipsDistricts).filter(
            cast(ZipsDistricts.zip_code, Integer).in_(kwargs['zip']),
            ZipsDistricts.active == 'Y').subquery()
        return query.join(
            districts,
            sa.or_(
                # House races from matching states and districts
                sa.and_(
                    ElectionsList.district == districts.c['district'],
                    ElectionsList.state == districts.c['state_abbrevation'],
                ),
                # Senate and presidential races from matching states
                sa.and_(
                    sa.or_(ElectionsList.district == '00'),
                    ElectionsList.state.in_(
                        [districts.c['state_abbrevation'], 'US'])),
            ))
Exemple #2
0
class ElectionsListView(utils.Resource):

    model = ElectionsList
    schema = schemas.ElectionsListSchema
    page_schema = schemas.ElectionsListPageSchema

    filter_multi_fields = [
        ('cycle', ElectionsList.cycle),
    ]

    @use_kwargs(args.paging)
    @use_kwargs(args.elections_list)
    @use_kwargs(args.make_sort_args('sort_order, district'))
    @marshal_with(schemas.ElectionsListPageSchema())
    def get(self, **kwargs):
        query = self._get_elections(kwargs)
        return utils.fetch_page(query, kwargs)

    def _get_elections(self, kwargs):
        """Get elections from ElectionsList model."""
        query = db.session.query(ElectionsList)
        if kwargs.get('office'):
            values = [each[0].upper() for each in kwargs['office']]
            query = query.filter(ElectionsList.office.in_(values))
        if kwargs.get('state'):
            query = query.filter(
                sa.or_(
                    ElectionsList.state.in_(kwargs['state']),
                    ElectionsList.office == 'P',
                ))
        if kwargs.get('district'):
            query = query.filter(
                sa.or_(
                    ElectionsList.district.in_(kwargs['district']),
                    ElectionsList.office.in_(['P', 'S']),
                ), )
        if kwargs.get('zip'):
            query = self._filter_zip(query, kwargs)

        return filters.filter_multi(query, kwargs, self.filter_multi_fields)

    def _filter_zip(self, query, kwargs):
        """Filter query by zip codes."""
        fips_states = sa.Table('ofec_fips_states',
                               db.metadata,
                               autoload_with=db.engine)
        zips_districts = sa.Table('ofec_zips_districts',
                                  db.metadata,
                                  autoload_with=db.engine)
        districts = db.session.query(
            zips_districts,
            fips_states,
        ).join(
            fips_states,
            zips_districts.c['State'] ==
            fips_states.c['FIPS State Numeric Code'],
        ).filter(zips_districts.c['ZCTA'].in_(kwargs['zip'])).subquery()
        return query.join(
            districts,
            sa.or_(
                # House races from matching states and districts
                sa.and_(
                    cast(ElectionsList.district,
                         Integer) == districts.c['Congressional District'],
                    ElectionsList.state == districts.c['Official USPS Code'],
                ),
                # Senate and presidential races from matching states
                sa.and_(
                    sa.or_(ElectionsList.district == '00'),
                    ElectionsList.state.in_(
                        [districts.c['Official USPS Code'], 'US'])),
            ))