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 list_posts(**data): # Приходит forum, а для list_posts нужен thread posts = post.list_posts(**data) if len(posts) == 0: # Знатный костыль, но так требует серв return response_error(Codes.ok, "") if 'related' in data: if 'user' in data['related']: for one_post in posts: user_data = {'user': one_post['user']} one_post['user'] = user.details(**user_data) if 'forum' in data['related']: for one_post in posts: forum_data = {'forum': one_post['forum']} one_post['forum'] = details(**forum_data) if 'thread' in data['related']: for one_post in posts: thread_data = {'thread': one_post['thread']} one_post['thread'] = thread.details(**thread_data) return { 'code': Codes.ok, 'response': posts }
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 listPosts(**data): required(data, ['forum']) if 'order' not in data: data['order'] = 'desc' query = StringToFile.StringToFilefunc() params = () query.append("""SELECT * FROM post WHERE thread in (SELECT id FROM thread WHERE forum = %s)""") params += (data['forum'],) if 'since' in data: query.append(""" AND date >= %s""") params += (data['since'],) query.append(""" ORDER BY date %s""" % data['order']) if 'limit' in data: query.append(""" LIMIT %s""" % data['limit']) db = DBconnect.connect() cur = db.cursor() cur.execute(str(query), params) posts = cur.fetchall() cur.close() for post in posts: 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']) post['date'] = post['date'].strftime("%Y-%m-%d %H:%M:%S") if 'related' in data: if 'thread' in data['related']: thread_data = {'thread': post['thread']} thread_data = thread.details(db, False, **thread_data) post['thread'] = thread_data if 'user' in data['related']: user_data = {'user': post['user']} user_data = user.details(db, False, **user_data) post['user'] = user_data if 'forum' in data['related']: forum_data = {'forum': post['forum']} forum_data = details(db, False, **forum_data) post['forum'] = forum_data return posts
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