Exemplo n.º 1
0
def add():
    user_info = request.get_json()
    user = User.query.filter_by(username=user_info['username']).first()
    if user:
        # 用户已经存在,不能添加
        raise APIException(500, "用户已经存在")
    user = User()
    user.nickname = user_info['nickname']
    user.username = user_info['username']
    user.password = user_info['password']
    user.create_time = datetime.datetime.now()
    user.status = 1
    user.add()
    return {"code": 200}
Exemplo n.º 2
0
    def post(self):
        """create new user"""
        data = SignUp.parser.parse_arg()
        username = data["username"]
        email = data["email"]
        password = data["password"]

        validate = validators.Validators()
        """validate user data before submission"""

        if not validate.valid_name("username"):
            return {
                "status": 400,
                "message": "please provide a valid username"
            }, 400
        if not validate.valid_email("email"):
            return {
                "status": 400,
                "message": "please provide a valid email"
            }, 400
        if not validate.valid_password("password"):
            return {
                "status":
                400,
                "message":
                "password should start with a capital letter, "
                "include a number and should be not less than 8 characters long"
            }, 400
        if User().fetch_by_username(username):
            return {"status": 400, "message": "user already exist"}, 400
        user = User(username, email, password)

        user_exist = User().get_user_by_email(email)
        if user_exist:
            return {"status": 400, "message": "This user already exists"}, 400
        User.add()
        expires = datetime.timedelta(minutes=60)
        token = create_access_token(identity=user.serialize(),
                                    expires_delta=expires)

        user_exist = User().get_user_by_email(email)

        return {
            "message": "Account created successfully",
            "access_token": token,
            "user": user_exist.serialize()
        }
Exemplo n.º 3
0
def register():
    regForm = registerForm()
    if regForm.validate_on_submit():
        if session['recaptcha'].lower() == regForm.recaptcha.data.lower():
            session['username'] = regForm.username.data
            query_obj = User(regForm.username.data, regForm.password.data)
            if (query_obj.isExisted()):
                flash(u'此用户名已经被使用,请重新注册!')
                del query_obj
                return render_template('register.html', form=regForm)
            else:
                user = User(regForm.username.data, regForm.password.data)
                user.add()
                flash(u'恭喜您,注册成功,您现在可以登录!')
                return redirect(url_for('index'))
        else:
            flash(u'Hi, 验证码错误, 请重输!')
    return render_template('register.html', form=regForm)
Exemplo n.º 4
0
def create_admin():
    """create an administrator"""
    user = User(username='******', email='*****@*****.**', password='******', is_admin=True)
    user.add()