def DELETE(self, user_id): """ Delete a user """ db = DB() cur = db.connect(settings.DSN) cur.execute("""DELETE FROM pq_user WHERE user_id = %s""", (user_id, )) if cur.rowcount > 0: db.commit() db.disconnect() return { 'result': 'success', 'message': 'User deleted successfully.', } elif cur.rowcount > 1: db.rollback() db.disconnect() return { 'result': 'failure', 'message': 'Delete failed. Attempt was made to delete more than one record.' } else: db.rollback() db.disconnect() return { 'result': 'failed', 'message': 'User delete failed.', }
def DELETE(self, test_id): """ Delete a test """ db = DB() cur = db.connect(settings.DSN) cur.execute("""UPDATE pq_test SET deleted = true WHERE test_id = %s""", (test_id, )) if cur.rowcount == 1: db.commit() db.disconnect() return { 'result': 'success', 'message': 'Test deleted successfully.', } elif cur.rowcount > 1: db.rollback() db.disconnect() return { 'result': 'failure', 'message': 'Delete failed. Attempt was made to delete more than one record.' } else: db.rollback() db.disconnect() return { 'result': 'failed', 'message': 'Test delete failed.', }
def POST( self, username, email, password = None, user_id = None ): # clean if user_id: try: user_id = int(user_id) except: return { 'result': 'failure', 'message': 'Invalid user_id'} # do action = None db = DB() cur = db.connect(settings.DSN) if user_id: if password: pasSQL = 'password = %s,' vals = (username, auth.hash(password), email, user_id) else: pasSQL = '' vals = (username, email, user_id) cur.execute( """UPDATE pq_user SET username = %s, """ + pasSQL + """ email = %s WHERE user_id = %s;""", vals ) action = Updated() else: cur.execute( """INSERT INTO pq_user (username, password, email) VALUES (%s,%s,%s);""", (username, auth.hash(password), email) ) action = Inserted() if cur.rowcount > 0: db.commit() db.disconnect() if type(action) == type(Updated()): return { 'result': 'success', 'message': 'User updated successfully.'} elif type(action) == type(Inserted()): return { 'result': 'success', 'message': 'User added successfully.'} else: return { 'result': 'failure', 'message': 'Something was successful? Add/update reported successful, but we have no idea what happened.'} else: db.rollback() db.disconnect() return { 'result': 'failure', 'message': 'Add/update failed.' }
def POST( self, name, username, password, hostname, database_id = None, port = 5432, active = True ): # do action = None db = DB() cur = db.connect(settings.DSN) if database_id: cur.execute( """UPDATE pq_database SET name = %s, username = %s, password = %s, port = %s, hostname = %s, active = %s WHERE database_id = %s;""", (name, username, password, port, hostname, active, database_id) ) action = Updated() else: cur.execute( """INSERT INTO pq_database (name, username, password, port, hostname, active) VALUES (%s,%s,%s,%s,%s,%s);""", (name, username, password, port, hostname, active) ) action = Inserted() if cur.rowcount > 0: db.commit() db.disconnect() if type(action) == type(Updated()): return { 'result': 'success', 'message': 'Database updated successfully.'} elif type(action) == type(Inserted()): return { 'result': 'success', 'message': 'Database added successfully.'} else: return { 'result': 'failure', 'message': 'Something was successful? Add/update reported successful, but we have no idea what happened.'} else: db.rollback() db.disconnect() return { 'result': 'failure', 'message': 'Add/update failed.' }
def POST( self, test_id = None, name = None, cc = None, schedule_id = None, database_id = None, test_type_id = None, user_id = None, sql = None, python = None, fail_on_no_results = False): """ Insert/update a test """ # do action = None db = DB() cur = db.connect(settings.DSN) #cur.execute("""SELECT TRUE AS exist FROM pq_test WHERE test_id = %s""" % test_id) #res = cur.fetchone() #if res['exist']: if test_id: cur.execute( """UPDATE pq_test SET name = %s, schedule_id = %s, database_id = %s, test_type_id = %s, cc = %s, sql = %s, python = %s, user_id = %s, fail_on_no_results = %s WHERE test_id = %s;""", (name, schedule_id, database_id, test_type_id, cc, sql, python, user_id, fail_on_no_results, test_id) ) action = Updated() else: cur.execute( """INSERT INTO pq_test (name, schedule_id, database_id, test_type_id, cc, sql, python, user_id, fail_on_no_results) VALUES (%s,%s,%s,%s,%s,%s,%s,%s, %s);""", (name, schedule_id, database_id, test_type_id, cc, sql, python, user_id or auth.user_id, fail_on_no_results) ) action = Inserted() if cur.rowcount > 0: db.commit() db.disconnect() if type(action) == type(Updated()): return { 'result': 'success', 'message': 'Test updated successfully.'} elif type(action) == type(Inserted()): return { 'result': 'success', 'message': 'Test added successfully.'} else: return { 'result': 'failure', 'message': 'Something was successful? Add/update reported successful, but we have no idea what happened.'} else: db.rollback() db.disconnect() return { 'result': 'failure', 'message': 'Add/update failed.' }