本文整理汇总了Python中accounts.models.User.check_password方法的典型用法代码示例。如果您正苦于以下问题:Python User.check_password方法的具体用法?Python User.check_password怎么用?Python User.check_password使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类accounts.models.User
的用法示例。
在下文中一共展示了User.check_password方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _user_from_ldap
# 需要导入模块: from accounts.models import User [as 别名]
# 或者: from accounts.models.User import check_password [as 别名]
def _user_from_ldap(self, entity, password=None):
is_new = False
try:
user = User.objects.get(username=entity["uid"][0])
except User.DoesNotExist:
is_new = True
user = User(username=entity["uid"][0])
user.is_staff = True
user.is_superuser = False
user.save()
if password and user.check_password(password) is False:
user.set_password(password)
user.is_staff = True
for k, kmap in (
("first_name", "givenName"),
("last_name", "sn"),
("email", "mail"),
("gid", "gidnumber"),
("uid", "uidnumber"),
("date_joined", "whencreated"),
("country", "co"),
("department", "department"),
("photo", "thumbnailphoto"),
):
if k == "date_joined":
val = datetime.strptime(entity.get(kmap)[0][:-3], "%Y%m%d%H%M%S")
elif k == "photo" and is_new == True:
photo_data = entity.get(kmap)[0]
photo_io_origin = StringIO(photo_data)
photo_img = Image.open(photo_io_origin)
photo_side = min(photo_img.size)
photo_croped = photo_img.crop((0, 0, photo_side, photo_side))
photo_croped.thumbnail(settings.THUMBNAIL_SIZE_HEAD, Image.ANTIALIAS)
photo_io_processed = StringIO()
photo_croped.save(photo_io_processed, 'png')
photo_io_origin.close()
photo_io_processed.seek(0)
val = InMemoryUploadedFile(
photo_io_processed,
None,
"{0}_{1}.png".format(user.first_name.lower(), user.last_name.lower()),
"image/png",
len(photo_data),
None
)
else:
val = entity.get(kmap)[0]
if not val:
continue
setattr(user, k, val)
user.save()
return user
示例2: RecoverPasswordConfirmViewTest
# 需要导入模块: from accounts.models import User [as 别名]
# 或者: from accounts.models.User import check_password [as 别名]
class RecoverPasswordConfirmViewTest(BaseTestCase):
def setUp(self):
self.s = get_signer()
self.data = {
'email': u'[email protected]',
'recover-password': True
}
self.token = self.s.dumps(self.data)
self.data['email'] = u'[email protected]'
self.token2 = self.s.dumps(self.data)
self.token3 = self.token + 'a'
self.user = User(
username='user1',
email='[email protected]',
password='123456'
).save()
self.url = url_for('accounts_app.recover_password_confirm', token=self.token)
self.url2 = url_for('accounts_app.recover_password_confirm', token=self.token2)
self.url3 = url_for('accounts_app.recover_password_confirm', token=self.token3)
self.redirect_invalid = url_for('pages_app.index')
self.redirect_to = url_for('accounts_app.login')
def tearDown(self):
User.drop_collection()
def test_render(self):
# test render with invalid token
resp = self.client.get(self.url3)
self.assertRedirects(resp, self.redirect_invalid)
resp = self.client.get(self.url3, follow_redirects=True)
self.assertTrue('Invalid Link.' in resp.data)
# test render with registered email
resp = self.client.get(self.url2)
self.assertRedirects(resp, self.redirect_invalid)
resp = self.client.get(self.url2, follow_redirects=True)
self.assertTrue('E-mail not found.' in resp.data)
# test render with valid token
resp = self.client.get(self.url)
self.assertStatus(resp, 200)
def test_form(self):
# test empty form
with self.captured_templates(self.app) as templates:
resp = self.client.post(self.url)
self.assertStatus(resp, 200)
template, context = templates[0]
form = context['form']
self.assertTrue(
u'This field is required.' in form.errors['password']
)
self.assertTrue(
u'This field is required.' in form.errors['password_confirm']
)
# test validate password_confirm
with self.captured_templates(self.app) as templates:
resp = self.client.post(
self.url,
data=dict(password='123456', password_confirm='1234567')
)
self.assertStatus(resp, 200)
template, context = templates[0]
form = context['form']
self.assertTrue(
u'Incorrect password.' in form.errors['password_confirm']
)
# test valid form
resp = self.client.post(
self.url,
data=dict(
password='1234567',
password_confirm='1234567'
)
)
self.assertRedirects(resp, self.redirect_to)
self.user = User.objects.get(pk=self.user.pk)
self.assertTrue(self.user.check_password('1234567'))
# test valid form after redirect
resp = self.client.post(
self.url,
data=dict(
password='12345678',
password_confirm='12345678'
),
follow_redirects=True
)
self.assertStatus(resp, 200)
self.assertTrue(
'Password set successfully.' in resp.data
)
self.user = User.objects.get(pk=self.user.pk)
self.assertTrue(self.user.check_password('12345678'))