本文整理汇总了Python中models.User.validate方法的典型用法代码示例。如果您正苦于以下问题:Python User.validate方法的具体用法?Python User.validate怎么用?Python User.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.User
的用法示例。
在下文中一共展示了User.validate方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_validate
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def test_validate():
# should not raise any error
User(name='totti', active=True, data=10).validate()
user = User(name='totti', posts=4)
with pytest.raises(ValidationError) as excinfo:
user.validate()
assert excinfo.value.as_dict() == {
'posts': 'invalid value type'
}
comment = Comment(_id=ObjectId(), body='')
with pytest.raises(ValidationError) as excinfo:
comment.validate()
assert excinfo.value.as_dict() == {
'body': 'blank value is not allowed',
'author': 'field is required'
}
post = Post(author=user, comments=[comment])
with pytest.raises(ValidationError) as excinfo:
post.validate()
assert excinfo.value.as_dict() == {
'title': 'field is required',
'comments': {
0: {
'body': 'blank value is not allowed',
'author': 'field is required'
}
}
}
示例2: register
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def register():
if current_user.is_authenticated():
return redirect(url_for('editprofile'))
if request.method == 'GET':
return render_template('register.html', user=User(), errors={})
user = User(
request.form['username'],
request.form['password'],
request.form['password2'],
request.form['email'],
request.form['fullname']
)
if user.validate():
user.enabled = True
current_app.db_session.add(user)
current_app.db_session.commit()
login_user(user)
flash(u'Du har nå opprettet en bruker!')
return redirect(request.args.get('next') or url_for('index'))
else:
return render_template(
'register.html',
errors=user.validation_errors,
user=user
)
示例3: api_user_new
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def api_user_new(values=None):
validation = User.validate(values)
if validation == True:
user = User.from_values(values)
session['id'] = user.username
return response_success(serializers.user(user))
else:
return response_error(validation)
示例4: api_user
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def api_user(user):
if request.method == 'GET':
return json.dumps(serializers.user(user))
else:
values = request.get_json(force=True)
validation = User.validate(values)
if validation == True:
user = User.from_values(values)
session['id'] = user.username
return response_success(serializers.user(user))
else:
return response_error(validation)
示例5: login
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def login():
if request.method == "GET":
return render_template("login.html")
elif request.method == 'POST':
user = User(g.db)
#validate user id
userinfo = user.validate(request.form['username'],request.form['password'])
if userinfo is not None:
session['userinfo'] = userinfo
return redirect("index")
else:
return 'error username or password'
示例6: login
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def login():
u = User(request.form)
user = User.query.filter_by(username=u.username).first()
log(user)
if u.validate(user):
log("用户登录成功")
session['user_id'] = user.id
r = redirect(url_for('timeline_view', username=user.username))
return r
else:
log("用户登录失败", user)
flash('登录失败')
return redirect(url_for('login_view'))
示例7: login
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def login():
form = LoginForm(request.form)
if form.validate_on_submit():
user = User.validate(form.username.data, form.password.data)
if user:
login_user(user)
if request.form['next'] != '':
return redirect(request.form['next'])
else:
return redirect(url_for('admin'))
else:
form.password.errors.append(u'Логін або пароль не вірні')
form.username.errors.append('')
return render_template('login.html', form=form)
示例8: post
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def post(self):
try:
data = json.loads(self.request.body.decode())
except:
raise
action = data['action']
user_data = data['user']
if action == 'create':
if User.validate(user_data):
saved = yield User.save(user_data)
if saved:
self.write('success')
return
raise HTTPError(400, 'bad input')
elif action == 'update':
pass
示例9: login
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def login():
next_ = request.values.get('next')
if request.method == 'POST':
form = {
'username' : request.form['username'],
'password' : request.form['password']
}
user = User.validate(**form)
if user[0]:
if request.form['next'] != '':
session['user'] = {
'username': user[1].username,
'id': user[1].id
}
return redirect(request.form['next'])
return redirect(url_for('home'))
return render_template('login.html', next=next_)
示例10: login
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def login():
u = User(request.form)
user = User.query.filter_by(username=u.username).first()
log(user)
if u.validate(user):
log("用户登录成功")
# 用 make_response 生成响应 并且设置 cookie
session['user_id'] = user.id
r = redirect(url_for('timeline_view', username=user.username))
# cookie_id = str(uuid.uuid4())
# cookie_dict[cookie_id] = user
# r.set_cookie('cookie_id', cookie_id)
return r
else:
("用户登录失败", user)
# message = '用户登录失败'
flash('用户登录失败')
flash('用户名不存在')
return redirect(url_for('login_view'))
示例11: register
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def register():
errors = []
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
newUser = User(username, password)
isValid, errors = newUser.validate()
if isValid:
db_session.add(newUser)
db_session.commit()
session['username'] = newUser.name
session.permanent = True
return redirect(url_for('index'))
else:
return render_template('register.html', user = g.user, errors=errors)
else:
return render_template('register.html', user = g.user, errors=errors)
示例12: login
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def login():
# 这里拿到的已经是一个字典了
form = request.get_json()
# if isinstance(form, dict):
# print('form, ', form)
# else:
# print('form不是一个dict, ', form)
u = User(form)
user = User.query.filter_by(username=u.username).first()
log(user)
status = {
'success': True,
'url': '/timeline/{}'.format(u.username),
'message': '登录成功',
}
if u.validate(user):
log("用户登录成功")
session['user_id'] = user.id
else:
log("用户登录失败", user)
status['success'] = False
status['message'] = '登录失败'
r = json.dumps(status, ensure_ascii=False)
return r
示例13: validate_username_or_email
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import validate [as 别名]
def validate_username_or_email(self, field):
try:
self.validated_user = User.validate(field.data, self.password.data)
except RuntimeError:
raise ValidationError("Invalid username/email or password.")