Ejemplo n.º 1
0
    def get(self, request):
        user_id = request.session.get("user_id")
        username = request.session.get("username")

        if username and user_id:
            return JsonResponse({"user_id": user_id, "username": username})
        else:
            return ErrorResponse(403, "User not login.")
Ejemplo n.º 2
0
    def get(self, request):
        article_id = request.GET.get("article_id")
        session = Session()
        if article_id is None:
            articles = session.query(Article).order_by(Article.id.desc()).all()
            rs = []
            for i in articles:
                _dict = i.serialize()
                _dict["author_name"] = i.author.username
                rs.append(_dict)

            return JsonResponse({"articles": rs})

        article = get_by_pk(session, Article, article_id)
        if article is None:
            return ErrorResponse(404, "Article not found")
        rsp = article.serialize()
        rsp["author_name"] = article.author.username
        return JsonResponse(rsp)
Ejemplo n.º 3
0
    def delete(self, request):
        user_id = request.session.get("user_id")
        username = request.session.get("username")

        if user_id:
            del request.session["user_id"]
        if username:
            del request.session["username"]

        return JsonResponse({})
Ejemplo n.º 4
0
    def post(self, request):
        username = request.json.get("username")
        password = request.json.get("password")

        session = Session()
        user = session.query(User).filter_by(username=username,
                                             password=password).one_or_none()
        if user is None:
            return ErrorResponse(401, "Username or password wrong.")

        request.session["user_id"] = user.id
        request.session["username"] = user.username

        return JsonResponse({})
Ejemplo n.º 5
0
    def delete(self, request):
        user_id = request.session.get("user_id")
        if user_id is None:
            return ErrorResponse(401, "Login first")

        form = JsonForm(request.json, ("article_id", ))
        if not form.is_valid():
            return form.error_resp()
        session = Session()
        article = get_by_pk(session, Article, form.article_id)
        if article is None:
            return ErrorResponse(404, "Article not found.")
        if article.author_id != user_id:
            return ErrorResponse(403, "The article is not belong to you.")

        session.delete(article)
        session.commit()
        return JsonResponse({})
Ejemplo n.º 6
0
    def post(self, request):
        print(request.json)
        form = JsonForm(request.json, ("username", "password"),
                        username=lambda x: len(x) > 5,
                        password=lambda x: len(x) > 5)
        if not form.is_valid():
            return form.error_resp()

        session = Session()

        user = session.query(User).filter_by(
            username=form.username).one_or_none()
        if user is not None:
            return ErrorResponse(401, "Username already exist.")

        user = User(username=form.username, password=form.password)
        session.add(user)
        session.commit()
        return JsonResponse({})
Ejemplo n.º 7
0
    def post(self, request):
        user_id = request.session.get("user_id")
        if user_id is None:
            return ErrorResponse(401, "Login first")

        form = JsonForm(request.json, ("title", "content"))
        if not form.is_valid():
            return form.error_resp()

        title = form.title
        content = form.content
        author_id = user_id
        article = Article(title=title,
                          content=md2html(content),
                          author_id=author_id)

        session = Session()
        session.add(article)
        session.commit()

        return JsonResponse({})
Ejemplo n.º 8
0
    def put(self, request):
        user_id = request.session.get("user_id")
        if user_id is None:
            return ErrorResponse(401, "Login first")

        form = JsonForm(request.json, ("id", "title", "content"))
        if not form.is_valid():
            return form.error_resp()

        title = form.title
        content = form.content
        session = Session()
        article = get_by_pk(session, Article, form.id)
        if article is None:
            return ErrorResponse(404, "Article not found.")
        if article.author_id != user_id:
            return ErrorResponse(403, "The article is not belong to you.")

        session.add(article)
        article.title, article.content = title, content
        session.commit()

        return JsonResponse({})