def details(id, related): thread = sql.select_query( 'select date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts ' 'FROM thread WHERE id = %s LIMIT 1;', (id, ) ) if len(thread) == 0: raise Exception('No thread exists with id=' + str(id)) thread = thread[0] thread = { '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], 'dislikes': thread[9], 'likes': thread[10], 'points': thread[11], 'posts': thread[12], } if "user" in related: thread["user"] = user.details(thread["user"]) if "forum" in related: thread["forum"] = forum.details(short_name=thread["forum"], related=[]) return thread
def details(ds, **args): required(['thread'], args) optional('related', args, [], ['user', 'forum']) conn = ds.get_db() db = conn['conn'] c = db.cursor() c.execute(u"""SELECT * FROM thread WHERE id = %s""", (args['thread'],)) thread_data = c.fetchone() c.close() db.close() #ds.close(conn['id']) check_empty(thread_data, u"No thread found with that id") make_boolean(['isClosed', 'isDeleted'], thread_data) thread_data['date'] = str(thread_data['date']) # del thread_data['user_id'] # del thread_data['forum_id'] if 'user' in args['related']: thread_data['user'] = user.details(ds, user=thread_data['user']) if 'forum' in args['related']: thread_data['forum'] = forum.details(ds, forum=thread_data['forum']) return thread_data
def details(ds, **args): required(['post'], args) optional('related', args, [], ['user', 'thread', 'forum']) conn = ds.get_db() db = conn['conn'] c = db.cursor() c.execute(u"""SELECT * FROM post WHERE id = %s""", (args['post'],)) post_data = c.fetchone() c.close() db.close() #ds.close(conn['id']) check_empty(post_data, u"No post found with that id") post_data['date'] = str(post_data['date']) make_boolean(['isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam'], post_data) thread_id = post_data['thread_id'] del post_data['user_id'] del post_data['thread_id'] if 'user' in args['related']: post_data['user'] = user.details(ds, user=post_data['user']) if 'thread' in args['related']: post_data['thread'] = thread.details(ds, thread=thread_id) else: post_data['thread'] = thread_id if 'forum' in args['related']: post_data['forum'] = forum.details(ds, forum=post_data['forum']) return post_data
def details(get_resp=False, **data): query = """SELECT date, dislikes, forum, id, isApproved, isDeleted, isEdited, isHighlighted, isSpam, likes, message, parent, likes - dislikes AS points, thread, user FROM Post WHERE id = %s""" % data['post'] with closing(cnxpool.get_connection()) as db: with closing(db.cursor(dictionary=True)) as cursor: try: cursor.execute(query) post = cursor.fetchone() except MysqlException as e: if get_resp: return response_error(Codes.unknown_error, e) raise e if post is None: post = {} else: if 'related' in data: if 'user' in data['related']: user_data = {'user': post['user']} post['user'] = user.details(**user_data) if 'forum' in data['related']: forum_data = {'forum': post['forum']} post['forum'] = forum.details(**forum_data) if 'thread' in data['related']: thread_data = {'thread': post['thread']} post['thread'] = thread.details(**thread_data) post['date'] = post['date'].strftime("%Y-%m-%d %H:%M:%S") post['isApproved'] = bool(post['isApproved']) post['isDeleted'] = bool(post['isDeleted']) post['isEdited'] = bool(post['isEdited']) post['isHighlighted'] = bool(post['isHighlighted']) post['isSpam'] = bool(post['isSpam']) if get_resp: if len(post) != 0: response = { 'code': Codes.ok, 'response': post } else: str_err = "Post with id=%s not found" % data['post'] response = response_error(Codes.not_found, str_err) return response return post
def details(details_id, related): post = post_query(details_id) if post is None: raise Exception("no post with id = " + details_id) if "user" in related: post["user"] = user.details(post["user"]) if "forum" in related: post["forum"] = forum.details(short_name=post["forum"], related=[]) if "thread" in related: post["thread"] = thread.details(id=post["thread"], related=[]) return post
def posts_list(entity, params, identifier, related=[]): query = "SELECT date, dislikes, forum, id, isApproved, isDeleted, isEdited, isHighlighted, isSpam, likes, message, " \ "parent, points, thread, user FROM post WHERE " + entity + " = %s " parameters = [identifier] if "since" in params: query += " AND date >= %s" parameters.append(params["since"]) query += " ORDER BY date " + params["order"] if "limit" in params: query += " LIMIT " + str(params["limit"]) post_ids = sql.select_query(query=query, params=parameters) post_list = [] for post in post_ids: pf = { 'date': str(post[0]), 'dislikes': post[1], 'forum': post[2], 'id': post[3], 'isApproved': bool(post[4]), 'isDeleted': bool(post[5]), 'isEdited': bool(post[6]), 'isHighlighted': bool(post[7]), 'isSpam': bool(post[8]), 'likes': post[9], 'message': post[10], 'parent': post[11], 'points': post[12], 'thread': post[13], 'user': post[14], } if "user" in related: pf["user"] = user.details(pf["user"]) if "forum" in related: pf["forum"] = forum.details(short_name=pf["forum"], related=[]) if "thread" in related: pf["thread"] = thread.details(id=pf["thread"], related=[]) post_list.append(pf) return post_list
def details(db=0, close_db=True, **data): required(data, ['post']) if db == 0: db = DBconnect.connect() cur = db.cursor() cur.execute("""SELECT * FROM post WHERE id = %s""", (data['post'],)) post = cur.fetchone() cur.close() post['date'] = post['date'].strftime("%Y-%m-%d %H:%M:%S") if 'related' in data: if 'user' in data['related']: post['user'] = user.details(db, False, **post) if 'thread' in data['related']: post['thread'] = thread.details(db, False, **post) if 'forum' in data['related']: short_name = {'forum': post['forum']} post['forum'] = forum.details(db, False, **short_name) if close_db: db.close() return post
def thread_list(entity, identifier, related, params): query = "SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts " \ "FROM thread WHERE " + entity + " = %s " parameters = [identifier] if "since" in params: query += " AND date >= %s" parameters.append(params["since"]) if "order" in params: query += " ORDER BY date " + params["order"] else: query += " ORDER BY date DESC " if "limit" in params: query += " LIMIT " + str(params["limit"]) thread_ids_tuple = sql.select_query(query=query, params=parameters) thread_list = [] for thread in thread_ids_tuple: thread = { '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], 'dislikes': thread[9], 'likes': thread[10], 'points': thread[11], 'posts': thread[12], } if "user" in related: thread["user"] = user.details(thread["user"]) if "forum" in related: thread["forum"] = forum.details(short_name=thread["forum"], related=[]) thread_list.append(thread) return thread_list