def create_thread(slug_or_id): posts_data = request.get_json() thread = Thread.get_by_slug_or_id_with_forum_id(slug_or_id) usernames = [i['author'] for i in posts_data] authors = User.get_user_ids_by_slug(usernames) posts_ids = set([i['parent'] for i in posts_data if 'parent' in i]) if posts_ids: posts = Post.get_posts_by_id_in_thread(thread['id'], posts_ids) if len(posts) != len(posts_ids): return json_response({'message': 'Parent in other thread'}, 409) if len(authors) != len(set(usernames)): return json_response({'message': 'user not found'}, 404) for post in posts_data: for a in authors: if a['nickname'] == post['author'].lower(): post['author_id'] = a['id'] post['author_nickname'] = a['nickname'] post['parent_id'] = post['parent'] if 'parent' in post else 0 if len(thread) > 0: posts = Post.create_posts(posts_data, thread['id'], thread['forum_id']) else: return json_response({'message': 'thread not found'}, 404) for item in posts: item['created'] = format_time(item['created']) return json_response(posts, 201)
def create_thread(slug): json_data = request.get_json() user = User().get_by_nickname(json_data['author'], hide_id=False) forum = Forum().get_by_slug_with_id(slug) if not (user and forum): return json_response({ "message": "Can't find user or forum" }, 404) if 'slug' in json_data.keys(): thread_exists = Thread.get_serialised_with_forum_user_by_id_or_slug(slug=json_data['slug']) if thread_exists: thread_exists['created'] = format_time(thread_exists['created']) return json_response(thread_exists, 409) thread = Thread.create_and_get_serialized( user_id=user['id'], forum_id=forum['id'], title=json_data['title'], message=json_data['message'], user_nickname=user['nickname'], forum_slug=forum['slug'], created=json_data['created'] if 'created' in json_data else None, slug=json_data['slug'] if 'slug' in json_data else None, ) thread['author'] = user['nickname'] thread['forum'] = forum['slug'] thread['created'] = format_time(thread['created']) return Response( response=json.dumps(thread), status=201, mimetype="application/json" )
def update_thread(slug_or_id): json_data = request.get_json() thread = Thread.get_by_slug_or_id(slug_or_id) if not thread: return json_response({'message': 'Thread not found'}, 404) thread = Thread.update_thread(thread['id'], json_data, thread) thread['created'] = format_time(thread['created']) return json_response(thread, 200)
def update_post(post_id): update_data = request.get_json() post = Post.get_post(post_id) if not post: return json_response({'message': 'post not found'}, 404) post = Post.update_post(post['id'], update_data, post) return json_response(post, 200)
def vote_thread(slug_or_id): post_data = request.get_json() thread_id, user_id = Thread.check_user_and_thread( thread_slug_or_id=slug_or_id, nickname=post_data['nickname']) if not user_id and not thread_id: return json_response({'message': 'Thread OR USER not found'}, 404) thread = Vote.vote_for_thread(user_id, post_data['voice'], thread_id) thread['created'] = format_time(thread['created']) return json_response(thread, 200)
def get_user_profile(nickname): user = User().get_by_nickname(nickname) if not user: return json_response( { 'message': 'Failed to find user with nickname = {0}'.format(nickname) }, 404) return json_response(user, 200)
def get_forum_users(slug): limit = request.args.get('limit') since = request.args.get('since') desc = request.args.get('desc') forum = Forum().get_by_slug_with_id(slug) if not forum: return json_response({ "message": "Can't find forum" }, 404) users = Forum.get_forum_users(forum['id'], limit, since, desc) return json_response(users, 200)
def edit_user_profile(nickname): user, status = User().update_by_nickname(nickname, payload=request.get_json()) if status == DbModel.NOT_FOUND: return json_response( { 'message': 'Failed to find user with nickname = {0}'.format(nickname) }, status) if status == DbModel.CONFLICTS: return json_response( {'message': 'Conflict with user {0}'.format(user['nickname'])}, DbModel.CONFLICTS) return json_response(user, status)
def get_db_status(): data = { 'forum': Forum.get_count(), 'post': Post.get_count(), 'thread': Thread.get_count(), 'user': User.get_count(), } return json_response(data, 200)
def clear_db(): sql_table = """ TRUNCATE TABLE "{0}"; """ sql = "" for item in (User, Forum, Post, Thread): sql += sql_table.format(item.tbl_name) DbConnector().execute_set(sql) return json_response({'status': 'ok'}, 200)
def get_thread_messages(slug_or_id): sort = request.args.get('sort') since = request.args.get('since') limit = request.args.get('limit') desc = request.args.get('desc') thread = Thread.get_by_slug_or_id(slug_or_id) posts = [] if not thread: return json_response({'message': 'Thread not found'}, 404) if sort == "flat" or sort is None: posts = Thread.get_posts_flat_sorted(thread['id'], since, limit, desc) elif sort == "tree": posts = Thread.get_posts_tree_sorted(thread['id'], since, limit, desc) elif sort == "parent_tree": posts = Thread.get_posts_parent_tree_sorter(thread['id'], since, limit, desc) for post in posts: post['created'] = format_time(post['created']) return json_response(posts, 200)
def create_forum(): json_data = request.get_json() user = User().get_by_nickname(json_data['user'], hide_id=False) if not user: return json_response( { "message": "Can't find user with nickname {0}".format(json_data['user']) }, 404 ) if 'slug' in json_data.keys(): forum_exists = Forum().get_by_slug(slug=json_data['slug']) if forum_exists: return json_response(forum_exists, 409) json_data.pop('user') json_data['user_id'] = user['id'] forum = Forum().create(payload=json_data) return json_response( forum, 201, )
def get_threads_list(slug): limit = request.args.get('limit') since = request.args.get('since') desc = request.args.get('desc') threads = Thread.get_threads_list(slug, limit, since, desc) forum = Forum().get_by_slug(slug) for thread in threads: thread['created'] = format_time(thread['created']) if forum: return Response( response=json.dumps(threads), status=200, mimetype="application/json" ) else: return json_response( {'message': 'not found'}, 404 )
def get_thread_details(slug_or_id): thread = Thread.get_by_slug_or_id(slug_or_id) if thread: thread['created'] = format_time(thread['created']) return json_response(thread, 200) return json_response({'message': 'thread not found'}, 404)
def create_thread(post_id): related = request.args.get('related') post = Post.get_info(post_id, related) if not post['post']: return json_response({'message': 'post not found'}, 404) return json_response(post, 200)
def create_user(nickname): user, status = User().create_by_nickname(nickname, payload=request.get_json()) return json_response(user, status)