예제 #1
0
class CandidateTotalsView(utils.Resource):
    @use_kwargs(args.paging)
    @use_kwargs(args.totals)
    @use_kwargs(args.candidate_committee_totals)
    @use_kwargs(args.make_sort_args(default='-cycle'))
    @marshal_with(schemas.CommitteeTotalsPageSchema(), apply=False)
    def get(self, candidate_id, **kwargs):
        query, totals_class, totals_schema = self.build_query(
            candidate_id=candidate_id, **kwargs)
        if kwargs['sort']:
            validator = args.IndexValidator(totals_class)
            validator(kwargs['sort'])
        page = utils.fetch_page(query, kwargs, model=totals_class)
        return totals_schema().dump(page).data

    def build_query(self, candidate_id=None, **kwargs):
        totals_class, totals_schema = candidate_totals_schema_map.get(
            self._resolve_committee_type(candidate_id=candidate_id, **kwargs),
            default_schemas,
        )
        query = totals_class.query
        if kwargs.get('cycle'):
            query = query.filter(totals_class.cycle.in_(kwargs['cycle']))
        if candidate_id:
            query = query.filter(totals_class.candidate_id == candidate_id)
        if kwargs.get('full_election'):
            query = query.filter(
                totals_class.full_election == kwargs['full_election'])
        return query, totals_class, totals_schema

    def _resolve_committee_type(self, candidate_id=None, **kwargs):
        if candidate_id is not None:
            return candidate_id[0]
예제 #2
0
파일: totals.py 프로젝트: mbokinala/openFEC
class TotalsCommitteeView(ApiResource):
    @use_kwargs(args.paging)
    @use_kwargs(args.totals)
    @use_kwargs(args.make_sort_args(default='-cycle'))
    @marshal_with(schemas.CommitteeTotalsPageSchema(), apply=False)
    def get(self, committee_id=None, committee_type=None, **kwargs):
        query, totals_class, totals_schema = self.build_query(
            committee_id=committee_id.upper(), committee_type=committee_type, **kwargs
        )
        page = utils.fetch_page(query, kwargs, model=totals_class)
        return totals_schema().dump(page).data

    def build_query(self, committee_id=None, committee_type=None, **kwargs):
        totals_class, totals_schema = totals_schema_map.get(
            self._resolve_committee_type(
                committee_id=committee_id.upper(), committee_type=committee_type, **kwargs
            ),
            default_schemas,
        )
        query = totals_class.query
        if committee_id is not None:
            query = query.filter(totals_class.committee_id == committee_id.upper())
        if kwargs.get('cycle'):
            query = query.filter(totals_class.cycle.in_(kwargs['cycle']))

        return query, totals_class, totals_schema

    def _resolve_committee_type(self, committee_id=None, committee_type=None, **kwargs):
        if committee_id is not None:
            query = models.CommitteeHistory.query.filter_by(committee_id=committee_id)
            if kwargs.get('cycle'):
                query = query.filter(models.CommitteeHistory.cycle.in_(kwargs['cycle']))
            query = query.order_by(sa.desc(models.CommitteeHistory.cycle))
            committee = query.first_or_404()
            return committee.committee_type
예제 #3
0
class TotalsView(Resource):

    @args.register_kwargs(args.paging)
    @args.register_kwargs(args.totals)
    @args.register_kwargs(args.make_sort_args(default=['-cycle']))
    @schemas.marshal_with(schemas.CommitteeTotalsPageSchema(), wrap=False)
    def get(self, committee_id, **kwargs):
        totals_class, totals_schema = totals_schema_map.get(
            self._resolve_committee_type(committee_id, kwargs),
            default_schemas,
        )
        validator = args.IndexValidator(totals_class)
        for key in kwargs['sort']:
            validator(key)
        totals = self.get_totals(committee_id, totals_class, kwargs)
        page = utils.fetch_page(totals, kwargs, model=totals_class)
        return totals_schema().dump(page).data

    def get_totals(self, committee_id, totals_class, kwargs):
        totals = totals_class.query.filter_by(committee_id=committee_id)
        if kwargs['cycle']:
            totals = totals.filter(totals_class.cycle.in_(kwargs['cycle']))
        return totals

    def _resolve_committee_type(self, committee_id, kwargs):
        query = models.CommitteeHistory.query.filter_by(committee_id=committee_id)
        if kwargs['cycle']:
            query = query.filter(models.CommitteeHistory.cycle.in_(kwargs['cycle']))
        query = query.order_by(sa.desc(models.CommitteeHistory.cycle))
        committee = query.first_or_404()
        return committee.committee_type
