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
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
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
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
def test_filter_match_exclude(self): # Exclude a single integer value query_dates = filters.filter_match( models.CalendarDate.query, {'event_id': -321}, CalendarDatesView.filter_match_fields ) self.assertEqual( set(query_dates.all()), set(each for each in self.dates if each.event_id != 321)) # Exclude a single string value query_reports = filters.filter_match( models.CommitteeReportsHouseSenate.query, {'filer_type': '-paper'}, get_match_filters() ) self.assertEqual( set(query_reports.all()), set(each for each in self.reports if each.means_filed != 'paper'))
def test_filter_match(self): # Filter for a single integer value query_dates = filters.filter_match( models.CalendarDate.query, {'event_id': 123}, CalendarDatesView.filter_match_fields ) self.assertEqual( set(query_dates.all()), set(each for each in self.dates if each.event_id == 123)) # Filter for a single string value query_reports = filters.filter_match( models.CommitteeReportsHouseSenate.query, {'filer_type': 'e-file'}, get_match_filters() ) self.assertEqual( set(query_reports.all()), set(each for each in self.reports if each.means_filed == 'e-file'))
def build_query(self, *args, _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) query = filters.filter_fulltext(query, kwargs, self.filter_fulltext_fields) query = filters.filter_overlap(query, kwargs, self.filter_overlap_fields) if _apply_options: query = query.options(*self.query_options) return query
def test_filter_match(self): # Filter for a single integer value query_dates = filters.filter_match( models.CalendarDate.query, {'event_id': 123}, CalendarDatesView.filter_match_fields, ) self.assertEqual( set(query_dates.all()), set(each for each in self.dates if each.event_id == 123), ) # Filter for a single string value query_reports = filters.filter_match( models.CommitteeReportsHouseSenate.query, {'filer_type': 'e-file'}, get_match_filters(), ) self.assertEqual( set(query_reports.all()), set(each for each in self.reports if each.means_filed == 'e-file'), )
def test_filter_match_exclude(self): # Exclude a single integer value query_dates = filters.filter_match( models.CalendarDate.query, {'event_id': -321}, CalendarDatesView.filter_match_fields, ) self.assertEqual( set(query_dates.all()), set(each for each in self.dates if each.event_id != 321 or each.event_id is None), ) # Exclude a single string value query_reports = filters.filter_match( models.CommitteeReportsHouseSenate.query, {'filer_type': '-paper'}, get_match_filters(), ) self.assertEqual( set(query_reports.all()), set(each for each in self.reports if each.means_filed != 'paper'), )
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
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
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
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