def user(db):
    """ Creates a user without a profile. """
    from my_app.models import User
    user = User(firstname="Person", lastname='One', email='*****@*****.**')
    user.set_password('password1')
    db.session.add(user)
    db.session.commit()
    return user
示例#2
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for("main.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("Successfully Registerd...")
        return redirect(url_for("auth.login"))

    return render_template("register.html", form=form)
def user_with_profile(db):
    """ Creates a user with a profile with a username and bio """
    from my_app.models import User, Profile
    user = User(firstname='Person', lastname='Three', email='*****@*****.**')
    user.profile = Profile(username='******',
                           bio="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ac tempor metus. "
                               "Aenean mattis, tortor fringilla iaculis pharetra, dui justo imperdiet turpis, "
                               "at faucibus risus eros vitae dui. Nam finibus, nibh eu imperdiet feugiat, nisl lacus "
                               "porta tellus, a tincidunt nibh enim in urna. Aliquam commodo volutpat ligula at "
                               "tempor. In risus mauris, commodo id mi non, feugiat convallis ex. Nam non odio dolor. "
                               "Cras egestas mollis feugiat. Morbi ornare laoreet varius. Pellentesque fringilla "
                               "convallis risus, sit amet laoreet metus interdum et.")
    user.set_password('password3')
    db.session.add(user)
    db.session.commit()
    return user
示例#4
0
def signup():
    form = SignupForm(request.form)
    if form.validate_on_submit():
        user = User(firstname=form.first_name.data,
                    lastname=form.last_name.data,
                    email=form.email.data)
        user.set_password(form.password.data)
        try:
            db.session.add(user)
            db.session.commit()
            flash(
                f"Hello, {user.firstname} {user.lastname}. You are signed up.")
        except IntegrityError:
            db.session.rollback()
            flash(f'Error, unable to register {form.email.data}. ', 'error')
            return redirect(url_for('auth.signup'))
        return redirect(url_for('main.index'))
    return render_template('signup.html', title='Sign Up', form=form)
 def setUp(self):
     db.session.commit()
     db.drop_all()
     db.create_all()
     # Add the local authority data to the database (this is a workaround you don't need this for your coursework!)
     csv_file = Path(__file__).parent.parent.joinpath(
         "data/household_recycling.csv")
     df = pd.read_csv(csv_file, usecols=['Code', 'Area'])
     df.drop_duplicates(inplace=True)
     df.set_index('Code', inplace=True)
     df.to_sql('area', db.engine, if_exists='replace')
     # Add a user and profile to the database
     u = User(firstname='TestFirstname1',
              lastname='TestLastname1',
              email='*****@*****.**')
     u.set_password('test1')
     p = Profile(username='******',
                 bio='Something about test1.',
                 area='Barnet',
                 user_id=u.id)
     u.profiles.append(p)
     db.session.add(u)
     db.session.commit()
示例#6
0
 def test_password_hashing(self):
     u = User(username='******')
     u.set_password('cat')
     self.assertFalse(u.check_password('dog'))
     self.assertTrue(u.check_password('cat'))
示例#7
0
def create_app(config_classname):
    """
    Initialise and configure the Flask application.
    :type config_classname: Specifies the configuration class
    :rtype: Returns a configured Flask object
    """
    app = Flask(__name__)
    app.config.from_object(config_classname)

    db.init_app(app)
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app)
    #csrf.init_app(app)
    configure_uploads(app, photos)

    with app.app_context():
        from my_app.models import User, Profile, Area
        db.create_all()

        # Add the local authority data to the database (this is a workaround you don't need this for your coursework!)
        csv_file = Path(__file__).parent.parent.joinpath(
            "data/household_recycling.csv")
        df = pd.read_csv(csv_file, usecols=['Code', 'Area'])
        df.drop_duplicates(inplace=True)
        df.set_index('Code', inplace=True)
        df.to_sql('area', db.engine, if_exists='replace')

        # Add sample data for the REST API exercise
        u = User.query.first()
        if u is None:
            p1 = Profile(username='******',
                         bio='something about me',
                         area='Barnet')
            p2 = Profile(username='******',
                         bio='something interesting',
                         area='Barnet')
            u1 = User(firstname='Jo',
                      lastname='Bloggs',
                      email='*****@*****.**',
                      profiles=[p1])
            u2 = User(firstname='Fred',
                      lastname='Smith',
                      email='*****@*****.**',
                      profiles=[p2])
            u3 = User(firstname='Santa',
                      lastname='Claus',
                      email='*****@*****.**')
            u4 = User(firstname='Robert',
                      lastname='Plant',
                      email='*****@*****.**')
            u1.set_password('test')
            u2.set_password('test')
            u3.set_password('test')
            u4.set_password('test')
            db.session.add_all([u1, u2, u3, u4])
            db.session.commit()

        from dash_app.dash import init_dashboard
        app = init_dashboard(app)

    from my_app.main.routes import main_bp
    app.register_blueprint(main_bp)

    from my_app.community.routes import community_bp
    app.register_blueprint(community_bp)

    from my_app.auth.routes import auth_bp
    app.register_blueprint(auth_bp)

    from my_app.api.routes import api_bp
    app.register_blueprint(api_bp)

    return app