示例#1
0
def post(token_data):
    '''threadを作成
    Args:
        title:          スレッドタイトル
        category_id:    カテゴリID
        comment_text:        1コメ
    Returns:
        dict:
            作成されたthread情報
    '''
    try:
        params = request.json

        new_thread = Thread.post(
            title=params.get('title'),
            category_id=params.get('category_id')
        )

        user_id = token_data.get('user_id')

        user = User.get(user_id)

        data = {
            'name': user.get('nick_name'),
            'thread_id': new_thread.get('thread_id'),
            'text': params.get('comment_text'),
            'user_id': user_id,
        }

        Comment.post(data)

        result = Thread.get(new_thread.get('thread_id'))

        return jsonify(result)
    except Exception as e:
        if str(e) == 'over title length':
            result = {
                'error_message': 'タイトルが長すぎます'
            }
            return make_response(jsonify(result), 400)

        return make_response('', 500)
    def test_post(self):
        '''新しいthread作成
        '''
        self.load_fixtures()

        thread = Thread()

        actual = thread.post(title='title24',
                             category_id=1,
                             params={
                                 'create_at': datetime.now(JST),
                                 'update_at': datetime.now(JST),
                             })
        expect = {
            'thread_id': 24,
            'title': 'title24',
            'create_at': datetime(2018, 1, 24, 0, 0),
            'update_at': datetime(2018, 1, 24, 0, 0),
            'speed': 0,
            'comment_count': 0,
            'category_id': 1,
        }

        self.assertDictEqual(expect, actual)