示例#1
0
 def get(self):
     quotes = model.Quote.query.all()
     if quotes:
         return base_response(
             data=quotes[random.randint(0,
                                        len(quotes) - 1)].json_str())
     return base_response(code=-1, msg="没有查询到结果")
示例#2
0
    def post(self):
        param = parser.parse_args()
        if not param.email:
            return base_response(code=-2)

        if not param.pwd:
            return base_response(code=-2)

        user = model.User.query.filter_by(email=param.email).first()
        if not user:
            return base_response(code=-1, msg="未找到用户,请注册")
        if user.verify_password(param.pwd):
            token = generate_token(param.email)
            # 保存token
            login_token = model.LoginToken.query.filter_by(token=token).first()
            if not login_token:
                new_login_token = model.LoginToken(token=token)
                new_login_token.user = user
                new_login_token.user = user
                db.session.add(new_login_token)
            else:
                login_token.update_time = datetime.now
                db.session.add(login_token)
            db.session.commit()
            return base_response(data={
                "token": token,
                "user": user.json_str()
            })
        else:
            return base_response(code=-1, msg="密码错误")
示例#3
0
    def post(self):
        param = parser.parse_args()
        # if not param.user_name:
        #     return base_response(code=-2)

        if not param.email:
            return base_response(code=-2)

        if not param.code:
            return base_response(code=-2)

        if not param.pwd:
            return base_response(code=-2)

        if not param.confirm_pwd:
            return base_response(code=-2)

        # if not param.phone:
        #     return base_response(code=-2)

        if param.pwd != param.confirm_pwd:
            return base_response(code=-1, msg="两次密码输入不一致")

        right_code = model.EmailCode.query.filter_by(email=param.email).first()
        if not right_code:
            return base_response(code=-1, msg="请重新发送验证码")
        # valid the email code is right
        if right_code.code != param.code:
            return base_response(code=-1, msg="验证码错误")
        else:
            user = model.User(email=param.email)
            user.hash_password(param.pwd)
            db.session.add(user)
            db.session.commit()
            db.session.refresh(user)

            ## 返回token
            token = generate_token(param.email)
            print(user.json_str())
            # 保存token
            login_token = model.LoginToken(token=token)
            login_token.user = user
            db.session.add(login_token)

            db.session.commit()

            return base_response(data={
                "token": token,
                "user": user.json_str()
            })
示例#4
0
    def post(self):
        param = parser.parse_args()
        token = request.headers['Authorization']
        login_token = model.LoginToken.query.filter_by(token=token).first()
        user = login_token.user
        if not user:
            return base_response(code=-1, msg="用户不存在")

        if not param.title:
            return base_response(code=-2)
            
        if not param.content:
            return base_response(code=-2)

        if not param.cate_id:
            return base_response(code=-2)
    
        blog = model.Blog(title=param.title, content=param.content, category_id=param.cate_id)
        blog.user = user

        all_tags = model.Tag.query.all()
        # 最终博客的tags
        tags = []
        
        if len(param.tag_names) != 0:
            # 循环参数的tag名
            for tag_name in param.tag_names:
                # 判断是否有该tag
            for index in all_tags:
                    if tag_name == all_tags[index].tag_name:
                        tags.append(all_tags[index])
                    else:
                        new_tag = model.Tag(tag_name=tag_name)
                        tags.append(new_tag)

            blog.tags = tags
            
        db.session.add(blog)
        db.session.commit()
        return base_response()
示例#5
0
    def get(self, email):
        if not email:
            return base_response(code=-2)
        user = model.User.query.filter_by(email=email).first()
        if user:
            return base_response(code=-1, msg="邮箱地址已经被注册了")
        # genarate 6 random code
        rand_code = "".join([str(random.randint(0, 9)) for n in range(6)])

        # send email code...
        sender = EmailUtil(email)
        sender.send_email_code(rand_code)
        # save code to db
        emailCode = model.EmailCode.query.filter_by(email=email).first()
        if emailCode:
            emailCode.code = rand_code
        else:
            emailCode = model.EmailCode(code=rand_code, email=email)
            db.session.add(emailCode)

        db.session.commit()
        return base_response()
示例#6
0
 def decorated_function(*args, **wkargs):
     token = request.headers['Authorization']
     login_token = model.LoginToken.query.filter_by(token=token).first()
     if not login_token:
         return base_response(code=-1, msg="未登陆")
     return f(*args, **wkargs)
示例#7
0
 def get(self, title):
     blog = model.Blog.query.filter_by(title=title).first()
     if blog:
         return base_response(data=blog.json_str())
     return base_response(msg="没有找到文章")
示例#8
0
 def get(self, blog_id):
     blog = model.Blog.query.filter_by(id=blog_id).first()
     return base_response(data=blog.json_str())
示例#9
0
 def get(self):
     blogs = model.Blog.query.all()
     if blogs:
         return base_response(data=[blog.json_str() for blog in blogs])
     return base_response(code=-1, msg="没有文章")
示例#10
0
 def get(self):
     categorys = model.Category.query.all()
     if categorys:
         return base_response(data=[category.json_str() for category in categorys])
     return base_response(code=-1, msg="没有找到分类")