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_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 add_user_report(reporter_id, reported_id, reason, conn=None): global ID_REPORTS_USERS, USER_REPORT_LIMIT ruid = dbhelper.generate_id(ID_REPORTS_USERS) if not ruid[0]: return (False, "UUID error") 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("""INSERT INTO ReportsUsers (ID, ReportID, ReporterId, ReportedId, Reason) VALUES (%s, %s, %s, %s, %s)""", (ruid[1], ruid[1], reporter_id, reported_id, reason)) conn.commit() if get_num_reports_for_user(reported_id) >= USER_REPORT_LIMIT: update_user_disabled(reported_id, True) return (True, "User report successful") except psycopg2.DatabaseError, e: print 'Error %s' % e
def add_user_report(reporter_id, reported_id, reason, conn=None): global ID_REPORTS_USERS, USER_REPORT_LIMIT ruid = dbhelper.generate_id(ID_REPORTS_USERS) if not ruid[0]: return (False, "UUID error") 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( """INSERT INTO ReportsUsers (ID, ReportID, ReporterId, ReportedId, Reason) VALUES (%s, %s, %s, %s, %s)""", (ruid[1], ruid[1], reporter_id, reported_id, reason)) conn.commit() if get_num_reports_for_user(reported_id) >= USER_REPORT_LIMIT: update_user_disabled(reported_id, True) return (True, "User report successful") except psycopg2.DatabaseError, e: print 'Error %s' % e
def add_temporary_url(uid, url_type, conn=None): global TEMP_URL_EXPIRY_TIME expire_temporary_urls() if get_temporary_url_timeout_pending(uid, url_type)[0]: return (False, "The temporary url timeout has not expired") uuid = dbhelper.generate_id(ID_USER) if not uuid[0]: return (False, "UUID error") 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("""INSERT INTO TemporaryUrls VALUES(%s, %s, NOW(), INTERVAL %s, %s, %s)""", (uuid[1], uuid[1], TEMP_URL_EXPIRY_TIME, url_type, uid)) conn.commit() return (True, uuid[1]) except psycopg2.DatabaseError, e: print 'Error %s' % e
def add_temporary_url(uid, url_type, conn=None): global TEMP_URL_EXPIRY_TIME expire_temporary_urls() if get_temporary_url_timeout_pending(uid, url_type)[0]: return (False, "The temporary url timeout has not expired") uuid = dbhelper.generate_id(ID_USER) if not uuid[0]: return (False, "UUID error") 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( """INSERT INTO TemporaryUrls VALUES(%s, %s, NOW(), INTERVAL %s, %s, %s)""", (uuid[1], uuid[1], TEMP_URL_EXPIRY_TIME, url_type, uid)) conn.commit() return (True, uuid[1]) except psycopg2.DatabaseError, e: print 'Error %s' % e
def add_place_report(reporter_id, reported_id, reason, conn=None): global ID_REPORTS_PLACES, PLACE_REPORT_LIMIT ruid = dbhelper.generate_id(ID_REPORTS_PLACES) if not ruid[0]: return (False, "UUID error") 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("""INSERT INTO ReportsPlaces (ID, ReportID, ReporterId, ReportedId, Reason) VALUES (%s, %s, %s, %s, %s)""", (ruid[1], ruid[1], reporter_id, reported_id, reason)) conn.commit() if get_num_reports_for_place(reported_id) >= PLACE_REPORT_LIMIT: remove_place_by_id(reported_id) # TODO perhaps, add an intermediary disabled state, rather than # automatically removing the place return (True, "Place report successful") except psycopg2.DatabaseError, e: print 'Error %s' % e