コード例 #1
0
ファイル: views.py プロジェクト: bluedogs/flask_app_boiler
def signup():
    """
    Handle requests to the /signup route
    Add an user to the database through the registration form
    """
    form = RegisterForm()
    if form.validate_on_submit():
        email = form.email.data
        # Add a check for specific email domain
        # dumb trick
        if email.endswith('@example.com'):
            user = User(email=form.email.data,
                        name=form.name.data,
                        password=form.password.data)
            user.is_admin = False

            # add user to the database
            db.session.add(user)
            db.session.commit()
            flash('You have successfully registered! You may now login.')

            # redirect to the login page
            return redirect(url_for('auth.signin'))
        else:
            # redirect to the login page as the email domain does not match
            flash("Sorry, We are not accepting users at this time.")
            return redirect(url_for('home.index'))

    # load registration template
    return render_template('auth/signup.html', form=form)
コード例 #2
0
    def post(self):
        try:
            data = request.get_json()
            print(data)
            username = data.get('username', None)
            password = data.get('password', None)

            if username and password:
                user = User.query.filter_by(username=username).first()
                if user:
                    return jsonify(
                        {'error': 'username has already been registered!'})
                else:
                    new_user = User()
                    new_user.username = username
                    new_user.set_password(password)
                    new_user.public_id = str(uuid.uuid4())
                    new_user.is_admin = False

                    db.session.add(new_user)
                    db.session.commit()

                    return jsonify({'message': 'Successful!'})

            raise Exception('Invalid input')
        except Exception as value:
            return jsonify({'error': 'Invalid input'}), 406
コード例 #3
0
ファイル: manage.py プロジェクト: bluedogs/flask_app_boiler
def create_admin():
    """Creates the admin user."""
    user = User(email="*****@*****.**", name="admin", password="******")

    user.is_admin = True

    db.session.add(user)
    db.session.commit()
コード例 #4
0
def create(username, email, password, admin):
    user = User.query.filter_by(username=username).first()
    if user:
        click.echo('User already exist with given username, please choose another username.')
        return
    user = User.query.filter_by(email=email).first()
    if user:
        click.echo('User already exist with given email, please choose another email.')
        return

    user = User(username, email, password)
    if admin:
        user.is_admin()

    try:
        db.session.add(user)
        db.session.commit()
        click.echo(f'User {user.username} has been successfully created.')
    except Exception as e:
        click.echo('Something went wrong.')
        click.echo(e)
        db.session.rollback()  # cofa zmiany db jesli cos pojdzie nie tak
コード例 #5
0
def admin():
    username = click.prompt("Username")
    email = click.prompt("Email")
    password = click.prompt("Password",
                            hide_input=True,
                            confirmation_prompt="Repeat Password")
    try:
        user = User(username=username, email=email)
        user.set_password(password)
        user.is_admin = True
        user.is_confirmed = True
        db.session.add(user)
        db.session.commit()
    except:
        click.echo("An error has occurred")
コード例 #6
0
 def create_user(name, email, password, is_admin):
     user = User(name, email)
     user.set_password(password)
     user.is_admin = is_admin
     user.save()
     return user