Esempio n. 1
0
def user_reward():
    data = request.get_json()
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    user_id = get_user_id_by_cookie(request)
    be_reward_user_id = data['be_reward_user_id']
    article_id = data['article_id']
    reward_money = int(data['reward_money'])
    if exist_user(user_id) and exist_user(be_reward_user_id) and exist_article(
            article_id):
        # 货币的添加减少部分
        user = User.query.filter(User.id == user_id).first()
        # 先判断用户是否够钱支付
        if user.vir_money >= reward_money:
            try:
                user.vir_money -= reward_money
                reward_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                            time.localtime())
                article = Article.query.filter(
                    Article.article_id == article_id).first()
                remark = "给文章(" + str(article.title) + ")打赏"
                reward_money1 = "-" + str(reward_money)
                reward = Reward(user_id, be_reward_user_id, reward_money1,
                                reward_time, article_id, remark)
                db.session.add(reward)
                db.session.commit()
                user = User.query.filter(User.id == be_reward_user_id).first()
                user.vir_money += reward_money
                remark1 = "你的文章(" + str(article.title) + ")得到奖励"
                reward1 = Reward(be_reward_user_id, user_id, reward_money,
                                 reward_time, article_id, remark1)
                db.session.add(reward1)
                db.session.commit()
                # 添加进记录表(打赏记录)
                # reward = Reward(user_id, be_reward_user_id, reward_money, reward_time, article_id, remark)
                #  TODO 发通知
                return jsonify({
                    "code": "200",
                    "msg": "打赏成功",
                    "reward_num": get_reward_number(article_id)
                })
            except:
                db.session.rollback()
                return jsonify({"code": "203"})
        else:
            return jsonify({"code": "203", "msg": "金币不足,请充值"})
    else:
        return jsonify({"code": "203", "msg": "用户或文章不存在"})
Esempio n. 2
0
def notice():
    data = request.get_json()
    sort = data['sort']
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    user_id = get_user_id_by_cookie(request)
    if exist_user(user_id):
        arr = []
        redis = get_redis_cli()
        if sort == "0":
            #  状态为0的话,表示查看关注列表
            result = Notice.query.filter(Notice.user_id == user_id, Notice.sort == sort).all()
            status_key = "status:" + "concern:" + str(user_id)
            redis.delete(status_key)
            for e in result:
                arr.append({
                    "notice_title": e.notice_title,
                    "notice_content": e.notice_content,
                    "notice_time": str(e.notice_time),
                    "notice_status": e.notice_status,
                    "sort":sort
                })
                # -1未读 0已读
                if e.notice_status == -1:
                    e.notice_status = 0
                    db.session.commit()
        if sort == "2":
            result = Notice.query.filter(Notice.user_id == user_id, Notice.sort == sort).all()
            status_key = "status:" + "comment:" + str(user_id)
            redis.delete(status_key)
            for e in result:
                arr.append({
                    "notice_title": e.notice_title,
                    "notice_content": e.notice_content,
                    "notice_time": str(e.notice_time),
                    "notice_status": e.notice_status,
                    "sort": sort
                })
                if e.notice_status == -1:
                    e.notice_status = 0
                    db.session.commit()
        if sort == "1":
            result = Notice.query.filter(Notice.user_id == user_id, Notice.sort == sort).all()

            status_key = "status:" + "like:" + str(user_id)
            redis.delete(status_key)
            for e in result:
                arr.append({
                    "notice_title": e.notice_title,
                    "notice_content": e.notice_content,
                    "notice_time": str(e.notice_time),
                    "notice_status": e.notice_status,
                    "sort": sort
                })
                if e.notice_status == -1:
                    e.notice_status = 0
                    db.session.commit()
        return jsonify(arr)
    else:
        return jsonify({"code": "203", "msg": "用户不存在"})
