Пример #1
0
def user_register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = UserRegisterForm()
    if not form.validate_on_submit():
        return render_template('register.html', form=form, testing=app.testing)
    else:
        username = form.username.data
        nickname = form.nickname.data
        password = form.password1.data
        email = form.email1.data
        print(username, nickname, password, email, flush=True)
        try:
            User.create(username=username,
                        nickname=nickname,
                        password=password,
                        email=email)
        except IntegrityError as e:
            app.logger.warning(f"Integrity Error: {e}")
            crx_flash('ERROR_USER_EXISTS')
            return redirect(url_for('user_register'))
        except Exception as e:
            app.logger.warning(f"Unknown Error: {e}")
            crx_flash('ERROR_UNKNOWN')
            return redirect(url_for('user_register'))

        app.logger.info(f"User created: {username}, {nickname}, {email}")
        crx_flash('ACCOUNT_CREATED', username)
        return redirect(url_for('user_login'))
Пример #2
0
def register():
    form = UserRegisterForm()

    if request.method == 'GET':
        return render_template('register.html', form=form)

    if request.method == 'POST':
        # 校验传递的username,pw1,pw2是必填值,且长度要限制,账号是否存在判断
        # 1. 定义form表单
        if form.validate_on_submit():
            # 账号密码全部校验成功
            username = form.username.data
            password = form.pw1.data
            icon = form.icon.data
            # 保存账号信息
            user = User()
            user.username = username
            user.password = generate_password_hash(password)
            if icon:
                # 保存图片,且保存图片字段
                BASE_DIR = os.path.dirname(
                    os.path.dirname(os.path.abspath(__file__)))
                STATIC_DIR = os.path.join(BASE_DIR, 'static')
                MEDIA_DIR = os.path.join(STATIC_DIR, 'media')
                # 保存图片的绝对路径
                icon_path = os.path.join(MEDIA_DIR, icon.filename)
                icon.save(icon_path)
                # 保存图片的相对路径
                user.icons = icon.filename

            db.session.add(user)
            db.session.commit()
        return render_template('register.html', form=form)
Пример #3
0
def registrar_usuario():
    form = UserRegisterForm()
    if form.validate_on_submit():
        nombre = form.nombre.data
        email = form.email.data
        password = form.password.data

        nuevo_usuario = Usuario(nombre=nombre, email=email, password=password)

        db.session.add(nuevo_usuario)
        db.session.commit()

        app.logger.info(f'{datetime.now()}: User {nuevo_usuario} registered')

        flash('Estas registrado!', 'success')
        return redirect(url_for('home'))

    return render_template('registrar.html',
                           barra_busqueda=True,
                           titulo='Registrarse',
                           form=form,
                           debug=app.config['DEBUG'])
Пример #4
0
def register():
    # 表单对象
    form = UserRegisterForm()
    if request.method == 'GET':

        return render_template('register.html', form=form)

    if request.method == 'POST':
        # 验证提交的字段信息
        if form.validate_on_submit():
            username = form.username.data
            password = form.password.data
            # 实现注册,保存用户信息到User模型中
            user = User()
            user.username = username
            user.password = generate_password_hash(password)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for('app.login'))
        else:
            # 验证失败, form.errors中存在错误信息
            return render_template('register.html', form=form)