Пример #1
0
def check_recommemder():
    form = API_RecommemderForm()

    if form.validate_on_submit():
        return jsonify(recommender=form.recommender)

    return jsonify(message=form.errors)
Пример #2
0
def like(pid):
    exist = current_app.redis.exists('post:%s'%pid)
    if exist:
        current_app.redis.zadd('post:%s:like'%pid, g.user['id'], time.time())
        current_app.redis.zadd('user:%s:like'%g.user['id'], pid, time.time())
        return jsonify(liked=True)
    else:
        return jsonify(status_code=400)
Пример #3
0
def change_password():
    form = API_PasswordFrom()

    if form.validate_on_submit():
        form.save()
        return jsonify(message="success")

    return jsonify(message=form.errors, status_code=400)
Пример #4
0
def api_signin():
    next_url = request.args.get('next', '/')

    form = API_SigninForm()
    if form.validate_on_submit():
        login(form.user)
        return jsonify(user=form.user)

    return jsonify(next_url=next_url, message="error", status_code=400)
Пример #5
0
def api_signup():
    form = API_SignupForm()

    if form.validate_on_submit():
        user = form.save()
        login(user)
        return jsonify(user=user)

    return jsonify(message=form.errors, status_code=400)
Пример #6
0
def update_profile():
    if request.method == 'GET':
        return jsonify(g.user)

    form = API_ProfileForm()
    if form.validate_on_submit():
        form.save()
        return jsonify(message="update profile successfully")
    return jsonify(message=form.errors, status_code=400)
Пример #7
0
def unlike(pid):
    exist = current_app.redis.exists('post:%s'%pid)

    if exist:
        current_app.redis.zrem('post:%s:like'%pid, g.user['id'])
        current_app.redis.zrem('user:%s:like'%g.user['id'], pid)
        return jsonify(liked=False)
    else:
        return jsonify(status_code=400)
Пример #8
0
def unfollow(uid):
    if not current_app.redis.exists('user:%s'%uid) or g.user['id'] == str(uid):
        return jsonify(messge="用户不存在", status_code=400)

    if not current_app.redis.zscore('user:%s:following'%g.user['id'], uid):
        return jsonify(followed=False, message='尚未关注', status_code=400)

    current_app.redis.zrem('user:%s:following'%g.user['id'], uid)
    current_app.redis.zrem('user:%s:follower'%uid, g.user['id'])
    postid_list = current_app.redis.zrange('user:%s:timeline'%uid, 0, -1)
    for pid in postid_list:
        current_app.redis.zrem('user:%s:feed'%g.user['id'], pid)

    return jsonify(followed=False, message="取消关注成功")
Пример #9
0
def follow(uid):
    if not current_app.redis.exists('user:%s'%uid)or g.user['id'] == str(uid):
        return jsonify(messge="用户不存在", status_code=400)

    # check if had followed
    if current_app.redis.zscore('user:%s:following'%g.user['id'], uid):
        return jsonify(followed=True, message="已关注")

    current_app.redis.zadd('user:%s:following'%g.user['id'], uid, time.time())
    current_app.redis.zadd('user:%s:follower'%uid, g.user['id'], time.time())
    postid_list = current_app.redis.zrevrange('user:%s:timeline'%uid, 0, -1)
    for pid in postid_list:
        current_app.redis.zadd('user:%s:feed'%g.user['id'], pid, time.time())

    return jsonify(followed=True, message="关注成功")
Пример #10
0
def recommended():
    invitation_count = current_app.redis.zcard('user:%s:recommended'%g.user['id'])
    since_id = int(request.args.get('since_id', invitation_count))
    page = int(request.args.get('page', 1))
    rev_since_id = invitation_count -since_id + (page-1)*PAGE_SIZE

    recent_invitation_ids = current_app.redis.zrevrange('user:%s:recommended'%g.user['id'],
            rev_since_id,
            rev_since_id+PAGE_SIZE)
    page += 1

    recommended = []
    fields = ['id', 'username', 'photo', 'about']
    for uid in recent_invitation_ids:
        user = current_app.redis.hmget('user:%s'%uid, fields)
        user = dict(zip(fields, user))
        recommended.append(user)

    if request.is_xhr or request.headers.get('X-Clover-Access', None):
        return jsonify(recommended=recommended, since_id=since_id, page=page)

    return render_template('account/recommended.html',
            recommended=recommended,
            since_id=since_id,
            page=page)
Пример #11
0
def signout():
    next_url = request.args.get('next', '/')
    logout()

    if request.headers.get('X-Clover-Access', None):
        return jsonify(message='success')

    flash('已退出')
    return redirect(next_url)
Пример #12
0
def create_item():
    form  = PostForm()

    if form.validate_on_submit():
        post = form.save()
        flash('发布成功')
        return jsonify(postid=post['postid'], message='success')

    return render_template('item/create.html', form=form)
