Esempio n. 1
0
class ScheduleAView(ItemizedResource):

    model = models.ScheduleA

    @property
    def year_column(self):
        return self.model.report_year
    @property
    def index_column(self):
        return self.model.sched_a_sk
    @property
    def amount_column(self):
        return self.model.contribution_receipt_amount

    filter_multi_fields = [
        ('image_number', models.ScheduleA.image_number),
        ('committee_id', models.ScheduleA.committee_id),
        ('contributor_id', models.ScheduleA.contributor_id),
        ('contributor_city', models.ScheduleA.contributor_city),
        ('contributor_state', models.ScheduleA.contributor_state),
    ]
    filter_match_fields = [
        ('is_individual', is_individual),
    ]
    filter_range_fields = [
        (('min_date', 'max_date'), models.ScheduleA.contribution_receipt_date),
        (('min_amount', 'max_amount'), models.ScheduleA.contribution_receipt_amount),
        (('min_image_number', 'max_image_number'), models.ScheduleA.image_number),
    ]
    filter_fulltext_fields = [
        ('contributor_name', models.ScheduleA.contributor_name_text),
        ('contributor_employer', models.ScheduleA.contributor_employer_text),
        ('contributor_occupation', models.ScheduleA.contributor_occupation_text),
    ]
    query_options = [
        sa.orm.joinedload(models.ScheduleA.committee),
        sa.orm.joinedload(models.ScheduleA.contributor),
    ]

    @use_kwargs(args.itemized)
    @use_kwargs(args.schedule_a)
    @use_kwargs(args.make_seek_args())
    @use_kwargs(
        args.make_sort_args(
            validator=args.OptionValidator([
                'contribution_receipt_date',
                'contribution_receipt_amount',
                'contributor_aggregate_ytd',
            ]),
            multiple=False,
        )
    )
    @marshal_with(schemas.ScheduleAPageSchema())
    def get(self, **kwargs):
        return super().get(**kwargs)

    def build_query(self, **kwargs):
        query = super().build_query(**kwargs)
        query = filters.filter_contributor_type(query, self.model.entity_type, kwargs)
        return query
Esempio n. 2
0
 def args(self):
     return utils.extend(
         args.electioneering,
         args.make_seek_args(),
         args.make_sort_args(validator=args.IndexValidator(
             models.Electioneering), ),
     )
Esempio n. 3
0
 def args(self):
     return utils.extend(
         args.electioneering,
         args.make_seek_args(),
         args.make_sort_args(
             validator=args.IndexValidator(models.Electioneering),
         ),
     )
Esempio n. 4
0
 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',
         ]), ))
Esempio n. 5
0
 def args(self):
     return utils.extend(
         args.itemized,
         args.communication_cost,
         args.make_seek_args(),
         args.make_sort_args(validator=args.IndexValidator(
             models.CommunicationCost), ),
     )
Esempio n. 6
0
 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']), ),
     )
Esempio n. 7
0
 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(self.sort_options),
             show_nulls_last_arg=False,
         ))
Esempio n. 8
0
 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']),
         ),
     )
Esempio n. 9
0
 def args(self):
     return utils.extend(
         args.itemized,
         args.communication_cost,
         args.make_seek_args(),
         args.make_sort_args(
             validator=args.IndexValidator(models.CommunicationCost),
         ),
     )
Esempio n. 10
0
 def args(self):
     return utils.extend(
         args.itemized, args.schedule_h4, args.make_seek_args(),
         args.make_sort_args(
             default='-event_purpose_date',
             validator=args.OptionValidator(
                 ['event_purpose_date', 'disbursement_amount']),
             show_nulls_last_arg=False,
         ))
Esempio n. 11
0
 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),
         ),
     )
Esempio n. 12
0
 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',
         ]), ),
     )
Esempio n. 13
0
 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,
         ),
     )
Esempio n. 14
0
 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,
         ))
Esempio n. 15
0
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,
        )
Esempio n. 16
0
 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',
             ]),
         )
     )
Esempio n. 17
0
 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',
             ]),
         ),
     )
Esempio n. 18
0
 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'
             ]),
         ),
     )
Esempio n. 19
0
 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',
                 ]
             ),
             show_nulls_last_arg=False,
         ),
     )
