本文整理汇总了Python中db.User.save方法的典型用法代码示例。如果您正苦于以下问题:Python User.save方法的具体用法?Python User.save怎么用?Python User.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类db.User
的用法示例。
在下文中一共展示了User.save方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post_signup_handler
# 需要导入模块: from db import User [as 别名]
# 或者: from db.User import save [as 别名]
def post_signup_handler(response):
email = response.get_field("email", "")
username = response.get_field("username", "")
password = response.get_field("password", "")
if username == "" or password == "":
response.redirect("/signup")
return
if len(password) < 8:
response.redirect("/signup?fail=password_short")
user = User(username, password)
try:
user.save()
except sqlite3.IntegrityError:
response.redirect("/signup?fail=user_exists")
else:
response.set_secure_cookie("user_id", str(user.id))
response.redirect("/")
示例2: route_register_api
# 需要导入模块: from db import User [as 别名]
# 或者: from db.User import save [as 别名]
def route_register_api():
if g.user:
raise APIError("Already logged in!")
params = {k:v for k, v in request.values.items() if k in ["username", "password", "email"]}
if not all(params.values()):
raise APIError("Missing required paramaters!")
try:
User.get((User.username == params["username"]) | (User.email == params["email"]))
raise APIError("User with that username/email already exists!")
except User.DoesNotExist: pass
u = User(username=params["username"], email=params["email"])
u.password = User.hash_password(params["password"])
session["id"] = u.save()
return APIResponse()
示例3: route_register
# 需要导入模块: from db import User [as 别名]
# 或者: from db.User import save [as 别名]
def route_register():
if g.user:
return redirect("/", code=302)
if request.method == "GET":
return render_template("register.html")
params = {k:v for k, v in request.values.items() if k in ["username", "password", "email"]}
if not all(params.values()):
return redirect("/", code=302)
try:
User.get((User.username == params["username"]) | (User.email == params["email"]))
return redirect("/", code=302)
except User.DoesNotExist: pass
u = User(username=params["username"], email=params["email"])
u.password = User.hash_password(params["password"])
session["id"] = u.save()
g.user = u
return redirect("/", code=302)
示例4: TestUsers
# 需要导入模块: from db import User [as 别名]
# 或者: from db.User import save [as 别名]
class TestUsers(TestCase):
def run(self, result=None):
with test_database(test_db, (User, Company)):
super(TestUsers, self).run(result)
def create_test_data(self):
self._company = Company().create(name='test_name', phone_number='1223456789', address='123 test road, testville test')
self._user = User().create(name='test_name', email='[email protected]', title='the_boss',
secret_question='what is the answer?',
phone_number='1234567891', authentication_level=3, company=self._company.pk)
self._user.set_password('test_password')
self._user.set_secret_answer('42')
self._admin = User().create(name='admin', email='[email protected]', title='the_boss',
secret_question='what is the answer?',
phone_number='1234567891', authentication_level=1, company=self._company.pk)
self._admin.set_password('test_password')
self._admin.set_secret_answer('42')
def test_user_creation(self):
self.create_test_data()
self.assertEqual(self._user.name, 'test_name')
self.assertEqual(self._user.email, '[email protected]')
self.assertEqual(self._user.title, 'the_boss')
self.assertEqual(self._user.secret_question, 'what is the answer?')
self.assertEqual(self._user.phone_number, '1234567891')
self.assertEqual(self._user.authentication_level, 3)
def test_user_delete(self):
self.create_test_data()
deleted = self._user.delete()
self.assertEqual(deleted, 1)
def test_user_validation(self):
self.create_test_data()
self.assertTrue(self._user.validate_login(plaintext_password='test_password'))
def test_user_validation_after_update(self):
self.create_test_data()
test_name = "test_name"
self._user.name = test_name
self._user.save()
self.assertTrue(self._user.validate_login(plaintext_password='test_password'))
def test_user_secret_question(self):
self.create_test_data()
self.assertTrue(self._user.validate_secret('42'))
self.assertTrue(self._user.validate_secret(' 42 '))
self.assertFalse(self._user.validate_secret(' forty-two '))
def test_user_change_info(self):
self.create_test_data()
query = User().update(authentication_level=2, email='[email protected]',
phone_number='1231231234', name='new_name', title='new_title').where(User.pk == self._user.pk)
query.execute()
pk = self._user.pk
self._user_2 = User().get(pk=pk)
self.assertEqual(self._user_2.authentication_level, 2)
self.assertEqual(self._user_2.phone_number, '1231231234')
self.assertEqual(self._user_2.name, 'new_name')
self.assertEqual(self._user_2.title, 'new_title')
def test_user_change_password(self):
self.create_test_data()
self._user.set_password('new_password')
self.assertFalse(self._user.validate_login(plaintext_password='test_password'))
self.assertTrue(self._user.validate_login(plaintext_password='new_password'))
def test_user_change_secret_question(self):
self.create_test_data()
self._user.set_secret_answer('neW Answer')
self.assertFalse(self._user.validate_secret('test_password'))
self.assertTrue(self._user.validate_secret('new answer'))
self.assertTrue(self._user.validate_secret('newanswer'))
def test_get_user_by_pk(self):
self.create_test_data()
pk_1 = self._user.pk
_user = User().get(pk=pk_1)
self.assertIsNotNone(_user)
_user3 = User().get(pk=pk_1)
pk_2 = _user3.pk
self.assertEqual(pk_1, pk_2)
def test_get_user_by_email(self):
self.create_test_data()
email = '[email protected]'
_user_1 = User().get(email=email)
self.assertIsNotNone(_user_1)
_user_2 = User().get(email=email)
self.assertEqual(_user_2.pk, self._user.pk)
def test_get_company(self):
self.create_test_data()
self.assertEqual(self._company.pk, self._user.company.pk)
示例5: save_userinfo
# 需要导入模块: from db import User [as 别名]
# 或者: from db.User import save [as 别名]
def save_userinfo(email, hashed_password):
user = User(email=email,
password=hashed_password,
join_date=datetime.datetime.now())
user.save()