예제 #1
0
파일: reports.py 프로젝트: mido3801/openFEC
    def build_query(self, committee_id=None, committee_type=None, **kwargs):
        reports_class, reports_schema = reports_schema_map.get(
            self._resolve_committee_type(committee_id=committee_id,
                                         committee_type=committee_type,
                                         **kwargs),
            default_schemas,
        )
        query = reports_class.query

        filter_multi_fields = [
            ('amendment_indicator',
             models.CommitteeReports.amendment_indicator),
            ('report_type', reports_class.report_type),
            ('year', reports_class.report_year),
            ('cycle', reports_class.cycle),
            ('beginning_image_number', reports_class.beginning_image_number),
        ]
        # Eagerly load committees if applicable
        if hasattr(reports_class, 'committee'):
            query = reports_class.query.options(
                sa.orm.joinedload(reports_class.committee))
        if committee_id is not None:
            query = query.filter_by(committee_id=committee_id)

        query = filters.filter_range(query, kwargs, get_range_filters())
        query = filters.filter_match(query, kwargs, get_match_filters())
        query = filters.filter_multi(query, kwargs, filter_multi_fields)
        return query, reports_class, reports_schema
 def build_query(self, **kwargs):
     history = models.CandidateHistoryWithFuture
     query = db.session.query(
         history.__table__,
         models.CandidateTotal.__table__
     ).join(
         models.CandidateTotal,
         sa.and_(
             history.candidate_id == models.CandidateTotal.candidate_id,
             history.two_year_period == models.CandidateTotal.cycle,
         )
     ).join(
         models.Candidate,
         history.candidate_id == models.Candidate.candidate_id,
     )
     if kwargs.get('q'):
         query = query.join(
             models.CandidateSearch,
             history.candidate_id == models.CandidateSearch.id,
         )
     query = filters.filter_multi(query, kwargs, self.filter_multi_fields(history, models.CandidateTotal))
     query = filters.filter_range(query, kwargs, self.filter_range_fields(models.CandidateTotal))
     query = filters.filter_fulltext(query, kwargs, self.filter_fulltext_fields)
     query = filters.filter_match(query, kwargs, self.filter_match_fields)
     return query
예제 #3
0
파일: reports.py 프로젝트: 18F/openFEC
    def build_query(self, committee_id=None, committee_type=None, **kwargs):
        reports_class, reports_schema = reports_schema_map.get(
            self._resolve_committee_type(
                committee_id=committee_id,
                committee_type=committee_type,
                **kwargs
            ),
            default_schemas,
        )
        query = reports_class.query

        filter_multi_fields = [
            ('amendment_indicator', models.CommitteeReports.amendment_indicator),
            ('report_type', reports_class.report_type),
        ]
        # Eagerly load committees if applicable
        if hasattr(reports_class, 'committee'):
            query = reports_class.query.options(sa.orm.joinedload(reports_class.committee))

        if committee_id is not None:
            query = query.filter_by(committee_id=committee_id)
        if kwargs.get('year'):
            query = query.filter(reports_class.report_year.in_(kwargs['year']))
        if kwargs.get('cycle'):
            query = query.filter(reports_class.cycle.in_(kwargs['cycle']))
        if kwargs.get('beginning_image_number'):
            query = query.filter(reports_class.beginning_image_number.in_(kwargs['beginning_image_number']))
        if kwargs.get('is_amended') is not None:
            query = query.filter(reports_class.is_amended == kwargs['is_amended'])

        query = filters.filter_range(query, kwargs, get_range_filters())
        query = filters.filter_match(query, kwargs, get_match_filters())
        query = filters.filter_multi(query, kwargs, filter_multi_fields)
        return query, reports_class, reports_schema
예제 #4
0
파일: reports.py 프로젝트: mido3801/openFEC
    def build_query(self, committee_type=None, **kwargs):
        #For this endpoint we now enforce the enpoint specified to map the right model.
        reports_class, reports_schema = reports_schema_map.get(
            reports_type_map.get(committee_type),
            default_schemas,
        )
        query = reports_class.query

        filter_multi_fields = [
            ('amendment_indicator',
             models.CommitteeReports.amendment_indicator),
            ('report_type', reports_class.report_type),
            ('committee_id', reports_class.committee_id),
            ('year', reports_class.report_year),
            ('cycle', reports_class.cycle),
            ('beginning_image_number', reports_class.beginning_image_number),
        ]

        if hasattr(reports_class, 'committee'):
            query = reports_class.query.outerjoin(
                reports_class.committee).options(
                    sa.orm.contains_eager(reports_class.committee))

        if kwargs.get('candidate_id'):
            query = query.filter(
                models.CommitteeHistory.candidate_ids.overlap(
                    [kwargs.get('candidate_id')]))
        if kwargs.get('type'):
            query = query.filter(
                models.CommitteeHistory.committee_type.in_(kwargs.get('type')))

        query = filters.filter_range(query, kwargs, get_range_filters())
        query = filters.filter_match(query, kwargs, get_match_filters())
        query = filters.filter_multi(query, kwargs, filter_multi_fields)
        return query, reports_class, reports_schema