Esempio n. 3
0
def notice_():
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    user_id = get_user_id_by_cookie(request)
    if exist_user(user_id):
        redis = get_redis_cli()
        status_key1 = "status:" + "concern:" + str(user_id)
        status_key2 = "status:" + "comment:" + str(user_id)
        status_key3 = "status:" + "like:" + str(user_id)
        concern_status = 0
        comment_status = 0
        like_status = 0
        if redis.keys(status_key1):
            concern_status = 1
        if redis.keys(status_key2):
            comment_status = 1
        if redis.keys(status_key3):
            like_status = 1
        if (concern_status == 1 or comment_status == 1 or like_status == 1):
            return jsonify({
                "code": "200",
                "msg": "1",
                "concern_status": concern_status,
                "comment_status": comment_status,
                "like_status": like_status
            })
        return jsonify({"code": "200", "msg": "0"})
    else:
        return jsonify({"code": "203", "msg": "用户不存在"})
Esempio n. 4
0
def update_reward():
    file = request.files['file']
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    user_id = get_user_id_by_cookie(request)
    if exist_user(user_id):
        filename = "".join(lazy_pinyin(file.filename))
        b = "."
        redis = get_redis_cli()
        incr = redis.incr('user-reward')
        filename = str(incr) + str(filename[filename.rfind(b):])
        print(filename)
        if file and allowed_file(filename):
            # 这个方法来保证文件名是安全的
            filename = secure_filename(filename)
            file.save(
                os.path.join(app.config['USER_REWARD_UPLOAD_FOLDER'],
                             filename))
            url = "http://192.168.195.10:5005/user/reward/" + filename
            user = User.query.filter(User.id == user_id).first()
            user.reward_addr = url
            db.session.commit()
            return {"code": "200", "msg": "上传成功", "url": url}
        else:
            return {"code": "203", "msg": "上传失败"}
    else:
        return {"code": "203", "msg": "抱歉,用户不存在"}
Esempio n. 5
0
def list():
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    # 拿到当前登录者id
    self_id = get_user_id_by_cookie(request)
    myself = User.query.filter(User.id == self_id).first()
    data = request.get_json()
    user_id = data["user_id"]
    if exist_user(user_id):
        arr = []
        result = Follow.query.filter(Follow.user_id == user_id).all()
        for e in result:
            # 结果为空,不存在此关系
            concern = Follow.query.filter(Follow.followed_id == e.followed_id,
                                          Follow.user_id == myself.id).first()
            # 如果mysql存在此关系。a=2表示我已经关注他,但他没有关注我,1表示互关,0表示我没有关注他
            if concern is None:
                a = 0
            else:
                concern1 = Follow.query.filter(
                    Follow.followed_id == myself.id,
                    Follow.user_id == e.followed_id).first()
                if concern1 is None:
                    a = 2
                else:
                    a = 1
            user = User.query.filter(User.id == e.followed_id).first()
            arr.append({
                'followed_id': e.followed_id,
                'followed_name': user.nick_name,
                'followed_avatar': user.avatar,
                'back_status': a
            })
        return jsonify(arr)
Esempio n. 6
0
def update():
    data = request.get_json()
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    user_id = get_user_id_by_cookie(request)
    article_id = int(data['article_id'])
    title = data['title']
    content = data['content']
    tag_id = data['tag_id']
    # 形如"1,2,3"的str
    category_id = int(data['category_id'])

    if not exist_user(user_id):
        return jsonify({"code": 204, "msg": "用户不存在"})
    if not exist_tag(tag_id):
        return jsonify({"code": 205, "msg": "标签不存在"})
    if not exist_category(category_id):
        return jsonify({"code": 206, "msg": "分类不存在"})
    article = db.session.query(Article).filter_by(
        article_id=article_id).first()
    if article is None:
        return jsonify({"code": 201, "msg": "文章不存在"})

    if article.user_id != user_id:
        return jsonify({"code": 202, "msg": "你不是该文章的作者"})

    # 形如linux-go-docker的字符串
    tag_name, success = split_id_str_to_name(tag_id)
    if not success:
        return jsonify({"code": 203, "msg": "标签参数有误"})

    article.tag_name = tag_name
    article.title = title
    article.content = content
    article.tag_id = tag_id
    article.category_id = category_id
    category = (db.session.query(Category).filter_by(
        category_id=category_id).first())
    if category is None:
        article.category_name = None
    else:
        article.category_name = category.cate_name
    article.update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    db.session.commit()
    return jsonify({"code": "200", "msg": "更新成功"})
