def myplaces():
    user_id = uuid.UUID(session['uid'])
    if request.method == "GET":
        ans = []
        conn = dbhelper.connect()
        favorites = favoritesdb.get_favorites(user_id, conn)
        for favorite in favorites:
            fav = {"type": placesdb.get_place_type(favorite[1], conn)
                  , "address": (
                      placesdb.get_place_location_x(favorite[1], conn),
                      placesdb.get_place_location_y(favorite[1], conn)
                      )
                  , "rating": placesdb.get_place_rating(favorite[1], conn)
            }
            ans.append(fav)
        conn.close()
        return json.dumps(ans)
    elif request.method == "POST":
        place = request.get_json()
        conn = dbhelper.connect()
        try:
            place_id = placesdb.get_place_id(place['type'],
                    float(place['address'][0]),
                    float(place['address'][1]), conn)
            return favoritesdb.remove_favorite(user_id, place_id, conn)
        except ValueError, e:
            return "Malformed Request"
        finally:
def remove_place_front_end():
    required_keys = [ 'placeType'
                    , 'locationX'
                    , 'locationY'
                    ]
    conn = dbhelper.connect()
    try:
        if is_valid_request(request.form, required_keys):
            place_id = placesdb.get_place_id(request.form['placeType'],
                    float(request.form['locationX']),
                    float(request.form['locationY']), conn)
        return placesdb.remove_place_by_id(place_id, conn)
    except ValueError, e:
        return "Malformed Request"
def remove_favorite_front_end():
    user_id = uuid.UUID(session['uid'])
    required_keys = [ 'placeType'
                    , 'locationX'
                    , 'locationY'
                    ]
    if is_valid_request(request.form, required_keys):
        conn = dbhelper.connect()
        try:
            place_id = placesdb.get_place_id(request.form['placeType'],
                float(request.form['locationX']),
                float(request.form['locationY']), conn)
            return favoritesdb.remove_favorite(user_id, place_id, conn)
        except ValueError, e:
            return "Malformed Request"
        finally:
def report_place_front_end():
    user_id = uuid.UUID(session['uid'])
    required_keys = [ 'placeType'
                    , 'locationX'
                    , 'locationY'
                    , 'reason'
                    ]
    conn = dbhelper.connect()
    try:
        if is_valid_request(request.form, required_keys):
            place_id = placesdb.get_place_id(request.form['placeType'],
                    float(request.form['locationX']),
                    float(request.form['locationY']), conn)
            reason = request.form['reason']
            return placesdb.add_place_report(user_id, place_id,
                    reason, conn)[1]
    except ValueError, e:
        return "Malformed Request"
def add_description_front_end():
    user_id = uuid.UUID(session['uid'])
    required_keys = [ 'placeType'
                    , 'locationX'
                    , 'locationY'
                    , 'description'
                    ]
    if is_valid_request(request.form, required_keys):
        conn = dbhelper.connect()
        try:
            place_id = placesdb.get_place_id(request.form['placeType'],
                    float(request.form['locationX']),
                    float(request.form['locationY']), conn)
            description = request.form['description']
            return placesdb.update_place_description(place_id, description, conn)
        except ValueError, e:
            return "Malformed Request"
        finally:
def place_info():
    required_keys = [ 'placeType'
                    , 'locationX'
                    , 'locationY'
                    ]
    if is_valid_request(request.form, required_keys):
        uid = uuid.UUID(session['uid'])
        data = {}
        try:
            location_x = float(request.form['locationX'])
            location_y = float(request.form['locationY'])
            review_data = []
            conn = dbhelper.connect()
            place_id = placesdb.get_place_id(request.form['placeType'],
                    location_x, location_y, conn)
            reviews = reviewsdb.get_reviews(place_id, conn)
            for review in reviews:
                review_data.append(
                            { "userFirstName" :
                                usersdb.get_user_firstname(review[3], conn)
                            , "rating" : review[4]
                            , "review" : review[5]
                            , "userProfile" : usersdb.get_user_profile_url(review[3])
                            , "userPic" : usersdb.get_user_profile_pic_url(review[3],
                                                                        128)
                            , "isRatable" : review[3] != uid
                            })
            data['reviewFromUserExists'] = reviewsdb.review_exists(uid,
                                                     place_id, conn)
            data['createdPlace'] = placesdb.created_place(uid, place_id, conn)
            data['placeDescription'] = placesdb.get_place_description(place_id,
                                                                      conn)
            data['placeRating'] = placesdb.get_place_rating(place_id, conn)
            data['inFavorites'] = favoritesdb.in_favorites(uid, place_id, conn)
            data['reviews'] = review_data
            conn.close()
            return json.dumps(data)
        except ValueError, e:
            pass
def add_review_front_end():
    user = uuid.UUID(session['uid'])
    required_keys = [ 'placeType'
                    , 'locationX'
                    , 'locationY'
                    , 'rating'
                    , 'review'
                    ]
    if is_valid_request(request.form, required_keys):
        conn = dbhelper.connect()
        try:
            rating = int(request.form['rating'])
            location_x = float(request.form["locationX"])
            location_y = float(request.form["locationY"])
            placeid = placesdb.get_place_id(request.form["placeType"],
                    location_x, location_y, conn)
            if validate.is_valid_rating(rating):
                return reviewsdb.add_review(placeid, user, rating,
                        request.form["review"], conn)[1]
            else:
                return "Malformed Request"
        except ValueError, e:
            return "Malformed Request"
        finally: