Пример #1
0
    def transaction_create(payload):
        body = request.get_json()
        new_drink_id = body.get('drink_id')
        new_quantity = body.get('quantity')
        new_created_at = body.get('created_at')

        # check input
        if new_drink_id is None or new_quantity is None or new_created_at is None:
            abort(400)
        drink = Drink.query.get(new_drink_id)
        if drink is None or drink.quantity < new_quantity:
            abort(400)

        t = datetime.datetime.strptime(new_created_at, "%Y-%m-%d")
        trans = Transaction(new_drink_id, new_quantity, t)
        trans.insert()

        drink.quantity -= new_quantity
        drink.update()

        return jsonify({
            'success': True,
            'transaction': trans.format(),
            'drink': drink.name
        })
Пример #2
0
    def add_transaction():
        body = request.get_json()
        item = body.get('item', None)
        price = body.get('price', None)
        buyer_id = body.get('buyer_id', None)
        borrower_id = body.get('borrower_id', None)
        group_id = body.get('group_id', None)

        # try:
        new_transaction = Transaction(item=item,
                                      price=price,
                                      buyer_id=buyer_id,
                                      borrower_id=borrower_id,
                                      group_id=group_id)
        new_transaction.insert()

        # except:
        #     abort(422)

        buyer = User.query.filter_by(id=buyer_id).one()

        #Updates the user debts
        if not group_id:
            price = (price / 2)
            update_individual_transaction(buyer_id, borrower_id, price)

        else:
            update_group_transaction(buyer_id, group_id, price)

        return jsonify({
            'success': True,
            'transaction': new_transaction.format()
        })