Esempio n. 7
0
def deliver_article():
    data = request.get_json()
    if is_user_login(request):
        user_id = get_user_id_by_cookie(request)
    else:
        return jsonify({'code': 208, 'msg': "登录信息已经过期"})
    reward_status = int(data['reward_status'])
    title = data['title']
    content = data['content']
    tag_id = data['tag_id']
    category_id = data['category_id']
    category_name = data['category_name']
    tag_name = data['tag_name']
    if exist_user(user_id) and exist_category(category_id):
        send_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        if title == '' or title.isspace():
            title = None
        if content == '' or content.isspace():
            content = None
        r1 = Article(user_id, title, content, tag_id, category_id, tag_name, send_time, send_time,
                     category_name, reward_status)
        try:
            db.session.add(r1)
            # 作者发布文章,+3积分
            user = User.query.filter(User.id == user_id).first()
            user.effect += 4
            tag_id =str(tag_id)
            ids =tag_id.split(",")
            for i in  ids :
                result=Tag.query.filter(Tag.tag_id == i).first()
                result.tag_heat=int(result.tag_heat) + int(1)
            db.session.commit()
            #print('user.effect:', user.effect)
            return jsonify({"status": "200", "msg": "发布成功"})
        except:
            db.session.rollback()
            return jsonify({"status": "203", "msg": "发布失败"})
    else:
        return jsonify({"status": "203", "msg": "参数有误"})
Esempio n. 8
0
def get_article_detail():
    data = request.get_json()
    article_id = data['article_id']
    user_id = get_user_id_by_cookie(request)
    print(user_id)
    if exist_article(article_id):
        detail = get_detail_from_db(article_id)
        detail.heat = int(detail.heat) + int(1)
        detail.look_num = int(detail.look_num) + 1
        db.session.commit()
        tag_id = detail.tag_id
        tag_id = str(tag_id)
        ids = tag_id.split(",")
        tag_ids = json_for_tag_id(ids)
        name = detail.tag_name
        names = name.split(",")
        tag_names = json_for_tag_name(names)

        # 如果用户是没有登录的状态
        if user_id is None:
            article_detail = serial(detail, 0, tag_ids, tag_names, 0,
                                    is_collection(-1, article_id),
                                    get_user(detail.user_id),
                                    get_reward_number(article_id))
            return jsonify(article_detail)
        # 如果是登录状态
        else:
            if exist_user(user_id):
                concern = Follow.query.filter(
                    Follow.user_id == user_id,
                    Follow.followed_id == detail.user_id).first()
                like = Fabulous.query.filter(
                    Fabulous.user_id == user_id,
                    Fabulous.article_id == article_id).first()
                if concern is None and (like is None
                                        or like.fabulous_status == 0):
                    article_detail = serial(detail, 0, tag_ids, tag_names, 0,
                                            is_collection(user_id, article_id),
                                            get_user(detail.user_id),
                                            get_reward_number(article_id))
                    return jsonify(article_detail)
                elif concern is not None and (like is None
                                              or like.fabulous_status == 0):
                    article_detail = serial(detail, 1, tag_ids, tag_names, 0,
                                            is_collection(user_id, article_id),
                                            get_user(detail.user_id),
                                            get_reward_number(article_id))
                    return jsonify(article_detail)
                elif concern is None and like.fabulous_status != 0:
                    article_detail = serial(detail, 0, tag_ids, tag_names, 1,
                                            is_collection(user_id, article_id),
                                            get_user(detail.user_id),
                                            get_reward_number(article_id))
                    return jsonify(article_detail)
                else:
                    article_detail = serial(detail, 1, tag_ids, tag_names, 1,
                                            is_collection(user_id, article_id),
                                            get_user(detail.user_id),
                                            get_reward_number(article_id))
                    return jsonify(article_detail)
            # 用户不存在和未登录的返回应该是一样的
            else:
                article_detail = serial(detail, 0, tag_ids, tag_names, 0,
                                        is_collection(user_id, article_id),
                                        get_user(detail.user_id),
                                        get_reward_number(article_id))
                return jsonify(article_detail)
    else:
        return jsonify({"code": "203", "msg": "抱歉,文章不存在"})
