Exemplo n.º 1
0
    def test_filter_multi_combo(self):
        # Exclude/include multiple integer values
        query_dates = filters.filter_multi(
            models.CalendarDate.query,
            {'calendar_category_id': [-1, 3]},
            CalendarDatesView.filter_multi_fields,
        )
        self.assertEqual(
            set(query_dates.all()),
            set(each for each in self.dates
                if each.calendar_category_id not in [1]
                and each.calendar_category_id in [3]),
        )

        # Exclude/include multiple string values
        query_committees = filters.filter_multi(
            models.Committee.query,
            {'designation': ['-P', 'U']},
            CommitteeList.filter_multi_fields,
        )
        self.assertEqual(
            set(query_committees.all()),
            set(each for each in self.committees if
                each.designation not in ['P'] and each.designation in ['U']),
        )
Exemplo n.º 2
0
 def _get_elections(self, kwargs):
     """Get elections from candidate history records."""
     query = CandidateHistory.query.distinct(
         CandidateHistory.state,
         CandidateHistory.office,
         CandidateHistory.district,
         CandidateHistory.two_year_period,
     ).filter(
         CandidateHistory.candidate_inactive == None,  # noqa
         # TODO(jmcarp) Revert after #1271 is resolved
         sa.or_(
             CandidateHistory.district == None,  # noqa
             CandidateHistory.district != '99',
         )
     )
     if kwargs.get('cycle'):
         query = query.filter(CandidateHistory.election_years.contains(kwargs['cycle']))
     if kwargs.get('office'):
         values = [each[0].upper() for each in kwargs['office']]
         query = query.filter(CandidateHistory.office.in_(values))
     if kwargs.get('state'):
         query = query.filter(CandidateHistory.state.in_(kwargs['state'] + ['US']))
     if kwargs.get('district'):
         query = query.filter(
             sa.or_(
                 CandidateHistory.district.in_(kwargs['district']),
                 CandidateHistory.district == None  # noqa
             ),
         )
     if kwargs.get('zip'):
         query = self._filter_zip(query, kwargs)
     return filters.filter_multi(query, kwargs, self.filter_multi_fields)
Exemplo n.º 3
0
    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
Exemplo n.º 4
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
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
 def _get_elections(self, kwargs):
     """Get elections from candidate history records."""
     query = CandidateHistory.query.distinct(
         CandidateHistory.state,
         CandidateHistory.office,
         CandidateHistory.district,
         CandidateHistory.two_year_period,
     ).filter(
         CandidateHistory.candidate_status == 'C',
     )
     if kwargs['cycle']:
         query = query.filter(CandidateHistory.election_years.contains(kwargs['cycle']))
     if kwargs['office']:
         values = [each[0].upper() for each in kwargs['office']]
         query = query.filter(CandidateHistory.office.in_(values))
     if kwargs['state']:
         query = query.filter(CandidateHistory.state.in_(kwargs['state'] + ['US']))
     if kwargs['district']:
         query = query.filter(
             sa.or_(
                 CandidateHistory.district.in_(kwargs['district']),
                 CandidateHistory.district == None  # noqa
             ),
         )
     if kwargs['zip']:
         query = self._filter_zip(query, kwargs)
     return filters.filter_multi(query, kwargs, self.filter_multi_fields)
