Ejemplo n.º 1
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)
Ejemplo n.º 2
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': '用户退出成功'})
Ejemplo n.º 3
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'})
Ejemplo n.º 4
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"})
Ejemplo n.º 5
0
def before_request():
    user_id = get_user_id_by_cookie(request)
    if user_id is not None:
        user = User.query.filter(User.id == user_id).first()
        # 如果今天还是首次登录,就加金币,改状态
        if user.first_login == 1:
            user.vir_money += 5
            user.first_login = 0
            # 添加记录
            reward_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            # 如果打赏用户是0,article_id 也是0, 说明是系统打赏的
            reward = Reward(user_id, user_id, 5, reward_time, 0, "系统打赏")
            db.session.add(reward)
        db.session.commit()
Ejemplo n.º 6
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)
Ejemplo n.º 7
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"})
Ejemplo n.º 8
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": "用户或文章不存在"})
Ejemplo n.º 9
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": "更新成功"})
Ejemplo n.º 10
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": "参数有误"})
Ejemplo n.º 11
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": "抱歉,文章不存在"})
Ejemplo n.º 12
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": "用户不存在,无法发表此评论"})
Ejemplo n.º 13
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})