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
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
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
def get_auction_endpoint(aid, uid): auction = Auction.query.get(aid) if auction is None: return '', 404 auction = AuctionSchema(only=('aid', 'uid', 'assetId', 'start', 'end', 'amount', 'minBid', 'sold', 'earnings', 'media.title', 'media.full_audio', 'media.demo_segment', 'media.cover')).dump(auction) auction['artist'] = User.query.get(auction['uid']).name auction['title'] = auction['media']['title'] auction['cover'] = auction['media']['cover'] if Ownership.query.filter_by(uid=uid, aid=aid).first() is None: auction['demo'] = auction['media']['demo_segment'] else: auction['demo'] = auction['media']['full_audio'] del auction['uid'] del auction['media'] participants = Bid.query.filter_by(aid=aid).order_by(Bid.bid.desc()).all() participants = BidSchema(only=('uid', 'bid')).dump(participants, many=True) for participant in participants: user = User.query.get(participant['uid']) participant['name'] = user.name participant['avatar'] = user.avatar del participant['uid'] auction['participants'] = participants return jsonify(auction), 200
def get_user_endpoint(uid): # get user's checksum user = User.query.get(uid) # evaluate if matches given checksum if user is None: return jsonify(message=f'User {uid} does not exist!'), 404 schema = UserSchema(only=('uid', 'name', 'avatar', 'title', 'identity', 'mnemonic', 'subscription', 'createdAt')) user = schema.dump(user) if user['identity'] == 0: # artist releases = Media.query \ .filter_by(uid=user['uid'], dist_type=False) \ .order_by(Media.title) \ .all() releases = MediaSchema(exclude=('uid', 'full_audio', 'demo_segment', 'dist_type')).dump(releases, many=True) for release in releases: release['artist'] = user['name'] user['releases'] = releases auctions = Auction.query \ .filter_by(uid=user['uid']) \ .order_by(Auction.start.desc()) \ .all() auctions = AuctionSchema(only=('aid', 'assetId', 'amount', 'start', 'end', 'sold', 'earnings', 'mid', 'media.title', 'media.cover')).dump(auctions, many=True) for auction in auctions: auction['cover'] = auction['media']['cover'] auction['title'] = auction['media']['title'] auction['artist'] = user['name'] del auction['media'] user['auctions'] = auctions else: # listener owns = Ownership.query.filter_by(uid=user['uid']).all() owns = OwnershipSchema(only=('aid', 'auction.assetId', 'auction.sold', 'auction.earnings', 'mid', 'media.uid', 'media.title', 'media.cover')).dump(owns, many=True) for own in owns: own['assetId'] = own['auction']['assetId'] own['sold'] = own['auction']['sold'] own['earnings'] = own['auction']['earnings'] own['title'] = own['media']['title'] own['cover'] = own['media']['cover'] own['artist'] = User.query.get(own['media']['uid']).name del own['auction'] del own['media'] user['owns'] = owns return jsonify(user), 200
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
def retrieve_all_auctions(): auctions = Auction.query.all() schema = AuctionSchema() return jsonify(*map(schema.dump, auctions)), 200