def update_bio(user_id): data = MultiDict(mapping=request.json) form = BioForm(data) if form.validate(): data = request.json profile = Profile.query.filter(Profile.user_id == user_id).one() profile.biography = data["bio"] db.session.commit() return profile.user.to_dict() else: res = make_response({ "errors": format_errors(form.errors) }, 401) return res
def login(): data = MultiDict(mapping=request.json) form = LoginForm(data) if form.validate(): user = User.query.filter(User.email == data["email"]).first() if user and user.check_password(data["password"]): login_user(user, remember=True, duration=timedelta(days=7)) return {"id": user.to_dict()["id"]} else: res = make_response({"errors": ["Invalid credentials"]}, 401) return res else: res = make_response({"errors": format_errors(form.errors)}, 401) return res
def update_profile_recording(recording_id, profile_id): profile_recording = ProfileRecording.query.filter_by( recording_id=int(recording_id), profile_id=int(profile_id)).first() data = MultiDict(mapping=request.json) form = RecordingForm(data) if form.validate(): data = request.json profile_recording.title = data["title"] profile_recording.description = data["description"] db.session.commit() return {"profileRecording": profile_recording.to_dict()} else: r = make_response({"errors": format_errors(form.errors)}, 401) return r
def signup_user(): data = MultiDict(mapping=request.json) form = SignUpForm(data) if form.validate(): data = request.json new_user = User(first_name = data["first_name"], last_name = data["last_name"], email = data["email"], DOB = data["date_of_birth"], password=data["password"], lat = data["lat"], lng = data["lng"]) db.session.add(new_user) db.session.commit() new_user_dict = new_user.to_dict() new_user_profile = Profile(user_id = new_user_dict["id"], biography = "", location = data["location"]) db.session.add(new_user_profile) db.session.commit() return new_user_dict else: res = make_response({ "errors": format_errors(form.errors) }, 401) return res
def update_overview(user_id): data = MultiDict(mapping=request.json) form = OverviewForm(data) if form.validate(): data = request.json user = User.query.get(user_id) user.DOB = data["date_of_birth"] user.lat = data["lat"] user.lng = data["lng"] profile = Profile.query.filter(Profile.user_id == user_id).first() profile.location = data["location"] profile.instruments = db.session.query(Instrument).filter(Instrument.id.in_(data["instruments"])).all() profile.styles = db.session.query(Style).filter(Style.id.in_(data["styles"])).all() db.session.commit() return user.to_dict() else: res = make_response({ "errors": format_errors(form.errors) }, 401) return res
def create_recording(): data = MultiDict(mapping=request.json) form = RecordingForm(data) if form.validate(): data = request.json new_recording = Recording(url=data["url"]) db.session.add(new_recording) db.session.commit() new_pr = ProfileRecording(profile_id=data["profileId"], recording_id=new_recording.to_dict()["id"], title=data["title"], description=data["description"]) db.session.add(new_pr) db.session.commit() return { "recording": new_recording.to_dict(), "profileRecording": new_pr.to_dict() } else: r = make_response({"errors": format_errors(form.errors)}, 401) return r
def get_search_results(): data = MultiDict(mapping=request.json) form = SearchForm(data) if form.validate(): data = request.json user = User.query.get(data["userId"]) user_dict = user.to_dict() user_lat = user_dict["lat"] user_lng = user_dict["lng"] coords_dict = reverse_haversine(target_distance=data["radius"], lat=user_lat, lng=user_lng) min_lat = coords_dict["min_lat"] max_lat = coords_dict["max_lat"] min_lng = coords_dict["min_lng"] max_lng = coords_dict["max_lng"] if data["firstName"] and data["lastName"]: users = [ user.to_dict()["id"] for user in User.query.filter( and_( between(User.lat, min_lat, max_lat), between(User.lng, min_lng, max_lng), User.id != data["userId"], func.soundex(User.first_name) == func.soundex( data["firstName"]), func.soundex(User.last_name) == func.soundex( data["lastName"]))).all() ] elif data["firstName"]: users = [ user.to_dict()["id"] for user in User.query.filter( and_( between(User.lat, min_lat, max_lat), between(User.lng, min_lng, max_lng), User.id != data["userId"], func.soundex(User.first_name) == func.soundex( data["firstName"]))).all() ] else: users = [ user.to_dict()["id"] for user in User.query.filter( and_(between(User.lat, min_lat, max_lat), between(User.lng, min_lng, max_lng), User.id != data["userId"])).all() ] profile_insts = db.session.query( profile_instruments.c.profile_id).filter( and_( profile_instruments.c.profile_id.in_(users), profile_instruments.c.instrument_id.in_( data["instruments"]))) profile_stls = db.session.query(profile_styles.c.profile_id).filter( and_(profile_styles.c.profile_id.in_(users), profile_styles.c.style_id.in_(data["styles"]))) profiles = profile_insts.intersect(profile_stls).all() return { "searchResults": list(set([profile[0] for profile in profiles])) } else: res = make_response({"errors": format_errors(form.errors)}, 401) return res