def init(with_testdb, with_data): """ Initialize the database. :param with_data: :param with_testdb: Create a test database :return: None """ uri = app.config['SQLALCHEMY_DATABASE_URI'] print(f"Database uri is {uri}") if not database_exists(uri): create_database(uri) db.create_all() else: db.drop_all() db.create_all() if with_testdb: db_uri = '{0}_test'.format(app.config['SQLALCHEMY_DATABASE_URI']) if not database_exists(db_uri): create_database(db_uri) user = User() user.username = "******" user.save() if with_data: _seed_catalog() return None
def signup(): form = SignupForm() if form.validate_on_submit(): u = User() form.populate_obj(u) u.password = User.encrypt_password(request.form.get('password')) u.save() if login_user(u): flash('Awesome, thanks for signing up!', 'success') return redirect(url_for('user.welcome')) return render_template('user/signup.html', form=form)
def signup(): form = SignupForm() if form.validate_on_submit(): u = User() form.populate_obj(u) u.password = User.encrypt_password(request.form.get('password')) u.save() if login_user(u): from app.blueprints.user.tasks import send_welcome_email send_welcome_email.delay(current_user.email) # Create a user id for the user mailbox_id = generate_mailbox_id() # Create an inbox for the user if create_inbox(mailbox_id): current_user.mailbox_count += 1 current_user.active_mailbox = True current_user.mailbox_id = mailbox_id current_user.save() from app.blueprints.parse.models.mailbox import Mailbox m = Mailbox() m.mailbox_id = mailbox_id m.user_email = current_user.email db.session.add(m) db.session.commit() flash('Awesome, thanks for signing up!', 'success') else: flash( 'There was a problem creating an inbox for you. Please try again.', 'error') current_user.active_mailbox = False current_user.mailbox_count = 0 current_user.mailbox_id = None current_user.save() return redirect(url_for('user.settings')) return render_template('user/signup.html', form=form)
def google_logged_in(blueprint, token): if not token: flash("Failed to log in with {name}".format(name=blueprint.name)) return # figure out who the user is resp = blueprint.session.get("/oauth2/v2/userinfo") if resp.ok: username = resp.json()["email"] query = User.query.filter_by(username=username) try: user = query.one() except NoResultFound: # create a user user = User() user.username = username user.save() login_user(user) flash("Successfully signed in with Google", "success") else: msg = "Failed to fetch user info from {name}".format( name=blueprint.name) flash(msg, category="error")
def github_logged_in(blueprint, token): if not token: flash("Failed to log in with {name}".format(name=blueprint.name)) return # figure out who the user is resp = blueprint.session.get("/user") if resp.ok: print(f"Response from github: {resp.json()}") username = resp.json()["login"] query = User.query.filter_by(username=username) try: user = query.one() except NoResultFound: # create a user user = User() user.username = username user.save() login_user(user) flash("Successfully signed in with Github", "success") else: print(resp.text) msg = "Failed to fetch user info from {name}".format( name=blueprint.name) flash(msg, category="error")