def get_all_followers(user_id):
    user = UserDetails.query.filter_by(user_id=user_id).first()
    if user is None:
        raise CustomException('Cannot find user', 404)
    user_followers = user.followers.paginate(0, 19, error_out=False)
    ret = list(map(lambda d: d.follower.to_short_json(), user_followers.items))
    total_followers = user_followers.total
    return jsonify({
        'user_id': user_id,
        'user_name': user.name,
        'followers': ret,
        'total_followers': total_followers
    }), 200
def get_post_by_id(post_id):
    post_current = Post.query.filter_by(post_id=post_id).first()
    cur_user_id = request.args.get('user_current_id')
    if cur_user_id is None:
        end_point = '/user_profile?profile_id=' + str(post_current.author_id)
    else:
        end_point = '/user_profile?profile_id=' + str(post_current.author_id) + '&my_user_id=' + str(cur_user_id)
    with get_connection(post, name='profile') as conn:
        resp = conn.get(ServiceURL.PROFILE_SERVICE + end_point)
        if resp.status_code != 200:
            raise CustomException('Cannot found post', 404)
    if post_current is None:
        raise CustomException('Cannot found post', 404)
    post_current.num_views += 1
    db.session.add(post_current)
    db.session.commit()
    ret = post_current.to_json_full(resp.json())
    ret['is_liked'] = False
    if cur_user_id is not None:
        like = post_current.like.filter_by(user_id=cur_user_id).first()
        if like is not None:
            ret['is_liked'] = True
    return jsonify(ret), 200
Exemple #3
0
def search_post():
    type = request.args.get('type')
    key_word = request.args.get('key_word')
    page = int(request.args.get('page', '0'))
    itemPerPage = 20
    if type == 'title':
        posts = Post.query.filter(Post.title.like(f'%{key_word}%')) \
            .order_by(Post.date_post.desc()) \
            .paginate(page, itemPerPage, error_out=False)
    else:
        posts = Post.query.filter(db.or_(Post.body.like(f'%{key_word}%'), Post.title.like(f'%{key_word}%'))) \
            .order_by(Post.date_post.desc()) \
            .paginate(
            page, itemPerPage, error_out=False)
    post_paginated = posts.items
    total = posts.total
    num_page = total // itemPerPage + 1
    list = [str(x.author_id) for x in post_paginated]
    str_list = ','.join(set(list))
    with get_connection(post, name='profile') as conn:
        resp = conn.get(ServiceURL.PROFILE_SERVICE +
                        'list_user_profile?list=' + str_list)
        if resp.status_code != 200:
            raise CustomException('Cannot found post', 404)
    data = resp.json().get('profile')
    list_post = []
    data_index = [x.get('user_id') for x in data]
    for element in post_paginated:
        index = data_index.index(element.author_id)
        json = element.to_json_summary(data[index])
        json['is_liked'] = False
        current_user_id = request.args.get('user_current_id')
        if current_user_id is not None:
            like = element.like.filter_by(user_id=current_user_id).first()
            if like is not None:
                json['is_liked'] = True
        list_post.append(json)
    return jsonify({
        'Post': list_post,
        'page': page,
        'itemPerPage': itemPerPage,
        'total_pages': num_page
    }), 200
Exemple #4
0
def add_comment(user_id):
    data = request.get_json()
    body = data.get('body')
    html = markdown(body)
    list_text = BeautifulSoup(html, features='html.parser').find_all(text=True)
    body_text = '.'.join(list_text)
    comment = Comments(body=body_text,
                       body_html=body,
                       post_id=data.get('post_id'),
                       date_comment=datetime.utcnow(),
                       user_id=user_id)
    with get_connection(post, name='profile') as conn:
        resp = conn.get(ServiceURL.PROFILE_SERVICE +
                        'user_profile?profile_id=' + str(comment.user_id))
        if resp.status_code != 200:
            raise CustomException('Cannot found post', 404)
    db.session.add(comment)
    db.session.commit()
    ret = comment.to_json(resp.json(), 0)
    return jsonify(ret), 200
Exemple #5
0
def get_most_post():
    date_before_now = timedelta(days=7)
    previous_date = datetime.utcnow() - date_before_now
    sql_str = 'select posts.post_id, posts.title, case ' \
              'when count(posts.post_id) = 1 and like_table.post_id is null then 0 ' \
              'else count(posts.post_id) ' \
              'end ' \
              '* 10 ' \
              '+ posts.num_views  as reputation, posts.num_views,posts.author_id, count(like_table.post_id) from posts ' \
              'left join like_table on posts.post_id = like_table.post_id ' \
              'where posts.date_post > :date_before ' \
              'group by posts.post_id ' \
              'order by reputation desc ' \
              'limit 5'
    result = db.session.execute(sql_str, {'date_before': previous_date})
    result1 = [x for x in result]
    list_post = []
    list_author_id = set(map(lambda d: str(d[4]), result1))
    str_list = ','.join(list_author_id)
    with get_connection(post, name='profile') as conn:
        resp = conn.get(ServiceURL.PROFILE_SERVICE +
                        'list_user_profile?list=' + str_list)
        if resp.status_code != 200:
            raise CustomException('Cannot found post', 404)
    data = resp.json().get('profile')
    data_index = [int(x.get('user_id')) for x in data]
    for element in result1:
        author_id = element[4]
        index = data_index.index(author_id)
        json_ret = {
            'post_id': element[0],
            'title': element[1],
            'reputation': element[2],
            'num_views': element[3],
            'author': data[index],
            'num_like': element[5],
        }
        list_post.append(json_ret)
    return jsonify({'post': list_post})
def get_user_profile():
    user_id = request.args.get('profile_id')
    my_user_id = request.args.get('my_user_id')
    user_details = UserDetails.query.filter_by(user_id=user_id).first()
    if user_details is None:
        raise CustomException('Cannot found User', status_code=404)
    resp = user_details.to_json()
    resp['is_followed'] = False
    if my_user_id is not None:
        my_user = UserDetails.query.filter_by(user_id=my_user_id).first()
        if my_user is not None:
            if my_user.is_following(user_id):
                resp['is_followed'] = True
            else:
                resp['is_followed'] = False
    with get_connection(profile, name='verify_jwt') as conn:
        resp_profile = conn.get(ServiceURL.POST_SERVICE + str(user_id) +
                                '/total_posts')
        if resp_profile.status_code != 200:
            resp['total_posts'] = 0
        else:
            resp['total_posts'] = resp_profile.json().get('total_posts')
    return jsonify(resp), 200
def get_all_followeds(user_id):
    user = UserDetails.query.filter_by(user_id=user_id).first()
    if user is None:
        raise CustomException('Cannot find user', 404)
    ret = list(map(lambda d: d.followed.to_short_json(), user.followed.all()))
    return jsonify(ret), 200
def test():
    raise CustomException('Test', 404)