Exemplo n.º 1
0
def update_bid():
    """Update a bid"""
    data = {
        'email': request.json.get('email'),
        'password': request.json.get('password'),
        'amount': request.json.get('amount'),
        'bid_id': request.json.get('bid_id'),
    }
    check_valid(UpdateBidSchema().validate(data))

    user = Users.get_user(data['email'], data['password'])
    bid = Bids.get_bid(data['email'], data['bid_id'])
    project = Projects.get_project(bid.project_id, 0)

    total_bids = 0
    current_amnt = float(bid.amount)
    new_amnt = float(request.json['amount'])

    for item in project.bids_received:
        total_bids = total_bids + float(item.amount)

    total_bids = total_bids - current_amnt + new_amnt
    if float(project.contract_value) < total_bids:
        return jsonify({
            "msg": "The amount added is too much. Reduce it by {}".format(
                (total_bids - float(project.contract_value)))}), 400

    bid.amount = new_amnt
    bid.date = datetime.date.today()
    bid.save()

    return jsonify({
        'message': 'Updated Bid.',
        'data': bid.serialize(),
        }), 200
Exemplo n.º 2
0
def bid_to_project():
    """Bid to fund a project"""
    data = {
        'email': request.json.get('email'),
        'password': request.json.get('password'),
        'amount': request.json.get('amount'),
        'project_id': request.json.get('project_id'),
    }
    check_valid(BidSchema().validate(data))

    data['amount'] = float(data['amount'])
    user = Users.get_user(data['email'], data['password'])
    project = Projects.get_project(
        request.json['project_id'], data['amount'])

    bid = Bids(
        amount=data['amount'], date=datetime.date.today(),
        user_email=user.email, project_id=project.id,)
    bid.save()

    data['user_email'] = user.email
    data['project_id'] = project.id
    data['bid_id'] = bid.id
    data['potential_profit_margin'] = "{0:.2f}".format(
        float(project.percentage_return) / 100 * data['amount'])

    return jsonify({
        'message': 'Bid Created Successfully.',
        'data': data,
        }), 201
Exemplo n.º 3
0
def delete_bid():
    """Delete a bid"""
    data = {
        'email': request.json.get('email'),
        'password': request.json.get('password'),
        'bid_id': request.json.get('bid_id'),
    }
    err = DeleteBidSchema().validate(data)
    if err:
        return jsonify(err), 400
    user = Users.get_user(data['email'], data['password'])

    bid = Bids.query.filter_by(
        user_email=user.email, id=data['bid_id']).first()
    if not bid:
        return jsonify({'msg': "This bid was not found"}), 404

    # Retrieve project to check whether project still active
    project = Projects.get_project(bid.project_id, 0)

    bid.delete()

    return jsonify({'msg': "Bid Deleted"}), 200