Пример #1
0
def get_purchase_orders(po_id):
    ''' Returns all or selected purchase orders in JSON '''
    purchase_orders = PurchaseOrder.query
    if po_id is not None:
        purchase_orders = purchase_orders.filter_by(id=po_id)
        if purchase_orders.count() == 1:
            return jsonify(purchase_orders.first().to_dict())
    if request.args.get('status'):
        purchase_orders = purchase_orders.filter_by(
            status=PurchaseOrderStatus[request.args['status']].name)
    if request.args.get(
            'draw') is not None:  # Args were provided by DataTables
        purchase_orders, records_total, records_filtered = prepare_datatables_query(
            purchase_orders, request.values,
            or_(
                PurchaseOrder.id.like(f"%{request.values['search[value]']}%"),
                PurchaseOrder.customer.has(
                    Subcustomer.name.like(
                        f"%{request.values['search[value]']}%")),
                PurchaseOrder.status.like(
                    f'%{request.values["search[value]"]}%')))
        return jsonify({
            'draw':
            request.values['draw'],
            'recordsTotal':
            records_total,
            'recordsFiltered':
            records_filtered,
            'data':
            list(map(lambda entry: entry.to_dict(), purchase_orders))
        })
    if purchase_orders.count() == 0:
        abort(Response("No purchase orders were found", status=404))
    else:
        return jsonify([entry.to_dict() for entry in purchase_orders])
Пример #2
0
def get_invoices(invoice_id):
    '''
    Returns all or selected invoices in JSON:
    '''

    invoices = Invoice.query

    if invoice_id is not None:
        invoices = invoices.filter_by(id=invoice_id)
    else: # here we check whether request is filtered by DataTables
        if request.values.get('draw') is not None:
            invoices, records_total, records_filtered = prepare_datatables_query(
                invoices, request.values,
                or_(                
                    Invoice.id.like(f"%{request.values['search[value]']}%"),
                    Invoice.orders.any(Order.id.like(f"%{request.values['search[value]']}%")),
                    Invoice.customer.like(f"%{request.values['search[value]']}%"))
            )
            return jsonify({
                'draw': request.values['draw'],
                'recordsTotal': records_total,
                'recordsFiltered': records_filtered,
                'data': list(map(lambda entry: entry.to_dict(), invoices))
            })
        else: # By default we return only 100 invoices
            invoices = invoices.limit(10)
    

    return jsonify(list(map(lambda entry: entry.to_dict(), invoices)))
Пример #3
0
def get_order_products():
    '''
    Returns list of ordered items.
    '''
    order_products = OrderProduct.query
    if not current_user.has_role('admin'):
        order_products = order_products.filter(
            OrderProduct.suborder.has(
                Suborder.order.has(Order.user == current_user)))

    if request.values.get('order_id'):
        order_products = order_products.filter(
            or_(
                OrderProduct.order_id == request.values['order_id'],
                OrderProduct.suborder.has(
                    Suborder.order_id == request.values['order_id'])))

    if request.values.get(
            'draw') is not None:  # Args were provided by DataTables
        filter_clause = f"%{request.values['search[value]']}%"
        order_products, records_total, records_filtered = prepare_datatables_query(
            order_products, request.values,
            or_(
                OrderProduct.suborder.has(
                    Suborder.order_id.like(filter_clause)),
                OrderProduct.suborder.has(
                    Suborder.subcustomer.has(
                        Subcustomer.name.like(filter_clause))),
                OrderProduct.suborder.has(
                    Suborder.order.has(
                        Order.customer_name.like(filter_clause))),
                OrderProduct.product_id.like(filter_clause),
                OrderProduct.product.has(Product.name.like(filter_clause)),
                OrderProduct.product.has(
                    Product.name_english.like(filter_clause)),
                OrderProduct.product.has(
                    Product.name_russian.like(filter_clause)),
                OrderProduct.status == request.values['search[value]']))
        outcome = list(map(lambda entry: entry.to_dict(), order_products))
        if not current_user.has_role('admin'):
            for entry in outcome:
                entry.pop('private_comment', None)
        return jsonify({
            'draw': request.values['draw'],
            'recordsTotal': records_total,
            'recordsFiltered': records_filtered,
            'data': outcome
        })
    order_products = order_products.limit(100)
    if order_products.count() == 0:
        abort(Response("No order products were fond", status=404))

    outcome = list(map(lambda entry: entry.to_dict(), order_products))
    if not current_user.has_role('admin'):
        for entry in outcome:
            entry.pop('private_comment', None)

    return jsonify(outcome)
Пример #4
0
def _filter_nodes(nodes, filter_params):
    nodes, records_total, records_filtered = prepare_datatables_query(
        nodes, filter_params, None)
    return jsonify({
        'draw': int(filter_params['draw']),
        'recordsTotal': records_total,
        'recordsFiltered': records_filtered,
        'data': list(map(lambda entry: entry.to_dict(), nodes))
    })
Пример #5
0
def _filter_products(products, filter_params):
    products, records_total, records_filtered = prepare_datatables_query(
        products, filter_params, None)
    return jsonify({
        'draw': int(filter_params['draw']),
        'recordsTotal': records_total,
        'recordsFiltered': records_filtered,
        'data': [entry.to_dict(details=True) for entry in products]
    })
Пример #6
0
def filter_orders(orders, filter_params):
    orders = orders.order_by(Order.purchase_date_sort)
    orders, records_total, records_filtered = prepare_datatables_query(
        orders, filter_params, None)
    return jsonify({
        'draw': int(filter_params['draw']),
        'recordsTotal': records_total,
        'recordsFiltered': records_filtered,
        'data': list(map(lambda entry: entry.to_dict(), orders))
    })
Пример #7
0
def _filter_transactions(transactions, filter_params):
    transactions, records_total, records_filtered = prepare_datatables_query(
        transactions, filter_params, None
    )
    return jsonify({
        'draw': int(filter_params['draw']),
        'recordsTotal': records_total,
        'recordsFiltered': records_filtered,
        'data': [entry.to_dict() for entry in transactions]
    })
Пример #8
0
def admin_get_subcustomers():
    subcustomers = Subcustomer.query
    if request.values.get(
            'draw') is not None:  # Args were provided by DataTables
        filter_clause = f"%{request.values['search[value]']}%"
        subcustomers, records_total, records_filtered = prepare_datatables_query(
            subcustomers, request.values,
            or_(Subcustomer.name.like(filter_clause),
                Subcustomer.username.like(filter_clause)))
        outcome = list(map(lambda entry: entry.to_dict(), subcustomers))

        return jsonify({
            'draw': request.values['draw'],
            'recordsTotal': records_total,
            'recordsFiltered': records_filtered,
            'data': outcome
        })

    return jsonify([entry.to_dict() for entry in subcustomers])