def sort_args(self): return args.make_sort_args( validator=args.IndexValidator( self.model, extra=['candidate', 'committee'], ), )
def args(self): return utils.extend( args.paging, args.calendar_dates, args.make_sort_args( default='-start_date', ), )
def args(self): return utils.extend( args.paging, args.rad_analyst, args.make_sort_args( validator=args.IndexValidator(models.RadAnalyst), ), )
def args(self): return utils.extend( args.electioneering, args.make_seek_args(), args.make_sort_args( validator=args.IndexValidator(models.Electioneering), ), )
def args(self): return utils.extend( args.paging, args.Category, args.make_sort_args( default='primary_category_name', ), )
def args(self): return utils.extend( args.paging, args.committee_history, args.make_sort_args( default='-cycle', validator=args.IndexValidator(self.model), ), )
def args(self): return utils.extend( args.itemized, args.schedule_c, args.paging, args.make_sort_args( default='incurred_date', ), )
def args(self): return utils.extend( args.paging, args.candidate_detail, args.make_sort_args( default='name', validator=args.IndexValidator(self.model), ), )
def args(self): return utils.extend( args.itemized, args.schedule_b, args.make_seek_args(), args.make_sort_args( validator=args.OptionValidator(['disbursement_date', 'disbursement_amount']), ), )
def args(self): return utils.extend( args.paging, args.candidate_history, args.make_sort_args( default='-two_year_period', validator=args.IndexValidator(self.model), ), )
def args(self): return utils.extend( args.paging, args.schedule_b_efile, args.make_sort_args( default='-disbursement_date', validator=args.OptionValidator(['disbursement_date', 'disbursement_amount']), ), )
def args(self): return utils.extend( args.itemized, args.communication_cost, args.make_seek_args(), args.make_sort_args( validator=args.IndexValidator(models.CommunicationCost), ), )
def args(self): return utils.extend( args.paging, args.efilings, args.make_sort_args( default='-receipt_date', # validator=args.IndexValidator(self.model), ), )
def args(self): return utils.extend( args.paging, args.election_dates, args.make_sort_args( default='-election_date', validator=args.IndexValidator(self.model), ), )
def args(self): return utils.extend( args.itemized, args.schedule_f, args.paging, args.make_sort_args( default='expenditure_date', ) )
def args(self): return utils.extend( args.paging, args.schedule_b_efile, args.make_sort_args( default='-disbursement_date', validator=args.OptionValidator( ['disbursement_date', 'disbursement_amount']), ), )
class ScheduleABySizeCandidateView(Resource): @args.register_kwargs(args.paging) @args.register_kwargs(args.make_sort_args()) @args.register_kwargs(args.schedule_a_candidate_aggregate) @schemas.marshal_with(schemas.ScheduleABySizeCandidatePageSchema()) def get(self, **kwargs): group_columns = [ScheduleABySize.size] query = candidate_aggregate(ScheduleABySize, group_columns, group_columns, kwargs) return utils.fetch_page(query, kwargs, cap=None)
def args(self): return utils.extend( args.paging, args.efilings, args.make_sort_args( default='-receipt_date', validator=args.IndexValidator(self.model), ), )
def args(self): return utils.extend( args.paging, args.candidate_list, args.candidate_detail, args.make_sort_args( default='name', validator=args.IndexValidator( models.Candidate, extra=list(self.aliases.keys()), ), ))
def args(self): return utils.extend( args.itemized, args.schedule_e, args.make_seek_args(), args.make_sort_args( default='-expenditure_date', validator=args.OptionValidator(self.sort_options), ), )
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]
class CommitteeList(Resource): @args.register_kwargs(args.paging) @args.register_kwargs(args.committee) @args.register_kwargs(args.committee_list) @args.register_kwargs( args.make_sort_args( default=['name'], validator=args.IndexValidator(models.Committee), )) @schemas.marshal_with(schemas.CommitteePageSchema()) def get(self, **kwargs): query = self.get_committees(kwargs) return utils.fetch_page(query, kwargs, model=models.Committee) def get_committees(self, kwargs): committees = models.Committee.query if kwargs['candidate_id']: committees = committees.filter( models.Committee.candidate_ids.overlap(kwargs['candidate_id'])) if kwargs.get('q'): committees = utils.search_text( committees.join( models.CommitteeSearch, models.Committee.committee_id == models.CommitteeSearch.id, ), models.CommitteeSearch.fulltxt, kwargs['q'], ) if kwargs.get('name'): committees = committees.filter( models.Committee.name.ilike('%{}%'.format(kwargs['name']))) committees = filter_query(models.Committee, committees, list_filter_fields, kwargs) if kwargs['year']: committees = filter_year(models.Committee, committees, kwargs['year']) if kwargs['cycle']: committees = committees.filter( models.Committee.cycles.overlap(kwargs['cycle'])) if kwargs['min_first_file_date']: committees = committees.filter(models.Committee.first_file_date >= kwargs['min_first_file_date']) if kwargs['max_first_file_date']: committees = committees.filter(models.Committee.first_file_date <= kwargs['max_first_file_date']) return committees
def args(self): return utils.extend( args.itemized, args.schedule_e, args.make_seek_args(), args.make_sort_args(validator=args.OptionValidator([ 'expenditure_date', 'expenditure_amount', 'office_total_ytd', ]), ), )
def args(self): return utils.extend( args.itemized, args.schedule_a, args.make_seek_args(), args.make_sort_args( default='contribution_receipt_date', validator=args.OptionValidator(self.sort_options), show_nulls_last_arg=False, ), )
def args(self): return utils.extend( args.itemized, args.schedule_b, args.make_seek_args(), args.make_sort_args( default='-disbursement_date', validator=args.OptionValidator( ['disbursement_date', 'disbursement_amount']), ), )
def args(self): return utils.extend( args.schedule_a_by_state_recipient_totals, args.paging, args.make_sort_args( default='cycle', validator=args.OptionValidator( ['cycle', 'state', 'committee_type', 'total'] ), ), )
def args(self): return utils.extend( args.itemized, args.schedule_a, args.make_seek_args(), args.make_sort_args( default='contribution_receipt_date', validator=args.OptionValidator([ 'contribution_receipt_date', 'contribution_receipt_amount', 'contributor_aggregate_ytd', ]), ))
def args(self): return utils.extend( args.paging, args.reporting_dates, args.make_sort_args( default='-due_date', validator=args.IndexValidator( self.model, schema=models.ReportDate.__table_args__.get('schema')), ), )
def args(self): return utils.extend( args.paging, args.election_dates, args.make_sort_args( default='-election_date', validator=args.IndexValidator( self.model, schema=models.ElectionDate.__table_args__.get('schema')), ), )
def args(self): return utils.extend( args.schedule_c, args.paging, args.make_seek_args(), args.make_sort_args( default='-incurred_date', validator=args.OptionValidator([ 'incurred_date', 'payment_to_date', 'original_loan_amount', ]), default_sort_nulls_last=True, ))
class ScheduleBView(ItemizedResource): model = models.ScheduleB @property def year_column(self): return self.model.report_year @property def index_column(self): return self.model.sched_b_sk filter_multi_fields = [ ('image_number', models.ScheduleB.image_number), ('committee_id', models.ScheduleB.committee_id), ('recipient_city', models.ScheduleB.recipient_city), ('recipient_state', models.ScheduleB.recipient_state), ('recipient_committee_id', models.ScheduleB.recipient_committee_id), ] filter_fulltext_fields = [ ('recipient_name', models.ScheduleBSearch.recipient_name_text), ('disbursement_description', models.ScheduleBSearch.disbursement_description_text), ] filter_range_fields = [ (('min_date', 'max_date'), models.ScheduleB.disbursement_date), (('min_amount', 'max_amount'), models.ScheduleB.disbursement_amount), (('min_image_number', 'max_image_number'), models.ScheduleB.image_number), ] @args.register_kwargs(args.itemized) @args.register_kwargs(args.schedule_b) @args.register_kwargs(args.make_seek_args()) @args.register_kwargs( args.make_sort_args( validator=args.OptionValidator(['disbursement_date', 'disbursement_amount']), multiple=False, ) ) @schemas.marshal_with(schemas.ScheduleBPageSchema()) def get(self, **kwargs): return super(ScheduleBView, self).get(**kwargs) def build_query(self, kwargs): query = super(ScheduleBView, self).build_query(kwargs) query = query.options(sa.orm.joinedload(models.ScheduleB.committee)) query = query.options(sa.orm.joinedload(models.ScheduleB.recipient_committee)) return query def join_fulltext(self, query): return query.join( models.ScheduleBSearch, models.ScheduleB.sched_b_sk == models.ScheduleBSearch.sched_b_sk, )
class TotalsView(utils.Resource): @use_kwargs(args.paging) @use_kwargs(args.totals_all) @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( self._resolve_committee_type(committee_id=committee_id, 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) if kwargs.get('cycle'): query = query.filter(totals_class.cycle.in_(kwargs['cycle'])) 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 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 elif committee_type is not None: return committee_type_map.get(committee_type)
def args(self): return utils.extend( args.itemized, args.schedule_a, args.make_seek_args(), args.make_sort_args( validator=args.OptionValidator([ 'contribution_receipt_date', 'contribution_receipt_amount', 'contributor_aggregate_ytd', ]), ) )
def args(self): return utils.extend( args.paging, args.schedule_e_efile, args.make_sort_args( default='-expenditure_date', validator=args.OptionValidator([ 'expenditure_date', 'expenditure_amount', 'office_total_ytd', ]), ), )
def args(self): return utils.extend( args.itemized, args.schedule_e, args.make_seek_args(), args.make_sort_args( validator=args.OptionValidator([ 'expenditure_date', 'expenditure_amount', 'office_total_ytd', ]), ), )
def args(self): return utils.extend( args.itemized, args.schedule_e, args.make_seek_args(), args.make_sort_args( default='-expenditure_date', validator=args.OptionValidator([ 'expenditure_date', 'expenditure_amount', 'office_total_ytd', 'support_oppose_indicator' ]), ), )
def args(self): return utils.extend( args.paging, args.candidate_list, args.candidate_detail, args.make_sort_args( default='name', validator=args.IndexValidator( models.Candidate, extra=list(self.aliases.keys()), ), ) )
class ScheduleABySizeCandidateView(utils.Resource): @use_kwargs(args.paging) @use_kwargs(args.make_sort_args()) @use_kwargs(args.schedule_a_candidate_aggregate) @marshal_with(schemas.ScheduleABySizeCandidatePageSchema()) def get(self, **kwargs): label_columns = [ ScheduleABySize.size, sa.func.sum(ScheduleABySize.total).label('total'), ] group_columns = [ScheduleABySize.size] _, query = candidate_aggregate(ScheduleABySize, label_columns, group_columns, kwargs) return utils.fetch_page(query, kwargs, cap=None)
def args(self): return utils.extend( args.paging, args.schedule_a_e_file, args.itemized, args.make_sort_args( default='-contribution_receipt_date', validator=args.OptionValidator([ 'contribution_receipt_date', 'contribution_receipt_amount', 'contributor_aggregate_ytd', ]), ), )
class FilingsView(Resource): @args.register_kwargs(args.paging) @args.register_kwargs( args.make_sort_args( default=['-receipt_date'], validator=args.IndexValidator(models.Filings), ) ) @schemas.marshal_with(schemas.FilingsPageSchema()) def get(self, committee_id=None, **kwargs): query = models.Filings.query query = query.filter_by(committee_id=committee_id) count = counts.count_estimate(query, models.db.session, threshold=5000) return utils.fetch_page(query, kwargs, model=models.Filings, count=count)
class CandidateSearch(CandidateList): @property def query(self): # Eagerly load principal committees to avoid extra queries return models.Candidate.query.options( sa.orm.subqueryload(models.Candidate.principal_committees)) @use_kwargs(args.paging) @use_kwargs(args.candidate_list) @use_kwargs(args.candidate_detail) @use_kwargs( args.make_sort_args(validator=args.IndexValidator(models.Candidate))) @marshal_with(schemas.CandidateSearchPageSchema()) def get(self, **kwargs): query = self.get_candidates(kwargs) return utils.fetch_page(query, kwargs, model=models.Candidate)
class ScheduleBByPurposeView(BaseAggregateView): model = models.ScheduleBByPurpose fields = [ ('cycle', models.ScheduleBByPurpose.cycle), ('purpose', models.ScheduleBByPurpose.purpose), ] @args.register_kwargs(args.paging) @args.register_kwargs(args.schedule_b_by_purpose) @args.register_kwargs( args.make_sort_args( validator=args.IndexValidator(models.ScheduleBByPurpose))) @schemas.marshal_with(schemas.ScheduleBByPurposePageSchema()) def get(self, committee_id=None, **kwargs): return super().get(committee_id=committee_id, **kwargs)
class ScheduleBByRecipientIDView(BaseAggregateView): model = models.ScheduleBByRecipientID fields = [ ('cycle', models.ScheduleBByRecipientID.cycle), ('recipient_id', models.ScheduleBByRecipientID.recipient_id), ] @args.register_kwargs(args.paging) @args.register_kwargs(args.schedule_b_by_recipient_id) @args.register_kwargs( args.make_sort_args( validator=args.IndexValidator(models.ScheduleBByRecipientID))) @schemas.marshal_with(schemas.ScheduleBByRecipientIDPageSchema()) def get(self, committee_id=None, **kwargs): return super().get(committee_id=committee_id, **kwargs)
class ScheduleAByStateCandidateView(Resource): @args.register_kwargs(args.paging) @args.register_kwargs(args.make_sort_args()) @args.register_kwargs(args.schedule_a_candidate_aggregate) @schemas.marshal_with(schemas.ScheduleAByStateCandidatePageSchema()) def get(self, **kwargs): query = candidate_aggregate( ScheduleAByState, [ ScheduleAByState.state, sa.func.max(ScheduleAByState.state_full).label('state_full'), ], [ScheduleAByState.state], kwargs, ) return utils.fetch_page(query, kwargs, cap=0)
class ScheduleAByZipView(BaseAggregateView): model = models.ScheduleAByZip fields = [ ('cycle', models.ScheduleAByZip.cycle), ('zip', models.ScheduleAByZip.zip), ] @args.register_kwargs(args.paging) @args.register_kwargs(args.schedule_a_by_zip) @args.register_kwargs( args.make_sort_args( validator=args.IndexValidator(models.ScheduleAByZip))) @schemas.marshal_with(schemas.ScheduleAByZipPageSchema()) def get(self, committee_id=None, **kwargs): return super(ScheduleAByZipView, self).get(committee_id=committee_id, **kwargs)
class FilingsList(Resource): @args.register_kwargs(args.paging) @args.register_kwargs(args.filings) @args.register_kwargs( args.make_sort_args( default=['-receipt_date'], validator=args.IndexValidator(models.Filings), ) ) @schemas.marshal_with(schemas.FilingsPageSchema()) def get(self, **kwargs): query = models.Filings.query query = filter_query(models.Filings, query, fields, kwargs) query = utils.filter_range(query, kwargs, range_fields) count = counts.count_estimate(query, models.db.session, threshold=5000) return utils.fetch_page(query, kwargs, model=models.Filings, count=count)
class CandidateList(utils.Resource): @property def query(self): return models.Candidate.query @use_kwargs(args.paging) @use_kwargs(args.candidate_list) @use_kwargs(args.candidate_detail) @use_kwargs( args.make_sort_args( default=['name'], validator=args.IndexValidator(models.Candidate), )) @marshal_with(schemas.CandidatePageSchema()) def get(self, **kwargs): query = self.get_candidates(kwargs) return utils.fetch_page(query, kwargs, model=models.Candidate) def get_candidates(self, kwargs): candidates = self.query if kwargs.get('q'): candidates = utils.search_text( candidates.join( models.CandidateSearch, models.Candidate.candidate_id == models.CandidateSearch.id, ), models.CandidateSearch.fulltxt, kwargs['q'], ).distinct() candidates = filter_query(models.Candidate, candidates, filter_fields, kwargs) if kwargs.get('name'): candidates = candidates.filter( models.Candidate.name.ilike('%{}%'.format(kwargs['name']))) # TODO(jmcarp) Reintroduce year filter pending accurate `load_date` and `expire_date` values if kwargs.get('cycle'): candidates = candidates.filter( models.Candidate.cycles.overlap(kwargs['cycle'])) return candidates
class FilingsList(BaseFilings): filter_multi_fields = BaseFilings.filter_multi_fields + [ ('committee_id', models.Filings.committee_id), ('candidate_id', models.Filings.candidate_id), ] @use_kwargs(args.paging) @use_kwargs(args.filings) @use_kwargs(args.entities) @use_kwargs( args.make_sort_args( default=['-receipt_date'], validator=args.IndexValidator(models.Filings), )) @marshal_with(schemas.FilingsPageSchema()) def get(self, **kwargs): return super().get(**kwargs)
class ScheduleAByOccupationView(BaseAggregateView): model = models.ScheduleAByOccupation fields = [ ('cycle', models.ScheduleAByOccupation.cycle), ('occupation', models.ScheduleAByOccupation.occupation), ] @args.register_kwargs(args.paging) @args.register_kwargs(args.schedule_a_by_occupation) @args.register_kwargs( args.make_sort_args( validator=args.IndexValidator(models.ScheduleAByOccupation))) @schemas.marshal_with(schemas.ScheduleAByOccupationPageSchema()) def get(self, committee_id=None, **kwargs): query = self._build_query(committee_id, kwargs) count = counts.count_estimate(query, models.db.session, threshold=5000) return utils.fetch_page(query, kwargs, model=self.model, count=count)
def sort_args(self): return args.make_sort_args(validator=args.IndexValidator(self.model))
def args(self): return utils.extend( args.paging, args.candidate_totals, args.make_sort_args(), )
def sort_args(self): return args.make_sort_args(validator=args.IndexValidator(self.model, extra=["candidate", "committee"]))