예제 #1
0
파일: blog.py 프로젝트: BeiMaGang/web_blog
def get_blogs():
    type = int(request.args.get("type"))
    blog_id = int(request.args.get("blog_id", 0))
    DBsession = get_db_session()
    user = getattr(request, "user", {})
    blogger_ids = []
    if type == 0:
        blogger_ids.append(user.get("id"))
    elif type == 1:
        if blog_id == 0:
            concerns = DBsession.query(Concern).filter(
                Concern.user_id == user.get("id")).all()
            for c in concerns:
                blogger_ids.append(c.blogger_id)
        else:
            blogger_ids.append(blog_id)

    if type == 2:
        blogs = DBsession.query(Blog).order_by(Blog.create_time.desc()).all()
    else:
        blogs = DBsession.query(Blog).filter(
            Blog.user_id.in_(blogger_ids)).order_by(
                Blog.create_time.desc()).all()

    blog_ids = [blog.id for blog in blogs]

    blog_dic = get_blogs_info(blog_ids)
    return json.dumps(
        {
            "status": 200,
            "message": "成功",
            "data": list(blog_dic.values())
        },
        ensure_ascii=False)
예제 #2
0
파일: blog.py 프로젝트: BeiMaGang/web_blog
def get_blogs_info(blog_ids):
    DBsession = get_db_session()
    blogs = DBsession.query(Blog).filter(Blog.id.in_(blog_ids)).order_by(
        Blog.create_time.desc()).all()
    blog_dic = {
        blog.id:
        json.loads(json.dumps(blog, cls=AlchemyEncoder, ensure_ascii=False))
        for blog in blogs
    }
    user_ids = [blog.user_id for blog in blogs]
    users = DBsession.query(User).filter(User.id.in_(user_ids)).all()
    user_dic = {user.id: user for user in users}
    for blog in blog_dic.values():
        blog["comments"] = []
        blog["author"] = json.loads(
            json.dumps(user_dic.get(blog["user_id"], {}),
                       cls=AlchemyEncoder,
                       ensure_ascii=False))
    comments = DBsession.query(Comment).filter(
        Comment.blog_id.in_(blog_dic.keys())).order_by(
            Comment.create_time.desc()).all()

    for comment in comments:
        comment_user = DBsession.query(User).filter(
            User.id == comment.user_id).first()
        comment.__setattr__(
            "user", json.loads(json.dumps(comment_user, cls=AlchemyEncoder)))
        blog_dic.get(comment.blog_id).get("comments", []).append(
            json.loads(json.dumps(comment, cls=AlchemyEncoder)))

    return blog_dic
예제 #3
0
def login():
    account = request.form['account']
    passwd = request.form['passwd']
    session = get_db_session()
    user = session.query(User).filter(User.account == account).first()
    if not user:
        return Response(json.dumps({"status": 801, "message": "username不存在"}))
    if passwd != user.passwd:
        return Response(json.dumps({"status": 802, "message": "账户密码不正确"}))
    res = Response(json.dumps({"status": 200, "message": "登陆成功"}))
    session_key = set_session(user)
    res.set_cookie("session_key", session_key)
    return res
예제 #4
0
파일: user.py 프로젝트: BeiMaGang/web_blog
def get_user_info():
    user = getattr(request, "user")
    user["concerns"] = []
    DBsession = get_db_session()
    concerns = DBsession.query(Concern).filter(
        Concern.user_id == user.get("id")).all()
    concern_user_ids = [c.blogger_id for c in concerns]
    concern_users = DBsession.query(User).filter(
        User.id.in_(concern_user_ids)).all()
    for c in concern_users:
        user.get("concerns",
                 []).append(json.loads(json.dumps(c, cls=AlchemyEncoder)))

    return json.dumps({"status": 200, "message": "成功", "data": user})
예제 #5
0
파일: user.py 프로젝트: BeiMaGang/web_blog
def concern_one():
    blogger_id = request.form["blogger_id"]
    user = getattr(request, "user")
    DBsession = get_db_session()
    result = DBsession.query(Concern).filter(
        Concern.user_id == user["id"],
        Concern.blogger_id == blogger_id).first()
    if result:
        return json.dumps({
            "status": 200,
            "message": "已经关注过此人"
        },
                          ensure_ascii=False)

    concern = Concern(user_id=user["id"], blogger_id=blogger_id)
    DBsession.add(concern)
    DBsession.commit()
    return json.dumps({"status": 200, "message": "关注成功"}, ensure_ascii=False)
예제 #6
0
파일: blog.py 프로젝트: BeiMaGang/web_blog
def create_blog():
    user = getattr(request, "user", {})
    title = request.form["title"]
    content = request.form["content"]
    new_blog = Blog(id=generate_id(),
                    user_id=user.get("id"),
                    title=title,
                    content=content)
    DBsession = get_db_session()
    DBsession.add(new_blog)
    DBsession.commit()
    return json.dumps(
        {
            "status": 200,
            "message": "创建成功",
            "data": json.loads(json.dumps(new_blog, cls=AlchemyEncoder))
        },
        ensure_ascii=False)
예제 #7
0
def register():
    account = request.form['account']
    passwd = request.form['passwd']
    nick_name = request.form['nick_name']
    session = get_db_session()
    users = session.query(User).filter(User.account == account).first()

    response = make_response('设置cookie')
    response.set_cookie("user_id", "111")
    if users:
        return json.dumps({"status": 901, "message": "username已经被注册"})
    user = User(
        id=generate_id(),
        account=account,
        passwd=passwd,
        nick_name=nick_name,
    )
    session.add(user)
    session.commit()
    return json.dumps({"status": 200, "message": "注册成功"})
예제 #8
0
def comment_blog():
    user = getattr(request, "user")
    DBsession = get_db_session()
    blog_id = request.form["blog_id"]
    comment = request.form["comment"]
    one = Comment(
        id=generate_id(),
        user_id=user["id"],
        blog_id=blog_id,
        content=comment,
        create_time=datetime.now(),
    )
    DBsession.add(one)
    setattr(one, "user", user)
    DBsession.commit()
    return json.dumps({
        "status": 200,
        "message": "评论成功",
        "data": json.loads(json.dumps(one, cls=AlchemyEncoder))
    }, ensure_ascii=False)
예제 #9
0
파일: blog.py 프로젝트: BeiMaGang/web_blog
def modify_blog():
    user = getattr(request, "user", {})
    blog_id = int(request.form["blog_id"])
    title = request.form["title"]
    content = request.form["content"]
    DBsession = get_db_session()
    blog = DBsession.query(Blog).filter(Blog.id == blog_id).first()
    if user.get("id", 0) != blog.user_id:
        return json.dumps({
            "status": 701,
            "message": "您不是此文章的作者无法修改",
        },
                          ensure_ascii=False)
    blog.title = title
    blog.content = content
    DBsession.commit()

    return json.dumps(
        {
            "status": 200,
            "message": "修改成功",
            "data": json.loads(json.dumps(blog, cls=AlchemyEncoder))
        },
        ensure_ascii=False)