Пример #13
0
def add_comment(pid):
    comment_text = request.form.get('comment', None)

    if not comment_text:
        return jsonify()

    commentid = current_app.redis.hincrby('system', 'comment_id', 1)

    comment = {'text': comment_text,
            'uid':g.user['id'],
            'cid': commentid,
            'pid': pid,
            'publish_time':time.strftime('%Y-%m-%dT%H:%M:%S%z')}

    current_app.redis.hmset('comment:%s'%commentid, comment)
    current_app.redis.zadd('post:%s:comment'%pid, commentid, time.time())
    current_app.redis.zadd('user:%s:comment'%g.user['id'], commentid, time.time())

    return jsonify(comment=comment)
Пример #14
0
def show_item(pid):
    item_comment_count = current_app.redis.zcard('post:%s:comment'%pid)
    since_id = int(request.args.get('since_id', item_comment_count))
    page = int(request.args.get('page', 1))
    rev_since_id = item_comment_count - since_id + (page-1)*PAGE_SIZE

    request_item = current_app.redis.hgetall('post:%s'%pid)
    if not request_item:
        abort(404)
    fields = ['id', 'username', 'photo']
    user = current_app.redis.hmget('user:%s'%request_item['uid'], fields)
    user = dict(zip(fields, user))
    user['listing_count'] = current_app.redis.zcard('user:%s:timeline'%request_item['uid']) or 0

    request_item['likes'] = current_app.redis.zcard('post:%s:like'%pid) or 0
    request_item['comment_count'] = current_app.redis.zcard('post:%s:comment'%pid) or 0
    if current_app.redis.zscore('user:%s:like'%g.user['id'], pid):
        request_item['liked'] = True

    post_commentid_list = current_app.redis.zrevrange('post:%s:comment'%pid, rev_since_id, rev_since_id+PAGE_SIZE)
    page = page + 1
    comments = []
    fields = ['id', 'username', 'photo']
    for commentid in post_commentid_list:
        comment = current_app.redis.hgetall('comment:%s'%commentid)
        comment_user = current_app.redis.hmget('user:%s'%comment['uid'], fields)
        comment['user'] = dict(zip(fields, comment_user))
        comments.append(comment)

    if request.is_xhr:
        return jsonify(request_item=request_item,
                user=user,
                comments=comments,
                since_id=since_id,
                page=page)

    user_postid_list = current_app.redis.zrange('user:%s:timeline'%user['id'], 0, 3)
    user_timeline = get_timeline(user_postid_list)

    return render_template('item/item.html',
            request_item=request_item,
            user=user,
            timeline=user_timeline,
            comments=comments,
            since_id=since_id,
            page=page)
Пример #15
0
def follower(uid):
    follower_count = current_app.redis.zcard('user:%s:follower'%uid)
    since_id = int(request.args.get('since_id', follower_count))
    page = int(request.args.get('page', 1))
    rev_since_id = follower_count - since_id + (page-1)*PAGE_SIZE

    fields = ['id', 'username', 'photo', 'about']
    user = current_app.redis.hmget('user:%s'%uid, fields)
    user = dict(zip(fields, user))

    user['listing_count'] = current_app.redis.zcard('user:%s:timeline'%uid) or 0
    user['following_count'] = current_app.redis.zcard('user:%s:following'%uid) or 0
    user['followed_count'] = current_app.redis.zcard('user:%s:follower'%uid) or 0

    if current_app.redis.zscore('user:%s:follower'%g.user['id'], uid):
        user['followed'] = True

    followedid_list = current_app.redis.zrevrange('user:%s:follower'%uid,
            rev_since_id,
            rev_since_id+PAGE_SIZE)
    page += 1

    followers = []
    for id in followedid_list:
        f = current_app.redis.hmget('user:%s'%id, fields)
        f = dict(zip(fields, f))
        followers.append(f)

    if request.is_xhr or request.headers.get('X-Clover-Access', None):
        return jsonify(user=user,
                followers=followers,
                since_id=since_id,
                page=page)

    return render_template('account/follower.html',
            user=user,
            followers=followers,
            since_id=since_id,
            page=page)
Пример #16
0
def show_comment(pid):
    item_comment_count = current_app.redis.zcard('post:%s:comment'%pid)
    since_id = int(request.args.get('since_id', item_comment_count))
    page = int(request.args.get('page', 1))
    rev_since_id = item_comment_count - since_id + (page-1)*PAGE_SIZE

    request_item = current_app.redis.hgetall('post:%s'%pid)

    if not request_item:
        abort(404)

    post_commentid_list = current_app.redis.zrevrange('post:%s:comment'%pid, rev_since_id, rev_since_id+PAGE_SIZE)
    page = page + 1

    comments = []
    for commentid in post_commentid_list:
        comment = current_app.redis.hgetall('comment:%s' % commentid)
        fields = ['id', 'username', 'photo']
        user = current_app.redis.hmget('user:%s'%comment['uid'], fields)
        comment['user'] = dict(zip(fields, user))
        comments.append(comment)

    return jsonify(comments=comments, since_id=since_id, page=page)