예제 #5
0
 def build_query(self, **kwargs):
     if kwargs['election_full']:
         history = models.CandidateHistoryLatest
         year_column = history.cand_election_year
     else:
         history = models.CandidateHistory
         year_column = history.two_year_period
     query = db.session.query(
         history.__table__,
         models.CandidateTotal.__table__,
     ).join(
         models.CandidateTotal,
         sa.and_(
             history.candidate_id == models.CandidateTotal.candidate_id,
             year_column == models.CandidateTotal.cycle,
         )).filter(
             models.CandidateTotal.is_election == kwargs['election_full'], )
     if kwargs.get('q'):
         query = query.join(
             models.CandidateSearch,
             history.candidate_id == models.CandidateSearch.id,
         )
     query = filters.filter_multi(
         query, kwargs,
         self.filter_multi_fields(history, models.CandidateTotal))
     query = filters.filter_range(
         query, kwargs, self.filter_range_fields(models.CandidateTotal))
     query = filters.filter_fulltext(query, kwargs,
                                     self.filter_fulltext_fields)
     return query
예제 #6
0
파일: reports.py 프로젝트: 18F/openFEC
    def build_query(self, committee_type=None, **kwargs):
        #For this endpoint we now enforce the enpoint specified to map the right model.
        reports_class, reports_schema = reports_schema_map.get(
            reports_type_map.get(committee_type),
            default_schemas,
        )
        query = reports_class.query

        filter_multi_fields = [
            ('amendment_indicator', models.CommitteeReports.amendment_indicator),
            ('report_type', reports_class.report_type),
        ]

        if hasattr(reports_class, 'committee'):
            query = reports_class.query.outerjoin(reports_class.committee).options(sa.orm.contains_eager(reports_class.committee))

        if kwargs.get('committee_id'):
            query = query.filter(reports_class.committee_id.in_(kwargs['committee_id']))
        if kwargs.get('candidate_id'):
            query = query.filter(models.CommitteeHistory.candidate_ids.overlap([kwargs.get('candidate_id')]))
        if kwargs.get('type'):
            query = query.filter(models.CommitteeHistory.committee_type.in_(kwargs.get('type')))
        if kwargs.get('year'):
            query = query.filter(reports_class.report_year.in_(kwargs['year']))
        if kwargs.get('cycle'):
            query = query.filter(reports_class.cycle.in_(kwargs['cycle']))
        if kwargs.get('beginning_image_number'):
            query = query.filter(reports_class.beginning_image_number.in_(kwargs['beginning_image_number']))
        if kwargs.get('is_amended') is not None:
            query = query.filter(reports_class.is_amended == kwargs['is_amended'])

        query = filters.filter_range(query, kwargs, get_range_filters())
        query = filters.filter_match(query, kwargs, get_match_filters())
        query = filters.filter_multi(query, kwargs, filter_multi_fields)
        return query, reports_class, reports_schema
예제 #7
0
 def build_query(self, _apply_options=True, **kwargs):
     query = self.model.query
     query = filters.filter_match(query, kwargs, self.filter_match_fields)
     query = filters.filter_multi(query, kwargs, self.filter_multi_fields)
     query = filters.filter_range(query, kwargs, self.filter_range_fields)
     if _apply_options:
         query = query.options(*self.query_options)
     return query
예제 #8
0
 def build_query(self, _apply_options=True, **kwargs):
     query = self.model.query
     query = filters.filter_match(query, kwargs, self.filter_match_fields)
     query = filters.filter_multi(query, kwargs, self.filter_multi_fields)
     query = filters.filter_range(query, kwargs, self.filter_range_fields)
     if _apply_options:
         query = query.options(*self.query_options)
     return query
예제 #9
0
파일: views.py 프로젝트: mbland/openFEC
    def build_query(self, kwargs):
        query = self.model.query.filter(
            self.year_column >= SQL_CONFIG['START_YEAR_ITEMIZED'],
        )

        query = filters.filter_multi(query, kwargs, self.filter_multi_fields)
        query = filters.filter_match(query, kwargs, self.filter_match_fields)
        query = filters.filter_range(query, kwargs, self.filter_range_fields)
        query = self.filter_fulltext(query, kwargs)

        return query
