def list_threads(): request_data = get_json(request) if 'user' in request_data: code, response = Thread.list_by_user(request_data['user'], request_data) elif 'forum' in request_data: code, response = Thread.list_by_forum(request_data['forum'], request_data) else: code, response = Codes.NOT_CORRECT, 'Please enter user or forum' return json.dumps( {'code': code, 'response': response} )
def list_threads(): request_data = get_json(request) if "related" not in request_data: request_data["related"] = None code, response = Thread.list_by_forum(request_data["forum"], request_data, request_data["related"]) return json.dumps({"code": code, "response": response})
def remove(): request_data = get_json(request) code, response = Thread.remove(request_data['thread']) return json.dumps( {'code': code, 'response': response} )
def open_thread(): request_data = get_json(request) code, response = Thread.open(request_data['thread']) return json.dumps( {'code': code, 'response': response} )
def update(): request_data = get_json(request) code, response = Thread.update(request_data['thread'], request_data['message'], request_data['slug']) return json.dumps( {'code': code, 'response': response} )
def unsubscribe(): request_data = get_json(request) code, response = Thread.unsubscribe(request_data['thread'], request_data['user']) return json.dumps( {'code': code, 'response': response} )
def details(): request_data = get_json(request) if 'related' not in request_data: request_data['related'] = None code, response = Thread.details(request_data['thread'], request_data['related']) return json.dumps( {'code': code, 'response': response} )
def list_threads(): request_data = get_json(request) if 'related' not in request_data: request_data['related'] = None code, response = Thread.list_by_forum(request_data['forum'], request_data, request_data['related']) return json.dumps( {'code': code, 'response': response} )
def create(): request_data = get_json(request) request_data['title'] = request_data['title'].encode('utf-8') columns = ', '.join(request_data.keys()) values = get_values(request_data) code, response = Thread.create(columns, values) return json.dumps( {'code': code, 'response': response} )
def details(post_id, related=None): if not related: related = [] code, result = Post.get_post(post_id) if code != Codes.OK: return code, result if code == Codes.OK and 'user' in related: code, result['user'] = User.details(result['user']) if code == Codes.OK and 'forum' in related: code, result['forum'] = Forum.details(result['forum']) if code == Codes.OK and 'thread' in related: code, result['thread'] = Thread.details(result['thread']) return code, result
def list_by_forum(forum, request, related=None): if not related: related = [] if 'order' not in request: request['order'] = 'DESC' columns = 'p.id, p.message, p.date, p.likes, p.dislikes, p.points, p.isApproved, ' \ 'p.isHighlighted, p.isEdited, p.isSpam, p.isDeleted, p.forum, p.thread, p.user, p.parent' if 'forum' in related: columns += ', f.id f_id, f.name f_name, f.short_name f_short_name, f.user f_user' if 'thread' in related: columns += ', t.id t_id, t.title t_title, t.slug t_slug, t.message t_message, t.date t_date, ' \ 't.posts t_posts, t.likes t_likes, t.dislikes t_dislikes, t.points t_points, ' \ 't.isClosed t_isClosed, t.isDeleted t_isDeleted, t.forum t_forum, t.user t_user' query = Query.SELECT.format( columns=columns, table=Tables.TABLE_NAMES['POST'] + ' as p' ) if 'forum' in related: query += Query.JOIN.format( table=Tables.TABLE_NAMES['FORUM'] + ' as f', clause='p.forum = f.short_name' ) if 'thread' in related: query += Query.JOIN.format( table=Tables.TABLE_NAMES['THREAD'] + ' as t', clause='p.thread = t.id' ) query += Query.WHERE.format( clause='p.forum = %r' % str(forum) ) if 'since' in request: query += Query.AND_CLAUSE.format( clause='p.date > %r' % str(request['since']) ) query += Query.ORDER_BY.format( column='p.date', type=request['order'] ) if 'limit' in request: query += Query.LIMIT.format( count=request['limit'] ) connection = db_connect.get_connection() try: cursor = connection.cursor() cursor.execute(query) posts = dictfetchall(cursor) except Exception as e: return Codes.UNKNOWN_ERROR, str(e) finally: connection.close() response = [] code = Codes.OK for post in posts: if 'forum' in related: post['forum'] = {} keys = [] for val in post: if val[:2] == 'f_': post['forum'][val[2:]] = post[val] keys.append(val) for key in keys: del post[key] if 'thread' in related: post['thread'] = {} keys = [] for val in post: if val[:2] == 't_': post['thread'][val[2:]] = post[val] keys.append(val) for key in keys: del post[key] post['thread'] = Thread.get_response_values(post['thread']) if 'user' in related: code, post['user'] = User.details(post['user']) response.append(Post.get_response_values(post)) return code, response