Esempio n. 20
0
class ScheduleEView(ItemizedResource):

    model = models.ScheduleE

    @property
    def year_column(self):
        return self.model.report_year
    @property
    def index_column(self):
        return self.model.sched_e_sk
    @property
    def amount_column(self):
        return self.model.expenditure_amount

    filter_multi_fields = [
        ('cycle', sa.func.get_cycle(models.ScheduleE.report_year)),
        ('image_number', models.ScheduleE.image_number),
        ('committee_id', models.ScheduleE.committee_id),
        ('candidate_id', models.ScheduleE.candidate_id),
    ]
    filter_fulltext_fields = [
        ('payee_name', models.ScheduleE.payee_name_text),
    ]
    filter_range_fields = [
        (('min_date', 'max_date'), models.ScheduleE.expenditure_date),
        (('min_amount', 'max_amount'), models.ScheduleE.expenditure_amount),
        (('min_image_number', 'max_image_number'), models.ScheduleE.image_number),
    ]
    query_options = [
        sa.orm.joinedload(models.ScheduleE.candidate),
        sa.orm.joinedload(models.ScheduleE.committee),
    ]

    @use_kwargs(args.itemized)
    @use_kwargs(args.schedule_e)
    @use_kwargs(args.make_seek_args())
    @use_kwargs(
        args.make_sort_args(
            validator=args.OptionValidator([
                'expenditure_date',
                'expenditure_amount',
                'office_total_ytd',
            ]),
            multiple=False,
        )
    )
    @marshal_with(schemas.ScheduleEPageSchema())
    def get(self, **kwargs):
        return super(ScheduleEView, self).get(**kwargs)

    def filter_election(self, query, kwargs):
        if not kwargs['office']:
            return query
        utils.check_election_arguments(kwargs)
        query = query.join(
            models.CandidateHistory,
            models.ScheduleE.candidate_id == models.CandidateHistory.candidate_id,
        ).filter(
            models.CandidateHistory.two_year_period == kwargs['cycle'],
            models.CandidateHistory.office == kwargs['office'][0].upper(),
            models.ScheduleE.report_year.in_([kwargs['cycle'], kwargs['cycle'] - 1]),
        )
        if kwargs['state']:
            query = query.filter(models.CandidateHistory.state == kwargs['state'])
        if kwargs['district']:
            query = query.filter(models.CandidateHistory.district == kwargs['district'])
        return query
Esempio n. 21
0
class ScheduleAView(ItemizedResource):

    model = models.ScheduleA

    @property
    def year_column(self):
        return self.model.report_year

    @property
    def index_column(self):
        return self.model.sched_a_sk

    @property
    def amount_column(self):
        return self.model.contributor_receipt_amount

    filter_multi_fields = [
        ('image_number', models.ScheduleA.image_number),
        ('committee_id', models.ScheduleA.committee_id),
        ('contributor_id', models.ScheduleA.contributor_id),
        ('contributor_city', models.ScheduleA.contributor_city),
        ('contributor_state', models.ScheduleA.contributor_state),
    ]
    filter_fulltext_fields = [
        ('contributor_name', models.ScheduleASearch.contributor_name_text),
        ('contributor_employer',
         models.ScheduleASearch.contributor_employer_text),
    ]
    filter_range_fields = [
        (('min_date', 'max_date'), models.ScheduleA.contributor_receipt_date),
        (('min_amount', 'max_amount'),
         models.ScheduleA.contributor_receipt_amount),
        (('min_image_number', 'max_image_number'),
         models.ScheduleA.image_number),
    ]

    @args.register_kwargs(args.itemized)
    @args.register_kwargs(args.schedule_a)
    @args.register_kwargs(args.make_seek_args())
    @args.register_kwargs(
        args.make_sort_args(
            validator=args.OptionValidator(
                ['contributor_receipt_date', 'contributor_receipt_amount']),
            multiple=False,
        ))
    @schemas.marshal_with(schemas.ScheduleAPageSchema())
    def get(self, **kwargs):
        return super(ScheduleAView, self).get(**kwargs)

    def build_query(self, kwargs):
        query = super(ScheduleAView, self).build_query(kwargs)
        query = query.options(sa.orm.joinedload(models.ScheduleA.committee))
        query = query.options(sa.orm.joinedload(models.ScheduleA.contributor))
        query = self.filter_contributor_type(query, kwargs)
        return query

    def filter_contributor_type(self, query, kwargs):
        if kwargs['contributor_type'] == ['individual']:
            query = query.filter(self.model.contributor_id == None)  # noqa
        elif kwargs['contributor_type'] == ['committee']:
            query = query.filter(self.model.contributor_id != None)  # noqa
        return query

    def join_fulltext(self, query):
        return query.join(
            models.ScheduleASearch,
            models.ScheduleA.sched_a_sk == models.ScheduleASearch.sched_a_sk,
        )