Exemplo n.º 9
0
 def _get_elections(self, kwargs):
     """Get elections from candidate history records."""
     query = CandidateHistory.query.distinct(
         CandidateHistory.state,
         CandidateHistory.office,
         CandidateHistory.district,
         CandidateHistory.two_year_period,
     ).filter(
         CandidateHistory.candidate_inactive == False,  # noqa
         # TODO(jmcarp) Revert after #1271 is resolved
         sa.or_(
             CandidateHistory.district == None,  # noqa
             CandidateHistory.district != '99',
         ))
     if kwargs.get('cycle'):
         query = query.filter(
             CandidateHistory.election_years.contains(kwargs['cycle']))
     if kwargs.get('office'):
         values = [each[0].upper() for each in kwargs['office']]
         query = query.filter(CandidateHistory.office.in_(values))
     if kwargs.get('state'):
         query = query.filter(
             sa.or_(
                 CandidateHistory.state.in_(kwargs['state']),
                 CandidateHistory.office == 'P',
             ))
     if kwargs.get('district'):
         query = query.filter(
             sa.or_(
                 CandidateHistory.district.in_(kwargs['district']),
                 CandidateHistory.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 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
Exemplo n.º 11
0
 def _get_elections(self, kwargs):
     """Get elections from candidate history records."""
     query = CandidateHistory.query.distinct(
         CandidateHistory.state,
         CandidateHistory.office,
         CandidateHistory.district,
         CandidateHistory.two_year_period,
     ).filter(CandidateHistory.candidate_status == 'C', )
     if kwargs['cycle']:
         query = query.filter(
             CandidateHistory.election_years.contains(kwargs['cycle']))
     if kwargs['office']:
         values = [each[0].upper() for each in kwargs['office']]
         query = query.filter(CandidateHistory.office.in_(values))
     if kwargs['state']:
         query = query.filter(
             CandidateHistory.state.in_(kwargs['state'] + ['US']))
     if kwargs['district']:
         query = query.filter(
             sa.or_(
                 CandidateHistory.district.in_(kwargs['district']),
                 CandidateHistory.district == None  # noqa
             ), )
     if kwargs['zip']:
         query = self._filter_zip(query, kwargs)
     return filters.filter_multi(query, kwargs, self.filter_multi_fields)
Exemplo n.º 12
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
Exemplo n.º 13
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
Exemplo n.º 14
0
 def _build_query(self, committee_id, kwargs):
     query = self.model.query
     if committee_id is not None:
         query = query.filter(self.model.committee_id == committee_id)
     query = filters.filter_match(query, kwargs, self.match_fields)
     query = filters.filter_multi(query, kwargs, self.fields)
     for join in self.joins:
         query = query.options(sa.orm.joinedload(join))
     return query
Exemplo n.º 15
0
 def _build_query(self, committee_id, kwargs):
     query = self.model.query
     if committee_id is not None:
         query = query.filter(self.model.committee_id == committee_id)
     query = filters.filter_match(query, kwargs, self.match_fields)
     query = filters.filter_multi(query, kwargs, self.fields)
     for join in self.joins:
         query = query.options(sa.orm.joinedload(join))
     return query
Exemplo n.º 16
0
 def test_filter_multi_combo(self):
     # Exclude/include multiple integer values
     query_dates = filters.filter_multi(
         models.CalendarDate.query,
         {'calendar_category_id': [-1, 3]},
         CalendarDatesView.filter_multi_fields
     )
     self.assertEqual(
         set(query_dates.all()),
         set(each for each in self.dates if each.calendar_category_id not in [1] and each.calendar_category_id in [3]))
     # Exclude/include multiple string values
     query_committees = filters.filter_multi(
         models.Committee.query,
         {'designation': ['-P', 'U']},
         CommitteeList.filter_multi_fields
     )
     self.assertEqual(
         set(query_committees.all()),
         set(each for each in self.committees if each.designation not in ['P'] and each.designation in ['U']))
Exemplo n.º 17
0
 def test_filter_multi(self):
     # Filter for multiple integer values
     query_dates = filters.filter_multi(
         models.CalendarDate.query,
         {'calendar_category_id': [1, 3]},
         CalendarDatesView.filter_multi_fields
     )
     self.assertEqual(
         set(query_dates.all()),
         set(each for each in self.dates if each.calendar_category_id in [1,3]))
     # Filter for multiple string values
     query_committees = filters.filter_multi(
         models.Committee.query,
         {'designation': ['P', 'U']},
         CommitteeList.filter_multi_fields
     )
     self.assertEqual(
         set(query_committees.all()),
         set(each for each in self.committees if each.designation in ['P','U']))
Exemplo n.º 18
0
    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
Exemplo n.º 19
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
Exemplo n.º 20
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
Exemplo n.º 21
0
    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
Exemplo n.º 22
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
Exemplo n.º 23
0
 def _get_elections(self, kwargs):
     """Get elections from candidate history records."""
     query = CandidateHistory.query.distinct(
         CandidateHistory.state, CandidateHistory.office, CandidateHistory.district, CandidateHistory.two_year_period
     ).filter(
         CandidateHistory.candidate_status == "C", CandidateHistory.candidate_inactive == None  # noqa
     )
     if kwargs.get("cycle"):
         query = query.filter(CandidateHistory.election_years.contains(kwargs["cycle"]))
     if kwargs.get("office"):
         values = [each[0].upper() for each in kwargs["office"]]
         query = query.filter(CandidateHistory.office.in_(values))
     if kwargs.get("state"):
         query = query.filter(CandidateHistory.state.in_(kwargs["state"] + ["US"]))
     if kwargs.get("district"):
         query = query.filter(
             sa.or_(CandidateHistory.district.in_(kwargs["district"]), CandidateHistory.district == None)  # noqa
         )
     if kwargs.get("zip"):
         query = self._filter_zip(query, kwargs)
     return filters.filter_multi(query, kwargs, self.filter_multi_fields)
Exemplo n.º 24
0
    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
Exemplo n.º 25
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
Exemplo n.º 26
0
    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)
Exemplo n.º 27
0
    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)
Exemplo n.º 28
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