예제 #10
0
 def build_query(self, **kwargs):
     if kwargs['election_full']:
         history = models.CandidateHistoryLatest
         year_column = history.candidate_election_year
     else:
         history = models.CandidateHistory
         year_column = history.two_year_period
     query = db.session.query(
         history.__table__, models.CandidateTotal.__table__,
         models.CandidateFlags.__table__
     ).join(
         models.CandidateTotal,
         sa.and_(
             history.candidate_id == models.CandidateTotal.candidate_id,
             year_column == models.CandidateTotal.cycle,
             history.candidate_election_year == models.CandidateTotal.cycle,
         )).join(
             models.Candidate,
             history.candidate_id == models.Candidate.candidate_id,
         ).join(
             models.CandidateFlags,
             history.candidate_id == models.CandidateFlags.candidate_id,
         ).filter(
             models.CandidateTotal.is_election == kwargs['election_full'], )
     if kwargs.get('q'):
         query = query.join(
             models.CandidateSearch,
             history.candidate_id == models.CandidateSearch.id,
         )
     #The .filter methods may be able to moved to the filters methods, will investigate
     if kwargs.get('has_raised_funds'):
         query = query.filter(
             models.Candidate.flags.has(
                 models.CandidateFlags.has_raised_funds ==
                 kwargs['has_raised_funds']))
     if kwargs.get('federal_funds_flag'):
         query = query.filter(
             models.Candidate.flags.has(
                 models.CandidateFlags.federal_funds_flag ==
                 kwargs['federal_funds_flag']))
     query = filters.filter_multi(
         query, kwargs,
         self.filter_multi_fields(history, models.CandidateTotal))
     query = filters.filter_range(
         query, kwargs, self.filter_range_fields(models.CandidateTotal))
     query = filters.filter_fulltext(query, kwargs,
                                     self.filter_fulltext_fields)
     return query
예제 #11
0
 def build_query(self, cycle, **kwargs):
     totals = sa.Table('ofec_totals_combined_mv', db.metadata, autoload_with=db.engine)
     query = models.CommitteeHistory.query.with_entities(
         models.CommitteeHistory.__table__,
         totals,
     ).outerjoin(
         totals,
         sa.and_(
             models.CommitteeHistory.committee_id == totals.c.committee_id,
             models.CommitteeHistory.cycle == totals.c.cycle,
         )
     ).filter(
         models.CommitteeHistory.cycle == cycle,
     )
     query = filters.filter_multi(query, kwargs, self.filter_multi_fields(models.CommitteeHistory))
     query = filters.filter_range(query, kwargs, self.filter_range_fields(totals.c))
     return query
예제 #12
0
    def build_query(self, **kwargs):
        if kwargs['election_full']:
            history = models.CandidateHistoryLatest
            year_column = history.cand_election_year
        else:
            history = models.CandidateHistory
            year_column = history.two_year_period
        query = db.session.query(
            history.__table__,
            models.CandidateTotal.__table__,
            models.CandidateFlags.__table__
        ).join(
            models.CandidateTotal,
            sa.and_(
                history.candidate_id == models.CandidateTotal.candidate_id,
                year_column == models.CandidateTotal.cycle,
            )
        ).join(
            models.Candidate,
            history.candidate_id == models.Candidate.candidate_id,
        ).join(
            models.CandidateFlags,
            history.candidate_id == models.CandidateFlags.candidate_id,
        ).filter(
            models.CandidateTotal.is_election == kwargs['election_full'],
        )
        if kwargs.get('q'):
            query = query.join(
                models.CandidateSearch,
                history.candidate_id == models.CandidateSearch.id,
            )
        #The .filter methods may be able to moved to the filters methods, will investigate

        if kwargs.get('has_raised_funds'):
            query = query.filter(
                models.Candidate.flags.has(models.CandidateFlags.has_raised_funds == kwargs['has_raised_funds'])
            )
        if kwargs.get('federal_funds_flag'):
            query = query.filter(
                models.Candidate.flags.has(models.CandidateFlags.federal_funds_flag == kwargs['federal_funds_flag'])
            )
        query = filters.filter_multi(query, kwargs, self.filter_multi_fields(history, models.CandidateTotal))
        query = filters.filter_range(query, kwargs, self.filter_range_fields(models.CandidateTotal))
        query = filters.filter_fulltext(query, kwargs, self.filter_fulltext_fields)
        return query
