def new_thread(forum, title, isClosed, user, date, message, slug, optional): isDeleted = 0 if "isDeleted" in optional: isDeleted = optional["isDeleted"] thread = dbFunctions.select_query( 'SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' 'FROM Threads WHERE slug = %s', (slug, ) ) if len(thread) == 0: dbFunctions.change_query('INSERT INTO Threads (forum, title, isClosed, user, date, message, slug, isDeleted) ' 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', (forum, title, isClosed, user, date, message, slug, isDeleted, )) thread = dbFunctions.select_query( 'select date, forum, id, isClosed, isDeleted, message, slug, title, user ' 'FROM Threads WHERE slug = %s', (slug, ) ) thread = thread[0] response = { 'date': str(thread[0]), 'forum': thread[1], 'id': thread[2], 'isClosed': bool(thread[3]), 'isDeleted': bool(thread[4]), 'message': thread[5], 'slug': thread[6], 'title': thread[7], 'user': thread[8], } return response
def save_user(email, username, about, name, optional): isAnonymous = 0 if "isAnonymous" in optional: isAnonymous = optional["isAnonymous"] try: user = dbFunctions.select_query('select email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (email, )) if len(user) == 0: dbFunctions.change_query( 'INSERT INTO Users (email, about, name, username, isAnonymous) VALUES (%s, %s, %s, %s, %s)', (email, about, name, username, isAnonymous, )) user = dbFunctions.select_query( 'SELECT email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (email, ) ) except Exception as e: raise Exception(e.message) user = user[0] user_response = { 'about': user[1], 'email': user[0], 'id': user[3], 'isAnonymous': bool(user[2]), 'name': user[4], 'username': user[5] } return user_response
def save_user(email, username, about, name, optional): isAnonymous = 0 if "isAnonymous" in optional: isAnonymous = optional["isAnonymous"] try: user = dbFunctions.select_query( 'select email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (email, )) if len(user) == 0: dbFunctions.change_query( 'INSERT INTO Users (email, about, name, username, isAnonymous) VALUES (%s, %s, %s, %s, %s)', ( email, about, name, username, isAnonymous, )) user = dbFunctions.select_query( 'SELECT email, about, isAnonymous, id, name, username FROM Users WHERE email = %s', (email, )) except Exception as e: raise Exception(e.message) user = user[0] user_response = { 'about': user[1], 'email': user[0], 'id': user[3], 'isAnonymous': bool(user[2]), 'name': user[4], 'username': user[5] } return user_response
def update_thread(id, slug, message): dbFunctions.change_query( 'UPDATE Threads SET slug = %s, message = %s WHERE id = %s', ( slug, message, id, )) return details(id=id, related=[])
def add_follow(email1, email2): follower = dbFunctions.select_query( 'SELECT id FROM Followers WHERE follower = %s AND followee = %s', (email1, email2, ) ) if len(follower) == 0: dbFunctions.change_query('INSERT INTO Followers (follower, followee) VALUES (%s, %s)', (email1, email2, )) user = details(email1) return user
def update_user(email, about, name): dbFunctions.change_query( 'UPDATE Users SET email = %s, about = %s, name = %s WHERE email = %s', ( email, about, name, email, )) return details(email)
def vote(id, vote): if vote == -1: dbFunctions.change_query( 'UPDATE Threads SET dislikes=dislikes+1, points=points-1 where id = %s', (id, ) ) else: dbFunctions.change_query( 'UPDATE Threads SET likes=likes+1, points=points+1 where id = %s', (id, ) ) return details(id=id, related=[])
def vote(id, vote): if vote == -1: dbFunctions.change_query( 'UPDATE Threads SET dislikes=dislikes+1, points=points-1 where id = %s', (id, )) else: dbFunctions.change_query( 'UPDATE Threads SET likes=likes+1, points=points+1 where id = %s', (id, )) return details(id=id, related=[])
def delete_subscription(email, thread_id): subscription = dbFunctions.select_query( 'SELECT thread, user FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, ) ) dbFunctions.change_query( 'DELETE FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, ) ) response = { "thread": subscription[0][0], "user": subscription[0][1] } return response
def remove_follow(email1, email2): follows = dbFunctions.select_query( 'SELECT id FROM Followers WHERE follower = %s AND followee = %s', (email1, email2, ) ) if len(follows) != 0: dbFunctions.change_query( 'DELETE FROM Followers WHERE follower = %s AND followee = %s', (email1, email2, ) ) else: raise Exception("Упс!") return details(email1)
def delete_subscription(email, thread_id): subscription = dbFunctions.select_query( 'SELECT thread, user FROM Subscriptions WHERE user = %s AND thread = %s', ( email, thread_id, )) dbFunctions.change_query( 'DELETE FROM Subscriptions WHERE user = %s AND thread = %s', ( email, thread_id, )) response = {"thread": subscription[0][0], "user": subscription[0][1]} return response
def add_subscribe(email, thread_id): subscription = dbFunctions.select_query( 'SELECT thread, user FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, ) ) if len(subscription) == 0: dbFunctions.change_query( 'INSERT INTO Subscriptions (thread, user) VALUES (%s, %s)', (thread_id, email, ) ) subscription = dbFunctions.select_query( 'select thread, user FROM Subscriptions WHERE user = %s AND thread = %s', (email, thread_id, ) ) response = { "thread": subscription[0][0], "user": subscription[0][1] } return response
def add_follow(email1, email2): follower = dbFunctions.select_query( 'SELECT id FROM Followers WHERE follower = %s AND followee = %s', ( email1, email2, )) if len(follower) == 0: dbFunctions.change_query( 'INSERT INTO Followers (follower, followee) VALUES (%s, %s)', ( email1, email2, )) user = details(email1) return user
def remove_follow(email1, email2): follows = dbFunctions.select_query( 'SELECT id FROM Followers WHERE follower = %s AND followee = %s', ( email1, email2, )) if len(follows) != 0: dbFunctions.change_query( 'DELETE FROM Followers WHERE follower = %s AND followee = %s', ( email1, email2, )) else: raise Exception("Упс!") return details(email1)
def new_post(date, thread, message, user, forum, optional): if len(dbFunctions.select_query("SELECT Threads.id FROM Threads JOIN Forums ON Threads.forum = Forums.short_name " "WHERE Threads.forum = %s AND Threads.id = %s", (forum, thread, ))) == 0: raise Exception("Нет thread с айдишником = " + str(thread) + " в форуме " + forum) if "parent" in optional: if len(dbFunctions.select_query("SELECT Posts.id FROM Posts JOIN Threads ON Threads.id = Posts.thread " "WHERE Posts.id = %s AND Threads.id = %s", (optional["parent"], thread, ))) == 0: raise Exception("Нет поста с айдишником = " + optional["parent"]) query = "INSERT INTO Posts (message, user, forum, thread, date" values = "(%s, %s, %s, %s, %s" parameters = [message, user, forum, thread, date] for param in optional: query += ", " + param values += ", %s" parameters.append(optional[param]) query += ") VALUES " + values + ")" update_posts = "UPDATE Threads SET posts = posts + 1 WHERE id = %s" dbFunctions.change_query(update_posts, (thread, )) post_id = dbFunctions.change_query(query, parameters) post = dbFunctions.select_query('SELECT date, forum, id, isApproved, isDeleted, isEdited, ' 'isHighlighted, isSpam, message, thread, user ' 'FROM Posts WHERE id = %s', (post_id, )) post = post[0] post_response = { 'date': str(post[0]), 'forum': post[1], 'id': post[2], 'isApproved': bool(post[3]), 'isDeleted': bool(post[4]), 'isEdited': bool(post[5]), 'isHighlighted': bool(post[6]), 'isSpam': bool(post[7]), 'message': post[8], 'thread': post[9], 'user': post[10], } return post_response
def add_subscribe(email, thread_id): subscription = dbFunctions.select_query( 'SELECT thread, user FROM Subscriptions WHERE user = %s AND thread = %s', ( email, thread_id, )) if len(subscription) == 0: dbFunctions.change_query( 'INSERT INTO Subscriptions (thread, user) VALUES (%s, %s)', ( thread_id, email, )) subscription = dbFunctions.select_query( 'select thread, user FROM Subscriptions WHERE user = %s AND thread = %s', ( email, thread_id, )) response = {"thread": subscription[0][0], "user": subscription[0][1]} return response
def new_forum(name, short_name, user): """ Создание нового форума """ dbFunctions.exist(entity="Users", identifier="email", value=user) forum = dbFunctions.select_query( 'SELECT id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, ) ) if len(forum) == 0: dbFunctions.change_query('INSERT INTO Forums (name, short_name, user) VALUES (%s, %s, %s)', (name, short_name, user, )) forum = dbFunctions.select_query( 'SELECT id, name, short_name, user FROM Forums WHERE short_name = %s', (short_name, ) ) forum = forum[0] response = { 'id': forum[0], 'name': forum[1], 'short_name': forum[2], 'user': forum[3] } return response
def new_thread(forum, title, isClosed, user, date, message, slug, optional): isDeleted = 0 if "isDeleted" in optional: isDeleted = optional["isDeleted"] thread = dbFunctions.select_query( 'SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' 'FROM Threads WHERE slug = %s', (slug, )) if len(thread) == 0: dbFunctions.change_query( 'INSERT INTO Threads (forum, title, isClosed, user, date, message, slug, isDeleted) ' 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', ( forum, title, isClosed, user, date, message, slug, isDeleted, )) thread = dbFunctions.select_query( 'select date, forum, id, isClosed, isDeleted, message, slug, title, user ' 'FROM Threads WHERE slug = %s', (slug, )) thread = thread[0] response = { 'date': str(thread[0]), 'forum': thread[1], 'id': thread[2], 'isClosed': bool(thread[3]), 'isDeleted': bool(thread[4]), 'message': thread[5], 'slug': thread[6], 'title': thread[7], 'user': thread[8], } return response
def vote(vote_id, vote_type): if vote_type == -1: dbFunctions.change_query("UPDATE Posts SET dislikes=dislikes+1, points=points-1 where id = %s", (vote_id, )) else: dbFunctions.change_query("UPDATE Posts SET likes=likes+1, points=points+1 where id = %s", (vote_id, )) return details(details_id=vote_id, related=[])
def update_thread(id, slug, message): dbFunctions.change_query( 'UPDATE Threads SET slug = %s, message = %s WHERE id = %s', (slug, message, id, ) ) return details(id=id, related=[])
def update_user(email, about, name): dbFunctions.change_query('UPDATE Users SET email = %s, about = %s, name = %s WHERE email = %s', (email, about, name, email, )) return details(email)