Ejemplo n.º 1
0
def user_check(request):
    username = request.json.get("username")
    password = request.json.get("password")

    session = Session()

    user = session.query(User).filter_by(username=username).one_or_none()
    session.close()
    return user
Ejemplo n.º 2
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.º 3
0
    def get(self, request):
        article_id = request.GET.get("id")
        session = Session()
        article = session.query(Article).filter_by(id=article_id).one_or_none()
        if article is None:
            return redirect("article-list")

        return render(request,
                      "article.html",
                      context={
                          "title": article.title,
                          "author": article.author.username,
                          "create_time": article.create_time,
                          "content": article.content
                      })
Ejemplo n.º 4
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.º 5
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.º 6
0
 def post(self, request):
     action = request.POST.get("action")
     article_id = request.POST.get("article_id")
     user_id = request.session.get("user_id")
     if action and article_id and user_id:
         session = Session()
         article = session.query(Article).filter_by(
             id=article_id).one_or_none()
         if article is None:
             return HttpResponse("article not exist")
         if article.author_id != user_id:
             return HttpResponse("article is not belong to you")
         if action == "DELETE":
             session.delete(article)
             session.commit()
             return redirect("article-list")
         else:
             pass
     else:
         return redirect('article-list')
Ejemplo n.º 7
0
 def get(self, request):
     session = Session()
     articles = session.query(Article).order_by(Article.id.desc())
     return render(request,
                   "article-list.html",
                   context={"articles": articles})