예제 #13
0
파일: reports.py 프로젝트: anyshu/openFEC-1
    def build_query(self, committee_type=None, **kwargs):
        #For this endpoint we now enforce the enpoint specified to map the right model.
        reports_class, reports_schema = reports_schema_map.get(
            reports_type_map.get(committee_type),
            default_schemas,
        )
        query = reports_class.query
        if hasattr(reports_class, 'committee'):
            query = reports_class.query.outerjoin(
                reports_class.committee).options(
                    sa.orm.contains_eager(reports_class.committee))

        if kwargs.get('committee_id'):
            query = query.filter(
                reports_class.committee_id.in_(kwargs['committee_id']))
        if kwargs.get('candidate_id'):
            query = query.filter(
                models.CommitteeHistory.candidate_ids.overlap(
                    [kwargs.get('candidate_id')]))
        if kwargs.get('type'):
            query = query.filter(
                models.CommitteeHistory.committee_type.in_(kwargs.get('type')))
        if kwargs.get('year'):
            query = query.filter(reports_class.report_year.in_(kwargs['year']))
        if kwargs.get('cycle'):
            query = query.filter(reports_class.cycle.in_(kwargs['cycle']))
        if kwargs.get('beginning_image_number'):
            query = query.filter(
                reports_class.beginning_image_number.in_(
                    kwargs['beginning_image_number']))
        if kwargs.get('report_type'):
            include, exclude = parse_types(kwargs['report_type'])
            if include:
                query = query.filter(reports_class.report_type.in_(include))
            elif exclude:
                query = query.filter(
                    sa.not_(reports_class.report_type.in_(exclude)))
        if kwargs.get('is_amended') is not None:
            query = query.filter(
                reports_class.is_amended == kwargs['is_amended'])

        query = filters.filter_range(query, kwargs, get_range_filters())
        query = filters.filter_match(query, kwargs, get_match_filters())
        query = filters.filter_multi(query, kwargs, get_multi_filters())
        return query, reports_class, reports_schema
예제 #14
0
파일: reports.py 프로젝트: anyshu/openFEC-1
    def build_query(self, committee_id=None, committee_type=None, **kwargs):
        reports_class, reports_schema = reports_schema_map.get(
            self._resolve_committee_type(committee_id=committee_id,
                                         committee_type=committee_type,
                                         **kwargs),
            default_schemas,
        )
        query = reports_class.query
        # Eagerly load committees if applicable

        if hasattr(reports_class, 'committee'):
            query = reports_class.query.options(
                sa.orm.joinedload(reports_class.committee))

        if committee_id is not None:
            query = query.filter_by(committee_id=committee_id)
        if kwargs.get('year'):
            query = query.filter(reports_class.report_year.in_(kwargs['year']))
        if kwargs.get('cycle'):
            query = query.filter(reports_class.cycle.in_(kwargs['cycle']))
        if kwargs.get('beginning_image_number'):
            query = query.filter(
                reports_class.beginning_image_number.in_(
                    kwargs['beginning_image_number']))
        if kwargs.get('report_type'):
            include, exclude = parse_types(kwargs['report_type'])
            if include:
                query = query.filter(reports_class.report_type.in_(include))
            elif exclude:
                query = query.filter(
                    sa.not_(reports_class.report_type.in_(exclude)))

        if kwargs.get('is_amended') is not None:
            query = query.filter(
                reports_class.is_amended == kwargs['is_amended'])

        query = filters.filter_range(query, kwargs, get_range_filters())
        query = filters.filter_match(query, kwargs, get_match_filters())
        query = filters.filter_multi(query, kwargs, get_multi_filters())
        return query, reports_class, reports_schema
예제 #15
0
    def build_query(self, **kwargs):
        history = models.CandidateHistoryWithFuture
        query = db.session.query(
            history.__table__, models.CandidateTotal.__table__).join(
                models.CandidateTotal,
                sa.and_(
                    history.candidate_id == models.CandidateTotal.candidate_id,
                    history.two_year_period == models.CandidateTotal.cycle,
                )).join(
                    models.Candidate,
                    history.candidate_id == models.Candidate.candidate_id,
                )
        if kwargs.get('q'):
            query = query.join(
                models.CandidateSearch,
                history.candidate_id == models.CandidateSearch.id,
            )

        if 'is_active_candidate' in kwargs and kwargs.get(
                'is_active_candidate'):  # load active candidates only if True
            query = query.filter(history.candidate_inactive == False  # noqa
                                 )
        elif 'is_active_candidate' in kwargs and not kwargs.get(
                'is_active_candidate'
        ):  # load inactive candidates only if False
            query = query.filter(history.candidate_inactive == True  # noqa
                                 )
        else:  # load all candidates
            pass

        query = filters.filter_multi(
            query, kwargs,
            self.filter_multi_fields(history, models.CandidateTotal))
        query = filters.filter_range(
            query, kwargs, self.filter_range_fields(models.CandidateTotal))
        query = filters.filter_fulltext(query, kwargs,
                                        self.filter_fulltext_fields)
        query = filters.filter_match(query, kwargs, self.filter_match_fields)
        return query
예제 #16
0
 def _build_query(self, **kwargs):
     query = models.Filings.query
     query = query.options(sa.orm.joinedload(models.Filings.committee))
     query = filters.filter_multi(query, kwargs, self.multi_fields)
     query = filters.filter_range(query, kwargs, self.range_fields)
     return query