def restore_remove(connection, thread, isDeleted): posts = 0 current_state = db_tools.execute_select(connection, "SELECT isDeleted FROM Threads WHERE id = %s", (thread,))[0][0] if current_state != isDeleted: if isDeleted == 0: query = 'SELECT COUNT(id) FROM Posts WHERE thread = %s' params = (thread, ) posts = db_tools.execute_select(connection, query, params)[0][0] query_thread = 'UPDATE Threads SET isDeleted = %s, posts = %s WHERE id = %s' query_post = 'UPDATE Posts SET isDeleted = %s WHERE thread = %s' params_thread = (isDeleted, posts, thread, ) try: db_tools.execute_update(connection, query_thread, params_thread) db_tools.execute_update(connection, query_post, (isDeleted, thread, )) except Exception as e: print (e.message) response = {"thread": thread} return response
def update(connection, message, slug, thread): query = 'UPDATE Threads SET slug = %s, message = %s WHERE id = %s' params = (slug, message, thread, ) try: db_tools.execute_update(connection, query, params) except Exception as e: print (e.message) return details(connection, id=thread, related=[])
def dec_posts(connection, post): query = 'SELECT thread FROM Posts WHERE id = %s' params = (post, ) thread = db_tools.execute_select(connection, query, params)[0][0] query = 'UPDATE Threads SET posts = posts - 1 WHERE id = %s' params = (thread, ) db_tools.execute_update(connection, query, params) return
def vote(connection, vote, post): if vote == 1: query = "UPDATE Posts SET likes = likes + 1, points = points + 1 WHERE id = " + \ str(post) else: query = "UPDATE Posts SET dislikes = dislikes + 1, points = points - 1 WHERE id = " + \ str(post) db_tools.execute_update(connection, query, ()) return
def set_post_path(connection, post, post_id): update_path = "UPDATE Posts SET path = %s WHERE id = %s" parent_path = "SELECT path FROM Posts WHERE id = %s" if post["parent"] is None: path = enlength(str(post_id)) else: parent_path = db_tools.execute_select(connection, parent_path, (post["parent"],)) path = "".join(parent_path[0]) + "." + enlength(str(post_id)) db_tools.execute_update(connection, update_path, (path, post_id,))
def unsubscribe(connection, user, thread): query = 'DELETE FROM Subscribe WHERE thread = %s AND user = %s' params = (thread, user, ) try: db_tools.execute_update(connection, query, params) except Exception as e: print (e.message) result = {"thread": thread, "user": user} return result
def open_close(connection, thread, isClosed): query = 'UPDATE Threads SET isClosed = %s WHERE id = %s' params = (isClosed, thread, ) try: db_tools.execute_update(connection, query, params) except Exception as e: print (e.message) response = {"thread": thread} return response
def vote(connection, vote, thread): if vote == -1: query = 'UPDATE Threads SET dislikes = dislikes + 1, points = points - 1 WHERE id = %s' else: query = 'UPDATE Threads SET likes = likes + 1, points = points + 1 WHERE id = %s' params = (thread, ) try: db_tools.execute_update(connection, query, params) except Exception as e: print (e.message) return details(connection, id=thread, related=[])
def create(connection, forum, title, isClosed, user, date, message, slug, optional): isDeleted = 0 if "isDeleted" in optional: isDeleted = optional["isDeleted"] query = 'INSERT INTO Threads (forum, title, isClosed, user, date, message, slug, isDeleted) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)' params = (forum, title, isClosed, user, date, message, slug, isDeleted, ) thread = db_tools.execute_update(connection, query, params) if thread == "Error": raise Exception("Thread already exists") response = { 'date': str(date), 'forum': forum, 'id': thread, 'isClosed': bool(isClosed), 'isDeleted': bool(isDeleted), 'message': message, 'slug': slug, 'title': title, 'user': user, } return response
def create(connection, username, about, name, email, optional): isAnonymous = 0 if "isAnonymous" in optional: isAnonymous = optional["isAnonymous"] query = 'INSERT INTO Users (username, about, name, email, isAnonymous) VALUES (%s, %s, %s, %s, %s)' params = ( username, about, name, email, isAnonymous, ) inserted_id = db_tools.execute_update(connection, query, params) if inserted_id == "Error": raise Exception("5") user = db_tools.execute_select( connection, 'SELECT id, email, about, isAnonymous, name, username FROM Users WHERE email = %s', (email, )) return serialize_u(user)
def restore_remove(connection, post, isDeleted): current_state = db_tools.execute_select(connection, "SELECT isDeleted FROM Posts WHERE id = %s", (post,))[0][0] query = "UPDATE Posts SET isDeleted = %s WHERE id = %s" params = (isDeleted, post) if current_state != isDeleted: if isDeleted == 0: thread_tools.inc_posts(connection, post) else: thread_tools.dec_posts(connection, post) db_tools.execute_update(connection, query, params) response = {"post": post} return response
def follow(connection, email1, email2): query = 'INSERT INTO Follow (follower, followee) VALUES (%s, %s)' params = ( email1, email2, ) response = db_tools.execute_update(connection, query, params) return details(connection, email1)
def unfollow(connection, email1, email2): query = 'DELETE FROM Follow WHERE follower = %s AND followee = %s' params = ( email1, email2, ) response = db_tools.execute_update(connection, query, params) return details(connection, email1)
def subscribe(connection, user, thread): query = 'INSERT INTO Subscribe (thread, user) VALUES (%s, %s)' params = (thread, user, ) subscriptions = [] try: db_tools.execute_update(connection, query, params) except Exception as e: print (e.message) query = 'SELECT thread, user FROM Subscribe WHERE thread = %s AND user = %s' try: subscriptions = db_tools.execute_select(connection, query, params) except Exception as e: print (e.message) result = {"thread": subscriptions[0][0], "user": subscriptions[0][1]} return result
def update_profile(connection, about, user_email, name): query = 'UPDATE Users SET about = %s, name = %s WHERE email = %s' params = ( about, name, user_email, ) response = db_tools.execute_update(connection, query, params) return details(connection, user_email)
def create(connection, name, short_name, user): query = 'INSERT INTO Forums (name, short_name, user) VALUES (%s, %s, %s)' params = (name, short_name, user, ) inserted_id = db_tools.execute_update(connection, query, params) query = 'SELECT id, name, short_name, user FROM Forums WHERE short_name = %s' params = (short_name, ) forum = db_tools.execute_select(connection, query, params) response = serialize_f(forum) return response
def update(connection, post, message): query = "UPDATE Posts SET message = %s WHERE id = %s" params = (message, post, ) db_tools.execute_update(connection, query, params) return details(connection, details_id=post, related=[])