コード例 #1
0
ファイル: views.py プロジェクト: chenkaiclyde/kaixinhuilixie
def register():
    '''注册'''
    # 1获取参数
    param_dict = request.json
    username = param_dict.get('username')
    email = param_dict.get('email')
    password = param_dict.get('password')
    repeatpassword = param_dict.get('repeatpassword')
    # 2校验参数
    if not all([email, username, password, repeatpassword]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不能为空')

    # 判断邮箱是否存在
    result = None
    try:
        result = User.query.filter(User.email == email).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据查询错误')
    if result:
        return jsonify(errno=RET.DATAEXIST, errmsg='数据已存在')
    # 判断用户名是否存在
    result = None
    try:
        result = User.query.filter(User.username == username).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据查询错误')
    if result:
        return jsonify(errno=RET.DATAEXIST, errmsg='数据已存在')

    # 初始化User对象添加到数据
    user = User()
    user.username = username
    user.password = password
    user.email = email
    user.mobile = username
    user.gender = 0  # 0代表male,1代表female

    # 密码  加密
    try:
        db.session.add(user)
        db.session.commit()

    except Exception as e:
        current_app.logger.error(e)
        # 回滚
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg='数据保存失败')

    # 5.保存用户的登录状态
    session['user_id'] = user.id
    session['username'] = user.username
    # 6返回相应
    return jsonify(errno=RET.OK, errmsg='注册成功')
コード例 #2
0
def admin_add():
    """新增管理员"""
    if not g.user.is_admin:
        return redirect('/')

    if request.method == 'GET':
        return render_template('admin/admin_add_user.html')

    # 获取post请求参数
    nick_name = request.form.get("nick_name")
    password = request.form.get("password")
    password2 = request.form.get("password2")
    mobile = request.form.get("mobile")
    signature = request.form.get("signature")
    gender = request.form.get("gender")
    avatar_url = request.files.get("avatar_url")

    # 1.1 判断数据是否有值
    if not all([nick_name, password, password2, mobile, gender]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数有误")

    if password != password2:
        return jsonify(errno=RET.PARAMERR, errmsg="两次密码不一致")

    if User.query.filter(User.nick_name == nick_name
                         or User.mobile == mobile).first():
        return jsonify(errno=RET.PARAMERR, errmsg="用户名或者手机号已存在")

    # 创建用户模型对象
    user = User()

    # 1.2 尝试读取图片
    if avatar_url:
        try:
            avatar_image = avatar_url.read()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.PARAMERR, errmsg="参数有误")

        # 2. 将标题图片上传到七牛
        try:
            key = storage(avatar_image)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.THIRDERR, errmsg="上传图片错误")
        user.avatar_url = constants.QINIU_DOMIN_PREFIX + key

    # 3. 设置相关数据
    user.nick_name = nick_name
    user.password = password
    user.mobile = mobile
    user.signature = signature
    user.gender = gender
    user.is_admin = 1

    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据存储失败')

    # 返回结果
    return jsonify(errno=RET.OK, errmsg='管理员用户创建成功')