def create_user(): if request.method == "POST": facebook_id = request.json["facebook_id"] thumbnail_image_location = request.json["thumbnail_image_location"] full_name = request.json["name"] email = request.json["email"] # See if this is the first time this user has logged in. user = g.db.query(User).filter(User.facebook_id == facebook_id).first() if user is None: new_user = User( facebook_id=facebook_id, thumbnail_image_location=thumbnail_image_location, full_name=full_name, email=email, created_at=arrow.utcnow(), ) g.db.add(new_user) # Commit early so we get a user id in the response. g.db.flush() return resource_created(new_user) else: # Update pictures user.thumbnail_image_location = thumbnail_image_location return resource_modified(user) else: return invalid_method()
def delete_match(match_id): if request.method == 'DELETE': match_id = int(match_id) return resource_deleted(match) else: return invalid_method()
def get_users(): if request.method == "GET": all_users = g.db.query(User).all() return resource_found(all_users) else: return invalid_method()
def get_match(match_id): match_id = int(match_id) if request.method == 'GET': return resource_found(match) else: return invalid_method()
def get_offer(offer_id): offer_id = int(offer_id) if request.method == "GET": offer = g.db.query(Offer).filter(Offer.id == offer_id).first() return resource_found(offer) else: return invalid_method()
def get_user(user_id): user_id = int(user_id) if request.method == "GET": user = g.db.query(User).filter(User.id == user_id).first() return resource_found(user) else: return invalid_method()
def delete_offer(offer_id): if request.method == "DELETE": offer_id = int(offer_id) offer = g.db.query(Offer).filter(Offer.id == offer_id).first() g.db.delete(offer) return resource_deleted(offer) else: return invalid_method()
def delete_user(user_id): if request.method == "DELETE": user_id = int(user_id) user = g.db.query(User).filter(User.id == user_id).first() g.db.delete(user) return resource_deleted(user) else: return invalid_method()
def change_user(user_id): if request.method == "PUT": user_id = int(user_id) user = g.db.query(User).filter(User.id == user_id).first() params = request.json for key in params: if key in User.changeable: setattr(user, key, params[key]) return resource_modified(user) else: return invalid_method()
def create_message(): if request.method == 'POST': params = request.json match_id = params['match_id'] user_id = params['user_id'] text = params['text'] new_message = Message(match_id=match_id,user_id=user_id,text=text) g.db.add(new_message) g.db.flush() g.socketio.emit('message',new_message.serialize(),namespace='/chat'+str(match_id)) return resource_created(new_message) else: return invalid_method()
def change_match(match_id): if request.method == 'PUT': match_id = int(match_id) params = request.json active = params.get('active',None) match = g.db.query(Match).filter(Match.id == match_id).first() if active is False: match.active = False g.socketio.emit('cancel',match.serialize(),namespace='/chat'+str(match.id)) return resource_modified(match) else: return invalid_method()
def get_offers(): if request.method == "GET": latitude = request.args.get("lat", None) longitude = request.args.get("lng", None) radius = request.args.get("rad", None) return_users = request.args.get("users", False) if latitude is None or longitude is None or radius is None: offers = g.db.query(Offer).all() else: offers = ( g.db.query(Offer) .filter( and_( Offer.latitude > latitude - radius, Offer.latitude < latitude + radius, Offer.longitude > longitude - radius, Offer.longitude < longitude + radius, ) ) .all() ) if return_users: offer_user_ids = (x.user_id for x in offers) users = g.db.query(User).filter(User.id.in_(user_ids)).all() return multiple_resources_found(offers, users) else: return resource_found(offers) else: return invalid_method()
def create_offer(): if request.method == "POST": params = request.json latitude = request.json["latitude"] longitude = request.json["longitude"] user_id = request.json["user_id"] text = request.json["text"] new_offer = Offer(latitude=latitude, longitude=longitude, text=text, created_at=arrow.utcnow(), user_id=user_id) g.db.add(new_offer) g.db.flush() g.socketio.emit("offer", new_offer.serialize(), namespace="/update") return resource_created(new_offer) else: return invalid_method()
def create_match(): if request.method == 'POST': params = request.json print params receiver_id = params['receiver_id'] deliverer_id = params['deliverer_id'] item_type = params['item_type'] item_id = params['item_id'] new_match = Match(receiver_id=receiver_id,deliverer_id=deliverer_id,item_type=item_type,item_id=item_id) g.db.add(new_match) g.db.flush() print "NEW MATCH" print new_match.serialize() g.socketio.emit('match',new_match.serialize(),namespace='/'+new_match.item_type+str(new_match.item_id)) return resource_created(new_match) else: return invalid_method()
def get_all(): if request.method == 'GET': latitude = request.args.get('lat',None) longitude = request.args.get('lng',None) radius = request.args.get('rad',None) if latitude is None or longitude is None or radius is None: orders = g.db.query(Order).all() offers = g.db.query(Offer).all() else: offers = g.db.query(Offer).filter(and_(Offer.latitude > latitude - radius,\ Offer.latitude < latitude + radius,\ Offer.longitude > longitude - radius, \ Offer.longitude < longitude + radius)).all() orders = g.db.query(Order).filter(and_(Order.latitude > latitude - radius,\ Order.latitude < latitude + radius,\ Order.longitude > longitude - radius, \ Order.longitude < longitude + radius)).all() order_user_ids = (x.user_id for x in orders) offer_user_ids = (x.user_id for x in offers) user_ids = set().union(*[order_user_ids,offer_user_ids]) users = g.db.query(User).filter(User.id.in_(user_ids)).all() return multiple_resources_found(orders,offers,users) else: return invalid_method()