Esempio n. 9
0
def send_comment():
    data = request.get_json()
    print(data)
    if is_user_login(request):
        user_id = get_user_id_by_cookie(request)
    else:
        return jsonify({'code': 208, 'msg': "登录信息已经过期"})
    article_id = data['article_id']
    comment_id = data['comment_id']
    be_comment_user_id = data['be_comment_user_id']
    com_content = data['com_content']
    redis = get_redis_cli()
    user = User.query.filter(User.id == user_id).first()
    user_name = user.nick_name
    if exist_user(user_id) and exist_article(article_id):
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        comment_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        if be_comment_user_id == "null":
            comment = Comment(None, user_id,
                              comment_id, article_id, comment_time, com_content, None)
            db.session.add(comment)
            # 评论文章,个人+1积分
            user = User.query.filter(User.id == user_id).first()
            user.effect += 1
            db.session.commit()
            result = Article.query.filter(Article.article_id == article_id).first()
            result.heat = int(result.heat) + int(5)
            # 找出作者的id
            author_user_id = result.user_id
            notice_content = str(user_name) + "评论了你的文章:" + str(result.title)
            notice_title = "你收到了一条评论"
            n1 = Notice(None, author_user_id, notice_title, notice_content, 0, comment_time, -1, 2, user_id)
            db.session.add(n1)
            # 有人评论,作者积分+2
            author = User.query.filter(User.id == author_user_id).first()
            author.effect += 3
            db.session.commit()
            status_key = "status:" + "comment:" + str(author_user_id)
            if redis.keys(status_key):
                print(redis.keys(status_key))
            else:
                redis.set(status_key, 1)
        else:
            if exist_user(be_comment_user_id):
                comment = Comment(None, user_id,
                                  comment_id, article_id, comment_time, com_content, be_comment_user_id)
                db.session.add(comment)
                db.session.commit()
                notice_title = "你收到了一条回复"
                notice_content = str(user_name) + "回复了你的评论"
                n1 = Notice(None, be_comment_user_id, notice_title, notice_content, 0, comment_time, -1, 2, user_id)
                db.session.add(comment)
                db.session.add(n1)
                db.session.commit()
                status_key = "status:" + "comment:" + str(be_comment_user_id)
                if redis.keys(status_key):
                    print(redis.keys(status_key))
                else:
                    redis.set(status_key, 1)
            else:
                return jsonify({"code": "203", "msg": "被评论用户不存在,无法发表此评论"})
        # 评论数加1,并更新到redis
        result = db.session.query(Article).get(article_id)
        result.comment_num = result.comment_num + 1
        db.session.commit()
        return jsonify({"code": "200", "msg": "评论成功"})
    else:
        return jsonify({"code": "203", "msg": "用户不存在,无法发表此评论"})
