def list_users(short_name, optional): query = "SELECT user.id, user.email, user.name, user.username, user.isAnonymous, user.about FROM user " \ "WHERE user.email IN (SELECT DISTINCT user FROM post WHERE forum = %s)" if "since_id" in optional: query += " AND user.id >= " + str(optional["since_id"]) if "order" in optional: query += " ORDER BY user.name " + optional["order"] if "limit" in optional: query += " LIMIT " + str(optional["limit"]) con = sql.connect() cursor = con.cursor(sql.db.cursors.DictCursor) cursor.execute(query, (short_name, )) users_tuple = [i for i in cursor.fetchall()] for user_sql in users_tuple: cursor.execute("""SELECT `thread` FROM `subscription` WHERE `user` = %s;""", (user_sql['email'], )) sub = [i['thread'] for i in cursor.fetchall()] followers = follower.list_followers(cursor, user_sql['email']) following = follower.list_following(cursor, user_sql['email']) user_sql.update({'following': following, 'followers': followers, 'subscriptions': sub}) cursor.close() return users_tuple
def create(date, thread, message, user, forum, optional): try: query = "INSERT INTO post (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]) except Exception as e: print e.message query += ") VALUES " + values + ")" update_thread_posts = "UPDATE thread SET posts = posts + 1 WHERE id = %s" con = sql.connect() with con: cursor = con.cursor() cursor.execute(update_thread_posts, (thread, )) cursor.execute(query, parameters) con.commit() post_id = cursor.lastrowid cursor.close() post = post_query(post_id) del post["dislikes"] del post["likes"] del post["parent"] del post["points"] return post