def signup(self): user = User() user.user_detail = UserDetail() self.populate_obj(user) db.session.add(user) db.session.commit() return user
def init_data(self): demo = User(name=u'demo', email=u'*****@*****.**', password=u'123456', role_code=USER, status_code=ACTIVE, user_detail=UserDetail( sex_code=MALE, age=10, url=u'http://demo.example.com', deposit=100.00, location=u'Hangzhou', bio=u'admin Guy is ... hmm ... just a demo guy.')) admin = User(name=u'admin', email=u'*****@*****.**', password=u'123456', role_code=ADMIN, status_code=ACTIVE, user_detail=UserDetail( sex_code=MALE, age=10, url=u'http://admin.example.com', deposit=100.00, location=u'Hangzhou', bio=u'admin Guy is ... hmm ... just a admin guy.')) db.session.add(demo) db.session.add(admin) db.session.commit()
def search(): keywords = request.args.get('keywords', '').strip() pagination = None if keywords: page = int(request.args.get('page', 1)) pagination = User.search(keywords).paginate(page, 1) else: flash(_('Please input keyword(s)'), 'error') return render_template('frontend/search.html', pagination=pagination, keywords=keywords)
def test_follow_unfollow(self): user1 = User(name='tester1', email='*****@*****.**', password='******') db.session.add(user1) user2 = User(name='tester2', email='*****@*****.**', password='******') db.session.add(user2) db.session.commit() user1.follow(user2) assert user1.num_following == 1 assert user1.get_following_query().first().id == user2.id assert user2.num_followers == 1 assert user2.get_followers_query().first().id == user1.id user1.unfollow(user2) assert user1.num_following == 0 assert user2.num_followers == 0
def login(): if current_user.is_authenticated(): return jsonify(flag='success') username = request.form.get('username') password = request.form.get('password') if username and password: user, authenticated = User.authenticate(username, password) if user and authenticated: if login_user(user, remember='y'): return jsonify(flag='success') current_app.logger.debug('login(api) failed, username: %s.' % username) return jsonify(flag='fail', msg='Sorry, try again.')
def reauth(): form = ReauthForm(next=request.args.get('next')) if request.method == 'POST': user, authenticated = User.authenticate(current_user.name, form.password.data) if user and authenticated: confirm_login() current_app.logger.debug('reauth: %s' % session['_fresh']) flash(_('Reauthenticated.'), 'success') return redirect('/change_password') flash(_('Password is wrong.'), 'error') return render_template('frontend/reauth.html', form=form)
def login(): if current_user.is_authenticated(): return redirect(url_for('user.index')) form = LoginForm(login=request.args.get('login', None), next=request.args.get('next', None)) if form.validate_on_submit(): user, authenticated = User.authenticate(form.login.data, form.password.data) if user and authenticated: remember = request.form.get('remember') == 'y' if login_user(user, remember=remember): flash(_("Logged in"), 'success') return redirect(form.next.data or url_for('user.index')) else: flash(_('Sorry, invalid login'), 'error') return render_template('frontend/login.html', form=form)
def initdb(): """Init/reset database.""" db.drop_all() db.create_all() admin = User( name=u'admin', email=u'*****@*****.**', password=u'123456', role_code=ADMIN, status_code=ACTIVE, user_detail=UserDetail( sex_code=MALE, age=10, url=u'http://admin.example.com', deposit=100.00, location=u'Hangzhou', bio=u'admin Guy is ... hmm ... just a admin guy.')) db.session.add(admin) db.session.commit()
def create_profile(self): user = User() self.populate_obj(user) db.session.add(user) db.session.commit()
def validate_password(form, field): user = User.get_by_id(current_user.id) if not user.check_password(field.data): raise ValidationError(_("Password is wrong."))
def validate_name(form, field): user = User.get_by_id(current_user.id) if not user.check_name(field.data): raise ValidationError(_("Please pick another name."))