Esempio n. 10
0
def like():
    data = request.get_json()
    # 参数:用户id 文章id 点赞状态 点赞数(?)
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    user_id = get_user_id_by_cookie(request)
    if not exist_user(user_id):
        return {"code": 201, "msg": "用户不存在"}
    article_id = int(data['article_id'])
    # action == 0 -> 取消点赞, 1 -> 点赞
    action = int(data['like_action'])
    redis = get_redis_cli()
    time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    # 是否已经点赞
    liked = db.session.query(Fabulous).filter_by(user_id=user_id, article_id=article_id).first()
    article = db.session.query(Article).filter_by(article_id=article_id).first()
    if article is None:
        # 并没有该文章, 报错
        return jsonify({"code": 500, "msg": "该文章不存在"})
    # 作者id
    author_id = article.user_id
    if not exist_user(author_id):
        return {"code": 203, "msg": "作者不存在"}
    author = User.query.filter(User.id == author_id).first()
    # 用户
    user = User.query.filter(User.id == user_id).first()
    notice_title = "你有新的一条点赞信息"
    notice_content = "用户:" + str(user.nick_name) + "点赞了你的文章:" + str(article.title)
    status_key = "status:" + "like:" + str(article.user_id)
    result = Notice.query.filter(Notice.user_id == author_id, Notice.initiative_id == user_id,
                                 Notice.sort == '1').first()
    if liked is not None:
        if action == 1:
            if liked.fabulous_status == 0:
                liked.fabulous_status = -1
                article.fabulous_num += 1
                # 有人点赞,作者+1积分
                author.effect += 2
                # 点赞,个人+1积分
                user.effect += 1
                db.session.commit()
                like_count = db.session.query(Article).filter_by(article_id=article_id).first().fabulous_num
                # 发送推送
                # 找出被点赞的文章作者的id
                # 点赞热度文章加3
                article.heat = int(article.heat) + int(3)
                if result is None:
                   n1 = Notice(None, article.user_id, notice_title, notice_content, 0, time1, -1, 1,user_id)
                   db.session.add(n1)
                else:
                   result.notice_content = "用户:" + str(user.nick_name) + "点赞了你的文章:" + str(article.title)
                db.session.commit()
                if redis.keys(status_key):
                    redis.incr(status_key)
                else:
                    redis.set(status_key, 1)
                return jsonify({"code": 200, "msg": "点赞成功", "like_count": like_count})
            return jsonify({"code": 204, "msg": "已经点过赞"})
        else:
            if liked.fabulous_status == 1 or liked.fabulous_status == -1:
                article.fabulous_num -= 1
                liked.fabulous_status = 0
                # 有人取消点赞,作者-1积分
                author.effect -= 2
                # 取消点赞,个人-1积分
                user.effect -= 1
                article.heat -= 3
                db.session.commit()
                redis_result=redis.get(status_key)
                if int(redis_result) == 1 :
                    redis.delete(status_key)
                if int(redis_result) > 1:
                    redis.decr(status_key)
            return jsonify({"code": 205, "msg": "取消点赞成功"})
    else:
        if action == 1:
            new_like = Fabulous(None, user_id=user_id, article_id=article_id,
                                fabulous_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), fabulous_status=1)
            db.session.add(new_like)
            article.fabulous_num += 1
            #  有人点赞,作者+1积分
            author.effect += 2
            db.session.commit()
            like_count = db.session.query(Article).filter_by(article_id=article_id).first().fabulous_num
            # TODO 发推送
            # 点赞热度文章加3
            article.heat = int(article.heat) + int(3)
            if result is None:
                n1 = Notice(None, article.user_id, notice_title, notice_content, 0, time1, -1, 1, user_id)
                db.session.add(n1)
            else:
                result.notice_content = "用户:" + str(user.nick_name) + "点赞了你的文章:" + str(article.title)
            db.session.commit()
            if redis.keys(status_key):
                redis.incr(status_key)
            else:
                redis.set(status_key, 1)
        else:
            return jsonify({"code": 203, "msg": "没有点赞无法取消点赞"})

    return jsonify({"code": 200, "msg": "点赞成功", "like": like_count})