def app(): os.environ['STAGE'] = 'test' app = create_app() user0 = User("Son", "*****@*****.**", bcrypt.generate_password_hash("1234567aA").decode('utf8')) user1 = User("Hoan", "*****@*****.**", bcrypt.generate_password_hash("1234567bB").decode('utf8')) user2 = User("Lam", "*****@*****.**", bcrypt.generate_password_hash("1234567cC").decode('utf8')) user3 = User("Hung", "*****@*****.**", bcrypt.generate_password_hash("1234567dD").decode('utf8')) user4 = User("Nam", "*****@*****.**", bcrypt.generate_password_hash("1234567eE").decode('utf8')) user5 = User("Viet", "*****@*****.**", bcrypt.generate_password_hash("1234567fF").decode('utf8')) user2.activated = True user_list = [user0, user1, user2, user3, user4, user5] for i in range(6): db.session.add(user_list[i]) db.session.commit() yield app cache.clear() db.session.remove() db.drop_all()
def register(): title = 'Create an account' if current_user.is_authenticated: return redirect(url_for('main.index')) form = RegistrationForm() if form.validate_on_submit(): email = form.email.data.lower() hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') master_key = generate_pswrd(length=32, special=False) encrypted_master_key = encrypt(get_key(form.password.data), master_key) user = User(email=email, password=hashed_password, master_key=encrypted_master_key) try: send_activation_email(user) flash('Account created! Verification link has been sent to your email.', 'success') except SMTPRecipientsRefused: flash('Entered email address is invalid!', 'danger') return redirect(url_for('account.register')) except: user.activated = True flash('Account created! You can now log in.', 'success') db.session.add(user) db.session.commit() return redirect(url_for('account.login')) return render_template('account/register.html', title=title, form=form)
def register(): form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data, confirmed=False) user.set_password(form.password.data) user.activated = False user.locked = False db.session.add(user) db.session.commit() session['username'] = user.username return redirect(url_for('two_factor_setup')) return render_template('authentication/register.html', title='Register', form=form)
def seed_db(num_of_employers=20, num_of_gigs=30, num_of_musicians=20): num_of_users = User.query.count() fake = Faker() i = 0 print("Creating employers...") print("Created: 0", end="\r") while i < num_of_employers: e = User( username=fake.company(), email=fake.email(), password='******', location=fake.city(), description="We sometimes put up gig offers. Here are some random words: " + fake.paragraph(3, True), role_id=Role.EMPLOYER ) e.activated = True db.session.add(e) try: db.session.commit() i += 1 print("Created: " + str(i), end="\r") except IntegrityError: db.session.rollback() print("Finished creating " + str(num_of_employers) + " employers...") print("\n") i = 0 print("Creating gigs...") print("Created: 0", end="\r") employer_ids = db.session.query(User.id).filter_by(role_id=3).all() while i < num_of_gigs: random_title = random_gig_title() g = Gig( title=random_title, description=random_gig_description(random_title), payment=round(random.uniform(100, 2000),2), location=fake.city(), employer_id=random.choice(employer_ids)[0] ) db.session.add(g) db.session.commit() i += 1 print("Created: " + str(i), end="\r") print("Finished creating " + str(num_of_gigs) + " gigs...") print("\n") i = 0 print("Creating musicians...") print("Created: 0", end="\r") while i < num_of_musicians: m = User( username=fake.user_name(), email=fake.email(), password='******', location=fake.city(), description="I am ready to make some music! Please hire me. Here are some random words: " + fake.paragraph(3, True), role_id=Role.MUSICIAN ) m.activated = True db.session.add(m) try: db.session.commit() i += 1 print("Created: " + str(i), end="\r") except IntegrityError: db.session.rollback() print("Finished creating " + str(num_of_musicians) + " musicians...") print("\n") i = 0 print("Applying musicians to gigs...") num_of_applications = int(num_of_musicians/2) print("Will create " + str(num_of_applications*num_of_gigs) + " applications.") musician_ids = db.session.query(User.id).filter_by(role_id=2).all() while i < num_of_gigs: g = Gig.query.get(i+1) j = 0 while j < num_of_applications: m = User.query.get(random.choice(musician_ids)[0]) try: m.apply(g) db.session.commit() j += 1 except IntegrityError: db.session.rollback() i += 1 print("\n") print("Creating admin user...") print("\n") print("Login info for admin:") print("Email: [email protected]") print("Password: pass") print("\n") admin = User("admin", "*****@*****.**", "password123", "Nowhere", "I am admin", Role.ADMIN) db.session.add(admin) db.session.commit() print("Database is ready!")