Beispiel #1
0
def user_info():
    user = g.user
    if not user:
        # 代表没有登录,重定向到首页
        return redirect('/')
    data = {"user": user.to_dict()}
    return render_template('news/user.html', data=data)
Beispiel #2
0
def pic_info():
    user = g.user
    if request.method == "GET":
        return render_template('news/user_pic_info.html',
                               data={"user": user.to_dict()})

    # 如果是post表示是修改头像
    # 1、取到上传的图片
    try:
        avatar = request.files.get("avatar").read()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    # 2、上传头像
    try:
        # 使用自己封装的storage方法去进行图片上传
        key = storage(avatar)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="上传头像失败")

    # 3、保存头像地址
    user.avatar_url = key
    return jsonify(errno=RET.OK,
                   errmsg="OK",
                   data={"avatar_url": constants.QINIU_DOMIN_PREFIX + key})
Beispiel #3
0
def pic_info():
    user = g.user
    if request.method == "GET":
        return render_template('news/user_pic_info.html',
                               data={"user_info": user.to_dict()})

    # 1. 获取到上传的文件
    try:
        avatar_file = request.files.get('avatar').read()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="读取文件出错!")

    # 2. 将文件上传到七牛云
    try:
        url = storage(avatar_file)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.THIRDERR, errmsg="上传图片错误!")

    # 3. 将头像信息更新到当前用户的模型类中
    user.avatar_url = url
    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存用户数据错误!")

    # 4. 返回上传的结果<avatar_url>
    return jsonify(errno=RET.OK,
                   errmsg="OK!",
                   data={"avatar_url": constants.QINIU_DOMIN_PREFIX + url})
Beispiel #4
0
def other_info():
    """查看其他用户信息"""
    user = g.user

    # 获取其他用户id
    user_id = request.args.get("id")
    if not user_id:
        abort(404)
    # 查询用户模型
    other = None
    try:
        other = User.query.get(user_id)
    except Exception as e:
        current_app.logger.error(e)
    if not other:
        abort(404)

    # 判断当前登录用户是否关注过该用户
    is_followed = False
    if g.user:
        if other.followers.filter(User.id == user.id).count() > 0:
            is_followed = True

    # 组织数据,并返回
    data = {
        "user_info": user.to_dict(),
        "other_info": other.to_dict(),
        "is_followed": is_followed
    }
    return render_template('news/other.html', data=data)
Beispiel #5
0
def get_user_info():
    """
    获取用户信息
    :return:
    """
    user = g.user
    if not user:
        return redirect('/')

    data = {
        "user_info": user.to_dict(),
    }
    # 渲染模板
    return render_template("news/user.html", data=data)
