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 blogpost(title=None,post=None,comments=None): if request.method=="GET": dbhelper.connect() post = dbhelper.get_post(title) comments = dbhelper.get_comments(title) titles = dbhelper.get_posts() author="" for item in titles: if item[1] == title: author = item[0] break dbhelper.close() return render_template("blogpost.html",title=title,author=author, post=post, comments=comments) else: button = request.form["submit"] poster = request.form["author"] text = request.form["textarea"] time = strftime("%a %d %b %Y %X") dbhelper.connect() dbhelper.insert_comment(title, poster, text, time) post = dbhelper.get_post(title) comments = dbhelper.get_comments(title) titles = dbhelper.get_posts() author="" for item in titles: if item[1] == title: author = item[0] break dbhelper.close() return render_template("blogpost.html",title=title,author=author, post=post,comments=comments)
def add_place(place_type, location_x, location_y, finder, description, conn=None): global ID_PLACE persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""SELECT 1 FROM Places WHERE PlaceType=%s AND LocationX=%s AND LocationY=%s LIMIT 1""", (place_type, location_x, location_y)) exists = c.fetchone() if not exists: puid = dbhelper.generate_id(ID_PLACE) if not puid[0]: return puid[1] c.execute("INSERT INTO Places VALUES(%s, %s, %s, %s, %s, 0, %s, %s)", (puid[1], puid[1], place_type, location_x, location_y, usersdb.get_user_id(finder), description)) conn.commit() return "Location added to map" else: return "Location already exists" except psycopg2.DatabaseError, e: print 'Error %s' % e
def update_user_email(uid, new_email, verify_password=None, conn=None): # NOTE: Make sure email is validated before calling this method # Verification of the old password can be skipped by setting # verify_password to None if verify_password: old_password = get_user_password(uid=uid) if old_password: if not validate.check_password(old_password, verify_password): return (False, "Invalid verification credentials") persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("UPDATE USERS SET Email = %s WHERE ID = %s", (new_email, uid)) conn.commit() session['email'] = new_email update_user_email_confirmed(uid, False) return (True, "Successfully updated email") except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_user_lastname(uid=None, email=None, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: results = () if uid: c.execute("""SELECT LastName FROM Users WHERE ID = %s LIMIT 1""", (uid, )) results = c.fetchone() elif email: c.execute( """SELECT LastName FROM Users WHERE Email = %s LIMIT 1""", (email, )) results = c.fetchone() if results and results[0]: return results[0] else: return '' except psycopg2.DatabaseError, e: print 'Error %s' % e
def add_review(placeID, reviewer, rating, review, conn=None): global ID_REVIEW persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: uuid = dbhelper.generate_id(ID_REVIEW) if not uuid[0]: return (False, uuid[1]) c.execute( """SELECT * FROM Reviews WHERE Reviewer=%s AND PlacesID=%s LIMIT 1""", (reviewer, placeID)) conn.commit() exists = c.fetchone() ret_str = "" if not exists: c.execute("INSERT INTO Reviews VALUES(%s, %s, %s, %s, %s, %s)", (uuid[1], uuid[1], placeID, reviewer, rating, review)) ret_str = "Successfully added review" else: c.execute( """UPDATE Reviews SET Rating=%s, Review=%s WHERE Reviewer=%s AND PlacesID=%s""", (rating, review, reviewer, placeID)) ret_str = "Successfully updated review" conn.commit() calc_rating(placeID) return (True, ret_str) except psycopg2.DatabaseError, e: print 'Error %s' % e
def add_favorite(user_id, place_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute( """SELECT * FROM FAVORITES WHERE UserID = %s AND PlacesID = %s LIMIT 1""", (user_id, place_id)) conn.commit() exists = c.fetchone() if not exists: c.execute("""INSERT INTO FAVORITES VALUES (%s, %s)""", (user_id, place_id)) c.execute( """UPDATE Places SET FAVORITES = FAVORITES + 1 WHERE PlaceID = %s""", (place_id, )) conn.commit() return "Location added to My Places" else: return "Location already in My Places" except psycopg2.DatabaseError, e: print 'Error %s' % e
def main(titles=None): if request.method == "GET": dbhelper.connect() #dbhelper.create_table_posts(); titles = dbhelper.get_posts() dbhelper.close() return render_template("home.html",titles=titles) else: button = request.form["submit"] title = request.form["title"] text = request.form["textarea"] poster = request.form["author"] time = strftime("%a %d %b %Y %X") dbhelper.connect() dbhelper.insert_post(poster, title, text, time) titles = dbhelper.get_posts() dbhelper.close() return render_template("home.html",titles=titles)
def get_route_info(route_short_name='15'): """ extract specific route from gtfs data :param route_short_name: default value is 15 :return: trips and stops_location """ connection = dbhelper.connect() cursor = connection.cursor() trips, stops_location = {}, {} for shape_id in get_shapes_id(route_short_name, cursor): shape_id = shape_id[0] trip_id = get_trips_id(shape_id, cursor)[0][0] trips[trip_id] = { 'stop_id': [], 'lon': [], 'lat': [], 'travel_time': [], 'travel_distance': [], 'timestamp': [] } for stop_id, departure_time, shape_dist_traveled in get_stops_id_using_trip_id( trip_id, cursor): if shape_dist_traveled == '0': trips[trip_id]['travel_time'].append(0) else: trips[trip_id]['travel_time'].append( convert_to_timestamp(departure_time) - trips[trip_id]['timestamp'][0]) stop_id_nor = stop_id[-4:] if stop_id[-4] != '0' else stop_id[-3:] if stop_id_nor not in stops_location: # stops location: (lat, lon) stops_location[stop_id_nor] = get_stop_loc(stop_id, cursor)[0] trips[trip_id]['stop_id'].append(stop_id_nor) trips[trip_id]['travel_distance'].append( float(shape_dist_traveled)) trips[trip_id]['timestamp'].append( convert_to_timestamp(departure_time)) trips[trip_id]['lon'].append(float(stops_location[stop_id_nor][1])) trips[trip_id]['lat'].append(float(stops_location[stop_id_nor][0])) # remove redundant routes trips_stops, trip_remove = {}, [] for trip_id, trip in trips.items(): start_stop = trip['stop_id'][0] if start_stop not in trips_stops: trips_stops[start_stop] = trip_id else: if len(trips[trip_id]['stop_id']) > len( trips[trips_stops[start_stop]]['stop_id']): trip_remove.append(trips_stops[start_stop]) else: trip_remove.append(trip_id) for trip_id in trip_remove: trips.pop(trip_id) trips_convert = {} for trip in trips.values(): trips_convert[trip['stop_id'][0]] = trip return trips_convert, stops_location
def uid_exists(uid, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""SELECT 1 FROM Users WHERE UserId = %s LIMIT 1""", (uid, )) return c.fetchone() except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_user_disabled(uid, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("SELECT Disabled FROM USERS WHERE ID = %s LIMIT 1", (uid, )) return c.fetchone()[0] except psycopg2.DatabaseError, e: print 'Error %s' % e
def uid_exists(uid, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""SELECT 1 FROM Users WHERE UserId = %s LIMIT 1""", (uid,)) return c.fetchone() except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_user_phone(uid, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""SELECT Phone FROM Users WHERE ID = %s LIMIT 1""", (uid, )) result = c.fetchone() return result[0] if result else None except psycopg2.DatabaseError, e: print 'Error %s' % e
def remove_temporary_url(uuid, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return (False, "Database Error") c = conn.cursor() try: c.execute("""DELETE FROM TemporaryUrls WHERE UrlID = %s""", (uuid,)) conn.commit() return (True, "Successfully deleted temporary url") except psycopg2.DatabaseError, e: print 'Error %s' % e
def update_user_bio(uid, new_bio, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("UPDATE USERS SET Bio = %s WHERE ID = %s", (new_bio, uid)) conn.commit() return (True, "Successfully updated user bio") except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_place_location_y(pid, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("SELECT LocationY FROM Places WHERE ID = %s LIMIT 1", (pid,)) result = c.fetchone() return result[0] if result else None except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_places(conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("SELECT * FROM PLACES") conn.commit() return dictionarify(c.fetchall()) except psycopg2.DatabaseError, e: print 'Error %s' % e
def is_place_reported_by(reporter_id, reported_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return (False, "Database Error") c = conn.cursor() try: c.execute("""SELECT 1 FROM ReportsPlaces WHERE ReporterId = %s AND ReportedId = %s LIMIT 1 """, (reporter_id, reported_id)) return c.fetchone() except psycopg2.DatabaseError, e: print 'Error %s' % e
def remove_place(place_type, location_x, location_y, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""DELETE FROM Places WHERE PlaceType = %s AND LocationX = %s AND LocationY = %s""", (place_type, location_x, location_y)) conn.commit() return "Location removed from map" except psycopg2.DatabaseError, e: print 'Error %s' % e
def expire_temporary_urls(conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""DELETE FROM TemporaryUrls WHERE CreationTime < NOW() - ExpiryTime""") conn.commit() except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_num_reports_for_user(reported_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return (False, "Database Error") c = conn.cursor() try: c.execute("""SELECT 1 FROM ReportsUsers WHERE ReportedId = %s""", (reported_id, )) return len(c.fetchall()) except psycopg2.DatabaseError, e: print 'Error %s' % e
def remove_place_by_id(place_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""DELETE FROM Places WHERE PlaceId = %s""", (place_id,)) conn.commit() return "Location removed" except psycopg2.DatabaseError, e: print 'Error %s' % e
def remove_temporary_url(uuid, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return (False, "Database Error") c = conn.cursor() try: c.execute("""DELETE FROM TemporaryUrls WHERE UrlID = %s""", (uuid, )) conn.commit() return (True, "Successfully deleted temporary url") except psycopg2.DatabaseError, e: print 'Error %s' % e
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 get_reviews(placeID, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("SELECT * FROM Reviews WHERE PlacesID=%s", (placeID, )) conn.commit() return c.fetchall() except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_user_disabled(uid, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("SELECT Disabled FROM USERS WHERE ID = %s LIMIT 1", (uid,)) return c.fetchone()[0] except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_favorites(user_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""SELECT * FROM Favorites WHERE UserID = %s""", (user_id,)) conn.commit() return c.fetchall() except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_num_reports_for_user(reported_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return (False, "Database Error") c = conn.cursor() try: c.execute("""SELECT 1 FROM ReportsUsers WHERE ReportedId = %s""", (reported_id,)) return len(c.fetchall()) except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_favorites(user_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""SELECT * FROM Favorites WHERE UserID = %s""", (user_id, )) conn.commit() return c.fetchall() except psycopg2.DatabaseError, e: print 'Error %s' % e
def remove_place_report(reporter_id, reported_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return (False, "Database Error") c = conn.cursor() try: c.execute("""REMOVE FROM ReportsPlaces WHERE ReporterId = %s AND ReportedId = %s """, (reporter_id, reported_id)) conn.commit() return (True, "Place report removal successful") except psycopg2.DatabaseError, e: print 'Error %s' % e
def update_user_phone_confirmed(uid, confirmed, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("UPDATE USERS SET PhoneConfirmed = %s WHERE ID = %s", (confirmed, uid)) conn.commit() return (True, "Successfully updated user phone confirmed flag") except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_user_phone_confirmed(uid, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("SELECT PhoneConfirmed FROM USERS WHERE ID = %s LIMIT 1", (uid,)) result = c.fetchone() return result[0] if result else None except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_user_id(email, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""SELECT UserID FROM Users WHERE Email = %s LIMIT 1""", (email,)) result = c.fetchone() return result[0] if result else None except psycopg2.DatabaseError, e: print 'Error %s' % e
def update_place_description(pid, description, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("UPDATE Places SET Description = %s WHERE PlaceID = %s", (description, pid)) conn.commit() return "Successfully updated description" except psycopg2.DatabaseError, e: print 'Error %s' % e
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 confirm_email(url_id=None): if url_id: url_id = inflate_uuid(url_id) if url_id: uid = uuid.UUID(session['uid']) url_id = uuid.UUID(url_id) conn = dbhelper.connect() if tmpurldb.get_temporary_url(url_id, uid, TEMP_URL_EMAIL_CONFIRM, conn)[0]: usersdb.update_user_email_confirmed(uid, True, conn) tmpurldb.remove_temporary_url(url_id, conn) conn.close() return redirect(url_for('settings')) else: conn.close() return redirect(url_for('index'))
def get_place_id(place_type, location_x, location_y, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("""SELECT * FROM Places WHERE PlaceType = %s AND abs(LocationX - %s) < 0.0000000000001 AND abs(LocationY - %s) < 0.0000000000001 LIMIT 1""", (place_type, location_x, location_y)) return c.fetchone()[0] except psycopg2.DatabaseError, e: print 'Error %s' % e
def review_exists(reviewer_id, place_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute( "SELECT * FROM Reviews WHERE Reviewer=%s AND PlacesID=%s LIMIT 1", (reviewer_id, place_id)) conn.commit() return True if c.fetchone() else False except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_place_finder(place_id, conn=None): global ID_PLACE persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return "Database Error" c = conn.cursor() try: c.execute("SELECT Finder FROM Places WHERE PlaceId=%s LIMIT 1", (place_id,)) results = c.fetchone() return results[0] if results else None except psycopg2.DatabaseError, e: print 'Error %s' % e
def created_place(user_id, place_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return (False, "Database Error") c = conn.cursor() try: c.execute("SELECT * FROM Places WHERE Finder=%s AND ID=%s LIMIT 1", (user_id, place_id)) exists = c.fetchone() if exists: return True else: return False except psycopg2.DatabaseError, e: print 'Error %s' % e
def get_place_rating(place_id, conn=None): persist_conn = True if not conn: conn = dbhelper.connect() persist_conn = False if conn == None: return (False, "Database Error") c = conn.cursor() try: c.execute("SELECT Rating FROM Places WHERE ID=%s LIMIT 1", (place_id,)) rating = c.fetchone() if rating and rating[0]: return "%.2f" % rating[0] else: return "No rating" except psycopg2.DatabaseError, e: print 'Error %s' % e
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"