예제 #1
0
def create_user(username, password, identity_word='normal'):
    """
    创建用户。
    :param username: 用户名
    :param password: 密码
    :param identity_word: 身份
    :return: 操作结果
    """
    if User.objects.filter(username=username).exists():
        return operation_failed(InfoType.Exists, InfoField.User)
    if identity_word not in UserIdentity.IDENTITY_VALUE:
        return operation_failed(InfoType.NotExists, InfoField.IdentityWord)

    for letter in username:
        if not (letter.isalpha() or letter.isdigit() or letter == '_'):
            return operation_failed(InfoType.Invalid, InfoField.Username)

    user = User(username=username)
    user.set_password(password)
    user.save()

    profile = UserProfile(user=user)
    profile.username = username
    profile.register_time = int(time.time())
    profile.save()

    identity = UserIdentity(user=user)
    identity.level = UserIdentity.IDENTITY_VALUE[identity_word]
    identity.save()

    return operation_succeeded()
예제 #2
0
def signup():
    """
    User sign-up page.
    GET requests serve sign-up page.
    POST requests validate form & user creation.
    """
    form = SignupForm()
    if form.validate_on_submit():
        existing_user = User.query.filter_by(email=form.email.data).first()
        if existing_user is None:
            user = User(name=form.name.data,
                        email=form.email.data,
                        phone_number=form.number.data)
            user.set_password(form.password.data)
            user.set_number(form.number.data)
            db.session.add(user)
            db.session.commit()  # Create new user
            login_user(user)  # Log in as newly created user
            return redirect(url_for('main_bp.dashboard'))
        flash('A user already exists with that email address.')
    return render_template('signup.jinja2',
                           title='Create an Account.',
                           form=form,
                           template='signup-page',
                           body="Sign up for a user account.")
예제 #3
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('Congratulations, you are now a registered user!')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
예제 #4
0
def sign_up():
    if current_user.is_authenticated:
        return redirect(url_for('ui.prices'))
    form = SignUpForm(meta={'csrf': False})
    if form.validate_on_submit():
        user = User(username=form.username.data)
        user.set_password(form.password.data)
        with session_scope() as session:
            session.add(user)
            session.commit()
        flash("Registration successful. Please, sign in now.")
        return redirect(url_for('auth.sign_in'))
    return render_template('auth/register.html', form=form)
예제 #5
0
def sign_up():
    if current_user.is_authenticated:
        return redirect(url_for('index.index'))

    form = SignupForm()
    if not form.validate_on_submit():
        return render_template('user/signup.html',
                               title=_('Register'),
                               form=form)

    user = User(username=form.username.data, email=form.email.data)
    user.set_password(form.password.data)
    db.session.add(user)
    flash(_('Congratulations, you are now a registered user!'))
    return redirect(url_for('user_bp.signin'))
예제 #6
0
def sign_up_user():
    logging.info('[SIGNUP] Got sign up request')
    username = request.form.get('uname')
    pwd = request.form.get('pwd')
    email = request.form.get('email')
    if username is None or pwd is None or email is None:
        flash('Wrong data provided', category='error')
        return redirect(url_for('logged_out_bp.main_page'))
    else:
        user = User(username=username, email=email)
        user.set_password(pwd)
        db.session.add(user)
        try:
            db.session.commit()
        except IntegrityError as e:
            print(e)
            flash('User exists', category='error')
            return redirect(url_for('logged_out_bp.main_page'))
        flash('User created', category='message')
        return redirect(url_for('logged_out_bp.main_page'))
예제 #7
0
def db_register_new_user(username, password, email):
	new_user = User(username, email=email)
	new_user.set_password(password)
	db.session.add(new_user)
	db.session.commit()