def costars_contract_upload():
    '''Upload a contract document pdf for costars

    Because the COSTARS website streams contract documents via POST requests
    instead having them live at some static endpoint, they are re-hosted in S3.

    :status 200: render the upload costars document template
    :status 302: attempt to upload a costars document to S3 and set the
        ``contract_href`` on the relevant
        :py:class:`~purchasing.data.contracts.ContractBase` object. Redirect
        to the same page.
    '''
    contracts = ContractBase.query.join(ContractType).filter(
        db.func.lower(ContractType.name) == 'costars',
        db.or_(ContractBase.contract_href == None,
               ContractBase.contract_href == '')).all()

    form = ContractUploadForm()

    if form.validate_on_submit():
        _file = request.files.get('upload')
        filename, filepath = upload_costars_contract(_file)

        contract = ContractBase.query.get(int(form.data.get('contract_id')))

        contract.update(contract_href=filepath)
        flash('Contract uploaded successfully', 'alert-success')
        return redirect(url_for('conductor_uploads.costars_contract_upload'))

    return render_template('/conductor/upload/upload_costars_documents.html',
                           form=form,
                           contracts=contracts)
def costars_contract_upload():
    contracts = ContractBase.query.join(ContractType).filter(
        db.func.lower(ContractType.name) == 'costars',
        db.or_(
            ContractBase.contract_href == None,
            ContractBase.contract_href == ''
        )
    ).all()

    form = ContractUploadForm()

    if form.validate_on_submit():
        _file = request.files.get('upload')
        filename, filepath = upload_costars_contract(_file)

        contract = ContractBase.query.get(int(form.data.get('contract_id')))

        contract.update(contract_href=filepath)
        flash('Contract uploaded successfully', 'alert-success')
        return redirect(url_for('conductor_uploads.costars_contract_upload'))

    return render_template(
        '/conductor/upload/upload_costars_documents.html',
        form=form, contracts=contracts
    )
def filter(department=None):
    '''
    Filter contracts by which ones have departmental subscribers
    '''
    pagination_per_page = current_app.config.get('PER_PAGE', 50)
    page = int(request.args.get('page', 1))
    lower_bound_result = (page - 1) * pagination_per_page
    upper_bound_result = lower_bound_result + pagination_per_page

    if department not in [i[0] for i in DEPARTMENT_CHOICES]:
        flash('You must choose a valid department!', 'alert-danger')
        return redirect(url_for('wexplorer.explore'))

    contracts = db.session.query(
        ContractBase.id, ContractBase.description,
        db.func.count(contract_user_association_table.c.user_id).label('cnt'),
        db.func.count(
            contract_starred_association_table.c.user_id).label('cnt2')
    ).outerjoin(contract_user_association_table).outerjoin(
        contract_starred_association_table).filter(
            db.or_(ContractBase.followers.any(department=department),
                   ContractBase.starred.any(department=department))
        ).group_by(ContractBase).having(
            db.or_(
                db.func.count(contract_user_association_table.c.user_id) > 0,
                db.func.count(contract_starred_association_table.c.user_id) >
                0)).order_by(db.text('cnt DESC')).all()

    pagination = SimplePagination(page, pagination_per_page, len(contracts))

    results = contracts[lower_bound_result:upper_bound_result]

    current_app.logger.info(
        'WEXFILTER - {department}: Filter by {department}'.format(
            department=department))

    return render_template('wexplorer/filter.html',
                           search_form=SearchForm(),
                           results=results,
                           pagination=pagination,
                           department=department,
                           choices=DEPARTMENT_CHOICES[1:],
                           path='{path}?{query}'.format(
                               path=request.path, query=request.query_string))
