Esempio n. 1
0
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)