def login(): if g.user is not None and g.user.is_authenticated: return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): user = User() user.name = form.name.data user.passwd = form.password.data.strip() users = User.find_all('name=?', [user.name]) if users is None or len(users) == 0: log(level=1, msg="用户名不存在!") return redirect(url_for('login')) elif not check_password_hash(users[0].passwd, user.passwd): log(level=1, msg="密码错误!") return redirect(url_for('login')) login_user(users[0], remember=True) return redirect(request.args.get('next') or url_for('index')) return render_template('login.html', title='Sign in', form=form)
def user(id): user = User.find(id) form = UserInfoEditForm() if len(user) == 0: flash('User not found') return redirect(url_for('index')) if form.validate_on_submit(): user.name = form.name.data user.email = form.name.email user.update() return redirect(url_for('index')) return render_template('user.html', user=user, form=form)
def create_user(user_to_create): session = Session() try: session.query(User).filter_by(email=user_to_create.get('email')).one() except NoResultFound: password = generate_password_hash(user_to_create.get('password')) user = User(email=user_to_create.get('email'), username=user_to_create.get('username'), password=password) session.add(user) session.commit() session.refresh(user) return UserData().dump(user) else: raise ValidationError("Email must be unique")
def confirm(id): if g.user is not None and g.user.is_authenticated: return redirect(url_for('index')) user = User.find(id) if not user: return redirect(url_for('register')) elif int(time.time()) - user.created_at > 3600: user.remove() return redirect(url_for('register')) elif user.id != id: return redirect(url_for('register')) # 验证正确,将数据保存到数据库 user.isconfirmed = True user.update() login_user(user, remember=True) return redirect(url_for('index'))
def test_models(self): from flask_bcrypt import generate_password_hash user = User(username='******', firstName='admin', lastName='admin', email='*****@*****.**', phone='+38099', userAuthStatus='notSignedIn', password=generate_password_hash('admin')) self.assertEqual( str(user), 'User None, terminator2000 (admin admin) [email protected] +38099 status: notSignedIn' ) wallet = Wallet(name='MyFirstWallet', balance=10000, currency='USD', owner=user) self.assertEqual( str(wallet), f'Wallet {wallet.id} {wallet.name}, owner: {wallet.user_id}, balance: {wallet.balance} {wallet.currency}' )
def register(): if g.user is not None and g.user.is_authenticated: return redirect(url_for('index')) form = RegisterForm() if form.validate_on_submit(): user = User() user.name = form.name.data user.email = form.email.data user.isconfirmed = False user.image = "/static/img/user.png" user.passwd = generate_password_hash(form.password.data.strip()) rows = user.save() if rows != 1: return redirect(url_for('register')) else: send_email(to=user.email, subject='确认注册信息!', template='/email/confirm', user=user, id=user.id) # login_user(user, remember=True) return redirect(url_for('index')) return render_template('register.html', title='Sign in', form=form, providers=app.config['OPENID_PROVIDERS'])
def create_objects(): session = Session() user = User(username='******', firstName='admin', lastName='admin', email='*****@*****.**', phone='+38099', userAuthStatus=UserStatus.notSignedIn, password=generate_password_hash('admin')) user2 = User(username='******', firstName='admin', lastName='admin', email='*****@*****.**', phone='+380990', userAuthStatus=UserStatus.notSignedIn, password=generate_password_hash('admin')) user3 = User(username='******', firstName='admin', lastName='admin', email='*****@*****.**', phone='+380940', userAuthStatus=UserStatus.notSignedIn, password=generate_password_hash('admin')) wallet13 = Wallet( name='MyFirstWallet', balance=10000, currency=Currency.USD, owner=user3, ) wallet23 = Wallet( name='MySecondWallet', balance=100, currency=Currency.UAH, owner=user3, ) wallet1 = Wallet( name='MyWallet', balance=10000, currency=Currency.USD, owner=user, ) wallet2 = Wallet( name='MySecoWallet', balance=100, currency=Currency.UAH, owner=user, ) session.add(user) session.add(user2) session.add(wallet1) session.add(wallet2) session.add(user3) session.add(wallet13) session.add(wallet23) session.commit()
def load_user(id): return User.find(id)
def userview(id): user = User.find(id) if g.user is not None and g.user.is_authenticated: return render_template('userview.html', userview=user, user=g.user) return render_template('userview.html', userview=user)