Пример #1
0
def get_all_auctions_endpoint():
    ts = datetime.utcnow()
    schema = AuctionSchema(exclude=('assetId', 'contractId', 'minBid', 'media',
                                    'bids'))
    upcoming = Auction.query.filter(Auction.end > ts).order_by(
        Auction.start).all()
    upcoming = schema.dump(upcoming, many=True)
    finished = Auction.query.filter(Auction.end <= ts).order_by(
        Auction.start).all()
    finished = schema.dump(finished, many=True)

    for auction in upcoming:
        media = Media.query.get(auction['mid'])
        auction['title'] = media.title
        auction['cover'] = media.cover
        auction['artist'] = User.query.get(auction['uid']).name
        del auction['uid']
    for auction in finished:
        media = Media.query.get(auction['mid'])
        auction['title'] = media.title
        auction['cover'] = media.cover
        auction['artist'] = User.query.get(auction['uid']).name
        del auction['uid']

    return jsonify(upcoming=upcoming, finished=finished), 200
Пример #2
0
def get_bids_endpoint(aid):
    auction = Auction.query.get(aid)
    if auction is None:
        return '', 404

    schema = AuctionSchema(only=('uid', 'mid', 'assetId', 'contractId',
                                 'start', 'end', 'amount', 'minBid',
                                 'bids.uid', 'bids.bid'))
    return jsonify(schema.dump(auction)), 200
Пример #3
0
def create_auction_endpoint():
    data = request.get_json()
    if data is None:
        return '', 400

    message = AuctionSchema().validate(data)
    if len(message) > 0:
        return jsonify(message), 400

    auction = Auction(**data)
    db.session.add(auction)
    db.session.commit()
    schema = AuctionSchema()
    return jsonify(schema.dump(auction)), 201
Пример #4
0
def update_auction_endpoint():
    data = request.get_json()
    if data is None:
        return '', 400
    if 'aid' not in data:
        return jsonify(message='Missing aid in the request'), 400

    auction = Auction.query.get(data['aid'])
    if auction is None:
        return '', 404

    if 'assetId' in data:
        auction.assetId = data['assetId']
    if 'contractId' in data:
        auction.contractId = data['contractId']
    if 'sold' in data:
        auction.sold = data['sold']
    if 'earnings' in data:
        auction.earnings = data['earnings']
    db.session.commit()
    schema = AuctionSchema(exclude=('media', ))
    return jsonify(schema.dump(auction)), 200