def restaurant(): try: restaurant_id = request.args.get('restaurant_id') if restaurant_id: cursor = g.conn.execute(FIND_RESTAURANT_WITH_REVIEW_BY_ID, \ restaurant_id) results = get_results(cursor) reviews = collect_reviews(results) rest_cursor = g.conn.execute(FIND_RESTAURANT_BY_RESTAURANT_ID, \ restaurant_id) rest_results = get_results(rest_cursor) rest = collect_restaurants(rest_results) return render_template("restaurant_review.html", reviews=reviews, \ restaurants=rest) else: feature_cursor = g.conn.execute(FIND_ALL_FEATURES_SQL) region_cursor = g.conn.execute(FIND_ALL_REGION_ID_ZIPCODE_SQL) feature_tuples = get_results(feature_cursor) region_tuples = get_results(region_cursor) features = collect_features(feature_tuples) regions = collect_regions(region_tuples) return render_template("restaurant.html", features=features, \ regions=regions) except: return redirect("/")
def event(): event_id = request.args.get("event_id") event_owner = request.args.get("event_owner") if event_id: if event_owner: feature_cursor = g.conn.execute(FIND_ALL_FEATURES_SQL) region_cursor = g.conn.execute(FIND_ALL_REGION_ID_ZIPCODE_SQL) feature_tuples = get_results(feature_cursor) region_tuples = get_results(region_cursor) features = collect_features(feature_tuples) regions = collect_regions(region_tuples) event_cursor = g.conn.execute(FIND_EVENT_WITH_ID_SQL, event_id) event = get_first_result(event_cursor) restaurant_cursor = g.conn.execute \ (RECOMMEND_RESTAURANT_FOR_EVENT_BY_ID, event_id) restaurants = collect_restaurants(get_results(restaurant_cursor)) event_dict = dict(name=event[1], desc=event[2], \ datetime=datetime.combine(event[3], event[4]) \ .strftime("%Y-%m-%d %H:%M:%S"), \ event_id=event_id, event_owner=event_owner, \ features=features, regions=regions) return render_template("event.html", **event_dict) else: user_id = request.cookies.get("user_id") own_cursor = g.conn.execute(FIND_USER_OWN_EVENTS_SQL, user_id) join_cursor = g.conn.execute(FIND_USER_JOIN_EVENTS_SQL, user_id) own_events = get_results(own_cursor) join_events = get_results(join_cursor) data = dict(own_events=collect_events(own_events), \ join_events=collect_events(join_events)) return render_template("user_events.html", **data)
def signup(): try: region_cursor = g.conn.execute(FIND_ALL_REGION_ID_ZIPCODE_SORTED_SQL) region_tuples = get_results(region_cursor) regions = collect_regions(region_tuples) if request.method == "GET": return render_template("sign_up.html", regions=regions) else: # POST params = (request.form["username"], request.form["email"], \ request.form["age"], request.form["gender"], request.form["zipcode"]) g.conn.execute(SIGNUP_USER_SQL, params) cursor = g.conn.execute(GET_LAST_USER_ID_SQL) result = get_first_result(cursor) if cursor: resp = make_response(redirect("/")) delete_existing_user_cookie(resp) resp.set_cookie('username', request.form["username"]) resp.set_cookie('email', request.form["email"]) resp.set_cookie('user_id', str(result[0])) return resp else: # TODO(Chris): Handle the error format for signup, # ex. enter age with not numbers or some db callback print "Something happens in DB" return render_template("sign_up.html", regions=regions) except: return redirect("/")
def find_restaurants(): try: zipcode = request.form["zipcode"] feature_id = request.form["feature_id"] if not (zipcode or feature_id): return redirect("/restaurant") else: if zipcode and feature_id: cursor = g.conn.execute(FIND_RESTAURANT_BY_ZIPCODE_AND_FEATURE, (feature_id, zipcode)) elif feature_id: cursor = g.conn.execute(FIND_RESTAURANT_BY_FEATURE, feature_id) else: cursor = g.conn.execute(FIND_RESTAURANT_BY_ZIPCODE, zipcode) results = get_results(cursor) restaurants = collect_restaurants(results) return render_template("restaurant_results.html", restaurants=restaurants) except: return redirect("/")
def find_restaurants_api(): zipcode = request.form["zipcode"] feature_id = request.form["feature_id"] restaurants = [] if zipcode or feature_id: if zipcode and feature_id: cursor = g.conn.execute \ (limit_10_sql_formatter \ (FIND_RESTAURANT_BY_ZIPCODE_AND_FEATURE), \ (feature_id, zipcode)) elif feature_id: cursor = g.conn.execute \ (limit_10_sql_formatter(FIND_RESTAURANT_BY_FEATURE), \ feature_id) else: cursor = g.conn.execute \ (limit_10_sql_formatter(FIND_RESTAURANT_BY_ZIPCODE), \ zipcode) results = get_results(cursor) restaurants = collect_restaurants(results) return jsonify({'restaurants': restaurants})
def login(): if request.method == "GET": return render_template("login.html") else: email = request.form["email"] username = request.form["username"] cursor = g.conn.execute(LOGIN_USER_SQL, (username, email)) result = get_first_result(cursor) if result: resp = make_response(redirect("/")) delete_existing_user_cookie(resp) resp.set_cookie('username', username) resp.set_cookie('email', email) resp.set_cookie('user_id', str(result[0])) return resp else: # TODO(Chris): Handle the not found case error message print "No such user!" region_cursor = g.conn.execute(FIND_ALL_REGION_ID_ZIPCODE_SORTED_SQL) region_tuples = get_results(region_cursor) regions = collect_regions(region_tuples) return render_template("sign_up.html", regions=regions)
def login(): if request.method == "GET": return render_template("login.html") else: email = request.form["email"] username = request.form["username"] cursor = g.conn.execute(LOGIN_USER_SQL, (username, email)) result = get_first_result(cursor) if result: resp = make_response(redirect("/")) delete_existing_user_cookie(resp) resp.set_cookie('username', username) resp.set_cookie('email', email) resp.set_cookie('user_id', str(result[0])) return resp else: # TODO(Chris): Handle the not found case error message print "No such user!" region_cursor = g.conn.execute( FIND_ALL_REGION_ID_ZIPCODE_SORTED_SQL) region_tuples = get_results(region_cursor) regions = collect_regions(region_tuples) return render_template("sign_up.html", regions=regions)
def browse_event(): user_id = request.cookies.get("user_id") cursor = g.conn.execute(FIND_EVENTS_USER_NOT_IN_SQL, (user_id, user_id)) results = get_results(cursor) events = collect_events(results) return render_template("other_events.html", events=events)