Esempio n. 22
0
class ScheduleAView(ItemizedResource):

    model = models.ScheduleA

    @property
    def year_column(self):
        return self.model.report_year

    @property
    def index_column(self):
        return self.model.sched_a_sk

    @property
    def amount_column(self):
        return self.model.contribution_receipt_amount

    filter_multi_fields = [
        ('image_number', models.ScheduleA.image_number),
        ('committee_id', models.ScheduleA.committee_id),
        ('contributor_id', models.ScheduleA.contributor_id),
        ('contributor_city', models.ScheduleA.contributor_city),
        ('contributor_state', models.ScheduleA.contributor_state),
    ]
    filter_match_fields = [
        ('is_individual', is_individual),
    ]
    filter_range_fields = [
        (('min_date', 'max_date'), models.ScheduleA.contribution_receipt_date),
        (('min_amount', 'max_amount'),
         models.ScheduleA.contribution_receipt_amount),
        (('min_image_number', 'max_image_number'),
         models.ScheduleA.image_number),
    ]
    filter_fulltext_fields = [
        ('contributor_name', models.ScheduleASearch.contributor_name_text),
        ('contributor_employer',
         models.ScheduleASearch.contributor_employer_text),
        ('contributor_occupation',
         models.ScheduleASearch.contributor_occupation_text),
    ]

    @args.register_kwargs(args.itemized)
    @args.register_kwargs(args.schedule_a)
    @args.register_kwargs(args.make_seek_args())
    @args.register_kwargs(
        args.make_sort_args(
            validator=args.OptionValidator([
                'contribution_receipt_date',
                'contribution_receipt_amount',
                'contributor_aggregate_ytd',
            ]),
            multiple=False,
        ))
    @schemas.marshal_with(schemas.ScheduleAPageSchema())
    def get(self, **kwargs):
        if len(kwargs['committee_id']) > 5:
            raise exceptions.ApiError(
                'Can only specify up to five values for "committee_id".',
                status_code=422,
            )
        if len(kwargs['committee_id']) > 1:
            query, count = self.join_committee_queries(kwargs)
            return utils.fetch_seek_page(query,
                                         kwargs,
                                         self.index_column,
                                         count=count)
        return super(ScheduleAView, self).get(**kwargs)

    def build_query(self, kwargs, join=True):
        query = super(ScheduleAView, self).build_query(kwargs)
        query = filters.filter_contributor_type(query, self.model.entity_type,
                                                kwargs)
        if join:
            query = query.options(sa.orm.joinedload(
                models.ScheduleA.committee))
            query = query.options(
                sa.orm.joinedload(models.ScheduleA.contributor))
        return query

    def join_committee_queries(self, kwargs):
        queries = []
        total = 0
        for committee_id in kwargs['committee_id']:
            query, count = self.build_committee_query(kwargs, committee_id)
            queries.append(query.subquery().select())
            total += count
        query = models.db.session.query(models.ScheduleA).select_entity_from(
            sa.union_all(*queries))
        query = query.options(sa.orm.joinedload(models.ScheduleA.committee))
        query = query.options(sa.orm.joinedload(models.ScheduleA.contributor))
        return query, total

    def build_committee_query(self, kwargs, committee_id):
        query = self.build_query(utils.extend(
            kwargs, {'committee_id': [committee_id]}),
                                 join=False)
        sort, hide_null, nulls_large = kwargs['sort'], kwargs[
            'sort_hide_null'], kwargs['sort_nulls_large']
        query, _ = sorting.sort(query,
                                sort,
                                model=models.ScheduleA,
                                hide_null=hide_null,
                                nulls_large=nulls_large)
        page_query = utils.fetch_seek_page(query,
                                           kwargs,
                                           self.index_column,
                                           count=-1,
                                           eager=False).results
        count = counts.count_estimate(query, models.db.session, threshold=5000)
        return page_query, count

    def join_fulltext(self, query):
        return query.join(
            models.ScheduleASearch,
            models.ScheduleA.sched_a_sk == models.ScheduleASearch.sched_a_sk,
        )