Exemple #1
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": "用户不存在"})
Exemple #2
0
def collect():
    data = request.get_json()
    if is_user_login(request):
        user_id = get_user_id_by_cookie(request)
    else:
        return jsonify({'code': 208, 'msg': "登录信息已经过期"})
    article_id = data["article_id"]
    article_result = Article.query.filter(Article.article_id == article_id).first()
    user_result = User.query.filter(User.id == user_id).first()
    collection = Collection.query.filter(Collection.user_id == user_id, Collection.article_id == article_id).first()

    if user_result is None:
        return jsonify({"code": "203", "msg": "该用户不存在"})
    if article_result is None:
        return jsonify({"code": "203", "msg": "该文章不存在"})
    if collection is None:
        collect_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        C = Collection(user_id, article_id, collect_time)
        try:
            db.session.add(C)
            db.session.commit()
            return jsonify({"code": "200", "msg": "已收藏该文章"})
        except:
            db.session.rollback()
            return jsonify({"code": "203", "msg": "收藏该文章失败"})
    else:
        try:
            db.session.delete(collection)
            db.session.commit()
            return jsonify({"code": "200", "msg": "已取消收藏该文章"})
        except:
            db.session.rollback()
            return jsonify({"code": "203", "msg": "取消收藏失败"})
Exemple #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": "用户不存在"})
Exemple #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": "抱歉,用户不存在"}
Exemple #5
0
def update_pwd():
    data = request.get_json()
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    user_id = get_user_id_by_cookie(request)
    update_pwd = data['update_pwd']
    original_pwd = data['original_pwd']
    # 根据用户id查询相关元组,如果该用户存在,继续逻辑判断,否则返回该用户不存在
    user = User.query.filter(User.id == user_id).first()
    if user:
        original_pwd = my_md5(str(original_pwd))
        # 根据传进来的id,原密码和数据库里的数据进行比较,成功继续,否则返回密码不一致
        account = User.query.filter(User.work_id == user.work_id, original_pwd == user.passwd).first()
        # 密码匹配成功
        if account:
            user.passwd = my_md5(str(update_pwd))
            # 新密码和旧密码一样,返回相同密码,否则将新密码提交
            if user.passwd == original_pwd:
                return jsonify({'code': 203, 'msg': "the same password"})
            else:
                db.session.commit()
                uid = request.cookies.get('token')
                # 从redis里删除该用户之前的token
                redis = get_redis_cli()
                value = redis.get("user:"******"user:"******"user:"******"user:"******"update password successfully"})
        else:
            return jsonify({'code': 203, 'msg': "Passwords are inconsistent!"})
    else:
        return jsonify({'code': 203, 'msg': "user_id does't exit"})
Exemple #6
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)
Exemple #7
0
def user_quit():
    redis = get_redis_cli()
    if not is_user_login(request):
        return jsonify({'code': 208, 'msg': '用户信息已过期'})
    user_id = get_user_id_by_cookie(request)
    uuid = redis.get("user:"******"user:"******"user:" + str(uuid))
    return jsonify({'code': 200, 'msg': '用户退出成功'})
Exemple #8
0
def delete_article():
    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 = data["article_id"]
    user = User.query.filter(User.id == user_id).first()
    if user:
        article = Article.query.filter(Article.article_id == article_id, Article.user_id == user.id).first()
        return delete(article)
    else:
        return jsonify({'code': 203, 'msg': 'please login again'})
Exemple #9
0
def get_fans():
    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"]
    user = User.query.filter(User.id == user_id).first()
    if user:
        return query_follow(user_id, myself)
    else:
        return jsonify({'code': 203, 'msg': "user_id does't exit"})
Exemple #10
0
def reward_li():
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    user_id = get_user_id_by_cookie(request)
    result = Reward.query.filter(Reward.user_id == user_id).order_by(
        desc('reward_time')).all()
    arr = []
    for i in result:
        arr.append({
            "reward_time": str(i.reward_time),
            "reward_money": i.reward_money,
            'remark': i.remark
        })
    return jsonify(arr)
Exemple #11
0
def update_in():
    data = request.get_json()
    if not is_user_login(request):
        return jsonify({"code": 208, "msg": "登录信息已经过期"})
    id = get_user_id_by_cookie(request)
    nick_name = data["nick_name"]
    # 自我介绍(简介)
    profile = data["profile"]
    mail = data["mail"]
    avatar = data["avatar"]
    tel = data["tel"]
    redis = get_redis_cli()

    user = User.query.filter(User.id == id).first()
    if user is not None:
        user.nick_name = nick_name
        user.profile = profile
        # mail为 unique
        user.mail = mail
        user.avatar = avatar
        user.tel = tel
        try:
            db.session.commit()
            salt = redis.get("user:"******"id": id,
                "nick_name": nick_name,
                "profile": profile,
                "mail": mail,
                "avatar": avatar,
                "tel": tel,
                "work_id": user.work_id,
                "gender": user.gender,
                "passwd": user.passwd,
                "vir_money": user.vir_money,
                "followers": user.followers,
                "follow": user.follow,
                "profile": user.profile,
                "reward_addr": user.reward_addr,
                "effect": user.effect,
                "status": user.status
            }
            redis.set("user:"******"utf-8"), 604800)
            return jsonify({"code": "200", "msg": "修改成功"})
        except:
            db.session.rollback()
            return jsonify({"code": "203"})
    else:
        # 该用户不存在
        return jsonify({"code": "203"})
Exemple #12
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": "用户或文章不存在"})
Exemple #13
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": "更新成功"})
Exemple #14
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": "参数有误"})
Exemple #15
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": "用户不存在,无法发表此评论"})
Exemple #16
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})