Beispiel #6
0
def base_info():
    from info import db
    """
    用户基本信息
    1. 获取用户登录信息
    2. 获取到传入参数
    3. 更新并保存数据
    4. 返回结果
    :return:
    """

    # 1. 获取当前登录用户的信息
    user = g.user
    if request.method == "GET":
        return render_template('news/user_base_info.html',
                               data={"user": user.to_dict()})

    # 2. 获取到传入参数
    data_dict = request.json
    nick_name = data_dict.get("nick_name")
    gender = data_dict.get("gender")
    signature = data_dict.get("signature")

    print(nick_name)
    print(signature)
    print(gender)
    if not all([nick_name, gender, signature]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")

    if gender not in (['MAN', 'WOMAN']):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")

    # 3. 更新并保存数据
    user.nick_name = nick_name
    user.gender = gender
    user.signature = signature
    # try:
    #     db.session.commit()
    # except Exception as e:
    #     current_app.logger.error(e)
    #     db.session.rollback()
    #     return jsonify(errno=RET.DBERR, errmsg="保存数据失败")

    # 将 session 中保存的数据进行实时更新
    session["nick_name"] = nick_name

    # 4. 返回响应
    return jsonify(errno=RET.OK, errmsg="更新成功")
Beispiel #7
0
def get_user_info():
    """
    获取用户信息
    1. 获取到当前登录的用户模型
    2. 返回模型中指定内容
    :return:
    """

    user = g.user
    if not user:
        # 用户未登录,重定向到主页
        return redirect('/')

    data = {
        "user_info": user.to_dict(),
    }
    # 渲染模板
    return render_template("news/user.html", data=data)
Beispiel #8
0
def news_detail(news_id):
    # print(123456465465)

    user = g.user
    news_click_list = News.query.order_by(
        News.clicks.desc()).limit(CLICK_RANK_MAX_NEWS)
    click_news_list = []
    for news in news_click_list if news_click_list else []:
        click_news_list.append(news.to_dict())

    news = News.query.get(news_id)

    is_collected = False
    is_followed = False
    comment_list = Comment.query.filter(Comment.news_id == news.id).order_by(
        Comment.create_time.desc()).all()
    comments = []
    if user:
        if news in user.collection_news:
            is_collected = True
        if news.user in user.followed:
            is_followed = True
        user_comment_likes = CommentLike.query.filter(
            CommentLike.user_id == user.id).all()
        user_comment_ids = [
            comment_like.comment_id for comment_like in user_comment_likes
        ]
        for comment in comment_list if comment_list else []:
            comment_dict = comment.to_dict()
            comment_dict['is_like'] = False
            if comment.id in user_comment_ids:
                comment_dict['is_like'] = True
            comments.append(comment_dict)
    data = {
        'news_dict': news_click_list,
        'user': user.to_dict() if user else None,
        'news': news.to_dict() if news else None,
        'is_collected': is_collected,
        'comments': comments,
        'is_followed': is_followed,
    }

    return render_template('news/detail.html', data=data)
Beispiel #9
0
def pic_info():
    user = g.user
    return render_template('news/user_pic_info.html',
                           data={"user_info": user.to_dict()})
Beispiel #10
0
def news_detail(news_id):
    # 根据新闻id来查询该新闻模型
    news = None  # type: News
    try:
        news = News.query.get(news_id)
    except BaseException as e:
        current_app.logger.error(e)

    if not news:
        return abort(404)

    # 让新闻的点击量+1
    news.clicks += 1

    # 查询新闻 按照点击量的倒序排列 取前10条
    rank_list = []
    try:
        rank_list = News.query.order_by(
            News.clicks.desc()).limit(CLICK_RANK_MAX_NEWS).all()
    except BaseException as e:
        current_app.logger.error(e)

    rank_list = [news.to_basic_dict() for news in rank_list]

    # 查询当前用户是否收藏了该新闻
    is_collected = False
    user = g.user
    if user:
        if news in user.collection_news:  # 当执行了懒查询的关系属性和in联用时(if in / for in), 会直接执行查询, 而不需要添加all()
            is_collected = True

    # 查询该新闻的所有评论,传到模板中
    # comments = [comment.to_dict() for comment in news.comments]
    # comments = Comment.query.filter(Comment.news_id == news.id).order_by(Comment.create_time.desc()).all()
    comments = news.comments.order_by(Comment.create_time.desc()).all()

    # 查询当前用户是否对某条评论点过赞
    comments_list = []
    for comment in comments:
        is_like = False
        comment_dict = comment.to_dict()
        if user:
            if comment in user.like_comments:
                is_like = True
            comment_dict["is_like"] = is_like
        # 将评论字典加入列表中
        comments_list.append(comment_dict)

    # 查询当前登录用户是否关注了新闻的作者
    is_followed = False
    # 判断是否登录以及新闻是否有作者
    if user and news.user:
        # 判断作者是否被关注
        if news.user in user.followed:
            is_followed = True

    # 将用户登录信息传到模板中
    user = user.to_dict() if user else None

    # 将模型数据传到模板中
    return render_template("news/detail.html",
                           news=news.to_dict(),
                           rank_list=rank_list,
                           user=user,
                           is_collected=is_collected,
                           comments=comments_list,
                           is_followed=is_followed)