本文整理汇总了Python中django.utils.http.urlsafe_base64_encode方法的典型用法代码示例。如果您正苦于以下问题:Python http.urlsafe_base64_encode方法的具体用法?Python http.urlsafe_base64_encode怎么用?Python http.urlsafe_base64_encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.http
的用法示例。
在下文中一共展示了http.urlsafe_base64_encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_password_reset_email
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def get_password_reset_email(user, reset_url,
subject_template_name='registration/password_reset_subject.txt', # noqa
email_template_name='api_password_reset_email.html', # noqa
token_generator=default_token_generator):
"""Creates the subject and email body for password reset email."""
result = urlparse(reset_url)
site_name = domain = result.hostname
c = {
'email': user.email,
'domain': domain,
'path': result.path,
'site_name': site_name,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'username': user.username,
'encoded_username': urlsafe_base64_encode(user.username),
'token': token_generator.make_token(user),
'protocol': result.scheme if result.scheme != '' else 'http',
}
subject = loader.render_to_string(subject_template_name, c)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
email = loader.render_to_string(email_template_name, c)
return subject, email
示例2: test_reset_user_password
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def test_reset_user_password(self):
# set user.last_login, ensures we get same/valid token
# https://code.djangoproject.com/ticket/10265
self.user.last_login = now()
self.user.save()
token = default_token_generator.make_token(self.user)
new_password = "bobbob1"
data = {'token': token, 'new_password': new_password}
# missing uid, should fail
request = self.factory.post('/', data=data)
response = self.view(request)
self.assertEqual(response.status_code, 400)
data['uid'] = urlsafe_base64_encode(force_bytes(self.user.pk))
# with uid, should be successful
request = self.factory.post('/', data=data)
response = self.view(request)
self.assertEqual(response.status_code, 204)
user = User.objects.get(email=self.user.email)
self.assertTrue(user.check_password(new_password))
request = self.factory.post('/', data=data)
response = self.view(request)
self.assertEqual(response.status_code, 400)
示例3: password_reset
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def password_reset(request):
"""REST API reset password view"""
serializer = UsernameSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
username = serializer.validated_data.get('username')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
pass
else:
context = {
'email': username,
'domain': get_current_site(request).domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'user': user,
'token': default_token_generator.make_token(user),
'protocol': 'https' if request.is_secure() else 'http',
}
send_mail(
request,
'password_reset',
[username],
context=context,
send_copy_to_admin=False)
return Response(dict(), status=status.HTTP_201_CREATED)
示例4: resend_signup_email
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def resend_signup_email(request):
user = request.user
current_site = get_current_site(request)
subject = 'Activate Your FAST AutoGrader Account'
message = render_to_string('account/account_activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
user.email_user(subject, message)
messages.success(request, 'Verification email sent, check your email account.')
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
示例5: test_put
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def test_put(self):
old_password = '0ld_passworD'
new_password = 'n3w_Password'
user = UserFactory.create(password=old_password)
token = default_token_generator.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk))
request = self.create_request(
'put',
data={'new_password': new_password, 'new_password2': new_password},
auth=False,
)
view = self.view_class.as_view()
response = view(request, uidb64=uid, token=token)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Get the updated user from the db
user = User.objects.get(pk=user.pk)
self.assertTrue(user.check_password(new_password))
示例6: test_password_mismatch
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def test_password_mismatch(self):
old_password = '0ld_passworD'
new_password = 'n3w_Password'
invalid_password = 'different_new_password'
user = UserFactory.create(password=old_password)
token = default_token_generator.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk))
request = self.create_request(
'put',
data={
'new_password': new_password,
'new_password2': invalid_password,
},
auth=False,
)
view = self.view_class.as_view()
response = view(request, uidb64=uid, token=token)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
# Get the updated user from the db
user = User.objects.get(pk=user.pk)
self.assertTrue(user.check_password(old_password))
示例7: save
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def save(self, domain_override=None,
subject_template_name='registration/password_reset_subject.txt',
email_template_name='registration/password_reset_email.html',
use_https=False, token_generator=default_token_generator,
**kwargs):
# Complete override, because we have to use our sendmail()
for user in self.users_cache:
# Update verification token
profile = user.userprofile
profile.email_token = token_generator.make_token(user)
profile.save()
sendmail(user, subject_template_name, email_template_name, extra_context={
'user': user,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': profile.email_token,
'protocol': use_https and 'https' or 'http',
})
示例8: test_recupero_password_link_valido
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def test_recupero_password_link_valido(self):
presidente = crea_persona()
persona, sede, app = crea_persona_sede_appartenenza(presidente=presidente)
persona_in_sede = crea_persona()
utenza_persona_in_sede = crea_utenza(persona_in_sede)
appartenenza_persona_in_sede = crea_appartenenza(persona, sede)
uid = urlsafe_base64_encode(force_bytes(utenza_persona_in_sede.pk))
reset_pw_link = default_token_generator.make_token(utenza_persona_in_sede)
sessione = self.sessione_anonimo()
sessione.visit("%s%s" % (self.live_server_url, reverse('recupera_password_conferma', kwargs={ 'uidb64': uid, 'token': reset_pw_link})))
sessione.fill('new_password1', 'new_password')
sessione.fill('new_password2', 'new_password')
sessione.find_by_css('.btn.btn-block.btn-primary').first.click()
self.assertTrue(sessione.is_text_present('La tua nuova password è stata impostata'))
sessione.visit("%s%s" % (self.live_server_url, '/login/'))
sessione.fill('auth-username', utenza_persona_in_sede.email)
sessione.fill('auth-password', 'new_password')
sessione.find_by_css('.btn.btn-block.btn-primary').first.click()
testo_personalizzato = 'Ciao, {0}'.format(persona_in_sede.nome)
self.assertTrue(sessione.is_text_present(testo_personalizzato))
示例9: get_serializer_context
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def get_serializer_context(self):
user = self.request.user
if not user.pk:
return
uid = urlsafe_base64_encode(force_bytes(user.pk))
# until django 2.1 urlsafe_base64_encode returned a bytestring
if not isinstance(uid, str): # noqa
uid = uid.decode()
token = default_token_generator.make_token(user)
password_reset_urls = app_settings.PASSWORD_RESET_URLS
default_url = password_reset_urls.get('default')
password_reset_url = password_reset_urls.get(
str(self.organization.pk), default_url
)
password_reset_url = password_reset_url.format(
organization=self.organization.slug, uid=uid, token=token
)
context = {'request': self.request, 'password_reset_url': password_reset_url}
return context
示例10: send_registration_mail
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def send_registration_mail(
request,
user,
template=None,
sender=None,
token_generator=default_token_generator,
extra_context=None,
):
template = template or mailutil.get_email_template(
default_registration_template_name(), default_registration_template()
)
return send_auth_token_mail(
user,
request.get_host(),
request.build_absolute_uri(
reverse(
'tracker:confirm_registration',
kwargs={
'uidb64': urlsafe_base64_encode(force_bytes(user.pk)),
'token': token_generator.make_token(user),
},
)
),
template,
sender,
extra_context,
)
示例11: test_registration_flow
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def test_registration_flow(self):
request = self.factory.post(reverse('tracker:register'))
new_user = AuthUser.objects.create(
username='dummyuser', email='test@email.com', is_active=False
)
sent_mail = tracker.auth.send_registration_mail(
request, new_user, template=TEST_AUTH_MAIL_TEMPLATE
)
contents = test_util.parse_test_mail(sent_mail)
self.assertEqual(new_user.username, contents['user'][0])
parsed = urllib.parse.urlparse(contents['url'][0])
resp = self.client.get(parsed.path)
expected_url = reverse(
'tracker:confirm_registration',
kwargs={
'uidb64': urlsafe_base64_encode(force_bytes(new_user.pk)),
'token': 'register-user',
},
)
self.assertRedirects(resp, expected_url)
resp = self.client.get(expected_url)
self.assertContains(resp, 'Please set your username and password.')
resp = self.client.post(
expected_url,
{
'username': 'dummyuser',
'password': 'foobar',
'passwordconfirm': 'foobar',
},
)
self.assertContains(resp, 'Your user account has been confirmed')
new_user.refresh_from_db()
self.assertTrue(new_user.is_active)
self.assertTrue(new_user.check_password('foobar'))
示例12: get_context_data
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def get_context_data(self):
context = super(PasswordResetEmail, self).get_context_data()
if not self.site_name or self.domain:
current_site = Site.objects.get_current()
self.site_name = current_site.name
self.domain = current_site.domain
protocol = 'https' if self.use_https else 'http'
uid = force_text(urlsafe_base64_encode(force_bytes(self.user.pk)))
token = self.token_generator.make_token(self.user)
context.update({
'full_reset_url': '{}://{}{}'.format(
protocol,
self.domain,
reverse('password_reset_confirm', kwargs={'uidb64': uid, 'token': token})
),
'email': self.user.email,
'domain': self.domain,
'site_name': self.site_name,
'uid': uid,
'user': self.user,
'token': token,
'protocol': protocol,
'template_name': self.email_template_name,
'html_template_name': self.html_email_template_name,
})
if self.extra_email_context is not None:
context.update(self.extra_email_context)
return context
示例13: save
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def save(self, domain_override=None,
subject_template_name='registration/password_reset_subject.txt',
email_template_name='registration/password_reset_email.html',
use_https=False, token_generator=default_token_generator,
from_email=None, request=None, html_email_template_name=None):
"""
Generates a one-use only link for resetting password and sends to the
user.
"""
email = self.cleaned_data["email"]
for user in self.get_users(email):
if not domain_override:
current_site = get_current_site(request)
site_name = current_site.name
domain = current_site.domain
else:
site_name = domain = domain_override
context = {
'email': user.email,
'domain': domain,
'site_name': site_name,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'user': user,
'token': token_generator.make_token(user),
'protocol': 'https' if use_https else 'http',
}
self.send_mail(subject_template_name, email_template_name,
context, from_email, user.email,
html_email_template_name=html_email_template_name)
示例14: test_get_password_reset_email
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def test_get_password_reset_email(self):
"""Test base64 username is included in reset email."""
subject, email = get_password_reset_email(self.user, 'https://ona.io')
self.assertIn(urlsafe_base64_encode(self.user.username), email,
"Username is included in reset email.")
示例15: get_absolute_url
# 需要导入模块: from django.utils import http [as 别名]
# 或者: from django.utils.http import urlsafe_base64_encode [as 别名]
def get_absolute_url(self):
invite_idb64 = urlsafe_base64_encode(force_bytes(self.pk))
kwargs = {'invite_idb64': invite_idb64, 'token': self.token}
return reverse('fieldsight:activate-role', kwargs=kwargs)