예제 #4
0
파일: totals.py 프로젝트: syyunn/openFEC
class CandidateTotalsView(utils.Resource):
    @use_kwargs(args.paging)
    @use_kwargs(args.candidate_totals_detail)
    @use_kwargs(args.make_sort_args(default='-cycle'))
    @use_kwargs(args.check_full_election_parameter)
    @marshal_with(schemas.CommitteeTotalsPageSchema(), apply=False)
    def get(self, candidate_id, **kwargs):
        query, totals_class, totals_schema = self.build_query(
            candidate_id=candidate_id.upper(), **kwargs)
        if kwargs['sort']:
            validator = args.IndexValidator(totals_class)
            validator(kwargs['sort'])
        page = utils.fetch_page(query, kwargs, model=totals_class)
        return totals_schema().dump(page).data

    def build_query(self, candidate_id=None, **kwargs):
        totals_class, totals_schema = candidate_totals_schema_map.get(
            self._resolve_committee_type(candidate_id=candidate_id.upper(),
                                         **kwargs),
            default_schemas,
        )
        query = totals_class.query
        query = query.filter(totals_class.candidate_id == candidate_id.upper())

        if 'full_election' in kwargs.keys():
            # full_election is replaced by election_full.
            raise exceptions.ApiError(
                exceptions.FULL_ELECTION_ERROR,
                status_code=400,
            )

        if kwargs.get('election_full') is None:
            # not pass election_full
            if kwargs.get('cycle'):
                # only filter by cycle
                query = query.filter(totals_class.cycle.in_(kwargs['cycle']))
        else:
            # pass election_full (true or false)
            query = query.filter(
                totals_class.election_full == kwargs['election_full'])
            if kwargs.get('cycle'):
                if kwargs.get('election_full'):
                    # if election_full = true, filter by candidate_election_year = cycle
                    query = query.filter(
                        totals_class.candidate_election_year.in_(
                            kwargs['cycle']))
                else:
                    # if election_full = false, filter by cycle = cycle
                    query = query.filter(
                        totals_class.cycle.in_(kwargs['cycle']))
        return query, totals_class, totals_schema

    def _resolve_committee_type(self, candidate_id=None, **kwargs):
        if candidate_id is not None:
            return candidate_id[0]
예제 #5
0
파일: totals.py 프로젝트: mbokinala/openFEC
class TotalsByCommitteeTypeView(utils.Resource):
    @use_kwargs(args.paging)
    @use_kwargs(args.totals_by_committee_type)
    @use_kwargs(args.make_sort_args(default='-cycle'))
    @marshal_with(schemas.CommitteeTotalsPageSchema(), apply=False)
    def get(self, committee_id=None, committee_type=None, **kwargs):
        query, totals_class, totals_schema = self.build_query(
            committee_id=committee_id, committee_type=committee_type, **kwargs
        )
        page = utils.fetch_page(query, kwargs, model=totals_class)
        return totals_schema().dump(page).data

    def build_query(self, committee_id=None, committee_type=None, **kwargs):
        totals_class, totals_schema = totals_schema_map.get(
            committee_type_map.get(committee_type), 
            default_schemas,
        )
        query = totals_class.query
        if committee_id is not None:
            query = query.filter(totals_class.committee_id == committee_id)
        if kwargs.get('cycle'):
            query = query.filter(totals_class.cycle.in_(kwargs['cycle']))
        if kwargs.get('committee_designation'):
            query = query.filter(totals_class.committee_designation.in_(kwargs['committee_designation']))
        if committee_type == 'pac':
            query = query.filter(
                models.CommitteeTotalsPacParty.committee_type.in_(pac_cmte_list)
            )
        if committee_type == 'party':
            query = query.filter(
                models.CommitteeTotalsPacParty.committee_type.in_(party_cmte_list)
            )
        if committee_type == 'pac-party':
            query = query.filter(
                models.CommitteeTotalsPacParty.committee_type.in_(
                    pac_cmte_list.union(party_cmte_list)
                )
            )
        return query, totals_class, totals_schema