def oauth_callback(provider): oauth = OAuthSignIn.get_provider(provider) social_id, username, email = oauth.callback() if not email: email = '{}@oauth.com'.format(social_id) if not social_id: flash('Sorry, the authentication failed.') return redirect(url_for('auth.home')) user = User.query.filter_by(oauth_id=social_id).first() if not user: user = User(username=username, oauth_id=social_id, email=email, password="******".format(social_id), registered_on=datetime.utcnow(), activated_on=datetime.utcnow(), active=True) db.session.add(user) db.session.commit() login_user(user, True) return redirect(url_for('users.account', user_name=username)) login_user(user, True) return return_user_homepage(user.homepage, user.username)
def home(): login_form = LoginForm() register_form = RegistrationForm() if login_form.validate_on_submit(): user = User.query.filter_by( username=login_form.login_username.data).first() if user and not user.active: app.logger.info( '[INFO] - [{}] Connexion attempt while account not activated'. format(user.id)) flash( 'Your Account is not activated. Please check your email address to activate your account.', 'danger') elif user and bcrypt.check_password_hash( user.password, login_form.login_password.data): login_user(user, remember=login_form.login_remember.data) app.logger.info('[INFO] - [{}] Logged in.'.format(user.id)) flash("You're now logged in. Welcome {0}".format(user.username), "success") next_page = request.args.get('next') if next_page: return redirect(next_page) return return_user_homepage(user.homepage, user.username) else: flash('Login Failed. Please check username and password.', 'warning') elif register_form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( register_form.register_password.data).decode('utf-8') user = User(username=register_form.register_username.data, oauth_id='{}'.format(random.randint(0, 16846876000056)), email=register_form.register_email.data, password=hashed_password, registered_on=datetime.utcnow()) db.session.add(user) db.session.commit() app.logger.info( '[INFO] - [{}] New account registration: Username: {}, email: {}'. format(user.id, register_form.register_username.data, register_form.register_email.data)) try: send_register_email(user) flash( 'Your account has been created. Check your e-mail address to activate your account.', 'info') except Exception as e: app.logger.error( '[ERROR] - Sending register email to account ID [{}]: {}.'. format(user.id, e)) flash( "An error occured while sending your register e-mail. Admin were advised. Please try again later." ) return redirect(url_for('auth.home')) return render_template('home.html', login_form=login_form, register_form=register_form)
def create_first_data(): db.create_all() if User.query.filter_by(id='1').first() is None: admin1 = User(username='******', email='*****@*****.**', password=bcrypt.generate_password_hash("password").decode('utf-8'), active=True, private=True, registered_on=datetime.utcnow(), activated_on=datetime.utcnow(), role=RoleType.ADMIN, oauth_id="a") manager1 = User(username='******', email='*****@*****.**', password=bcrypt.generate_password_hash("password").decode('utf-8'), active=True, registered_on=datetime.utcnow(), activated_on=datetime.utcnow(), role=RoleType.MANAGER, oauth_id="b") user1 = User(username='******', email='*****@*****.**', password=bcrypt.generate_password_hash("password").decode('utf-8'), active=True, registered_on=datetime.utcnow(), activated_on=datetime.utcnow(), oauth_id="c") db.session.add(admin1) db.session.add(manager1) db.session.add(user1) add_frames_to_db() add_badges_to_db() add_ranks_to_db() refresh_db_frames() refresh_db_badges() refresh_db_ranks() # add_eps_watched() compute_media_time_spent() # update_Mylists_stats() db.session.commit()