Beispiel #4
0
def find_contract_metadata(search_term,
                           case_statements,
                           filter_clauses,
                           archived=False):
    '''
    Takes a search term, case statements, and filter clauses and
    returns out a list of result objects including contract id,
    company id, financial id, expiration date, awarded name
    '''

    contracts = db.session.query(
        db.distinct(SearchView.contract_id).label('contract_id'),
        SearchView.company_id, SearchView.contract_description,
        SearchView.financial_id, SearchView.expiration_date,
        SearchView.company_name,
        db.case(case_statements).label('found_in'),
        db.func.max(
            db.func.full_text.ts_rank(
                db.func.setweight(
                    db.func.coalesce(SearchView.tsv_company_name, ''),
                    'A').concat(
                        db.func.setweight(
                            db.func.coalesce(
                                SearchView.tsv_contract_description, ''), 'D')
                    ).concat(
                        db.func.setweight(
                            db.func.coalesce(SearchView.tsv_detail_value, ''),
                            'D')).concat(
                                db.func.setweight(
                                    db.func.coalesce(
                                        SearchView.tsv_line_item_description,
                                        ''), 'B')),
                db.func.to_tsquery(
                    search_term,
                    postgresql_regconfig='english'))).label('rank')).join(
                        ContractBase,
                        ContractBase.id == SearchView.contract_id).filter(
                            db.or_(
                                db.cast(SearchView.financial_id,
                                        db.String) == search_term,
                                *filter_clauses), ContractBase.financial_id
                            is not None, ContractBase.expiration_date
                            is not None).group_by(
                                SearchView.contract_id, SearchView.company_id,
                                SearchView.contract_description,
                                SearchView.financial_id,
                                SearchView.expiration_date,
                                SearchView.company_name,
                                db.case(case_statements)).order_by(
                                    db.text('rank DESC'))

    if not archived:
        contracts = contracts.filter(ContractBase.is_archived == False)

    return contracts.all()
def find_contract_metadata(search_term, case_statements, filter_clauses, archived=False):
    '''
    Takes a search term, case statements, and filter clauses and
    returns out a list of result objects including contract id,
    company id, financial id, expiration date, awarded name
    '''

    contracts = db.session.query(
        db.distinct(SearchView.contract_id).label('contract_id'),
        SearchView.company_id, SearchView.contract_description,
        SearchView.financial_id, SearchView.expiration_date,
        SearchView.company_name, db.case(case_statements).label('found_in'),
        db.func.max(db.func.full_text.ts_rank(
            db.func.setweight(db.func.coalesce(SearchView.tsv_company_name, ''), 'A').concat(
                db.func.setweight(db.func.coalesce(SearchView.tsv_contract_description, ''), 'D')
            ).concat(
                db.func.setweight(db.func.coalesce(SearchView.tsv_detail_value, ''), 'D')
            ).concat(
                db.func.setweight(db.func.coalesce(SearchView.tsv_line_item_description, ''), 'B')
            ), db.func.to_tsquery(search_term, postgresql_regconfig='english')
        )).label('rank')
    ).join(
        ContractBase, ContractBase.id == SearchView.contract_id
    ).filter(
        db.or_(
            db.cast(SearchView.financial_id, db.String) == search_term,
            *filter_clauses
        ),
        ContractBase.financial_id is not None,
        ContractBase.expiration_date is not None
    ).group_by(
        SearchView.contract_id,
        SearchView.company_id,
        SearchView.contract_description,
        SearchView.financial_id,
        SearchView.expiration_date,
        SearchView.company_name,
        db.case(case_statements)
    ).order_by(
        db.text('rank DESC')
    )

    if not archived:
        contracts = contracts.filter(ContractBase.is_archived == False)

    return contracts.all()
def costars_contract_upload():
    '''Upload a contract document pdf for costars

    Because the COSTARS website streams contract documents via POST requests
    instead having them live at some static endpoint, they are re-hosted in S3.

    :status 200: render the upload costars document template
    :status 302: attempt to upload a costars document to S3 and set the
        ``contract_href`` on the relevant
        :py:class:`~purchasing.data.contracts.ContractBase` object. Redirect
        to the same page.
    '''
    contracts = ContractBase.query.join(ContractType).filter(
        db.func.lower(ContractType.name) == 'costars',
        db.or_(
            ContractBase.contract_href == None,
            ContractBase.contract_href == ''
        )
    ).all()

    form = ContractUploadForm()

    if form.validate_on_submit():
        _file = request.files.get('upload')
        filename, filepath = upload_costars_contract(_file)

        contract = ContractBase.query.get(int(form.data.get('contract_id')))

        contract.update(contract_href=filepath)
        flash('Contract uploaded successfully', 'alert-success')
        return redirect(url_for('conductor_uploads.costars_contract_upload'))

    return render_template(
        '/conductor/upload/upload_costars_documents.html',
        form=form, contracts=contracts
    )