當前位置: 首頁>>代碼示例>>Python>>正文


Python http.urlsafe_base64_encode方法代碼示例

本文整理匯總了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 
開發者ID:awemulya,項目名稱:kobo-predict,代碼行數:26,代碼來源:password_reset_serializer.py

示例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) 
開發者ID:awemulya,項目名稱:kobo-predict,代碼行數:26,代碼來源:test_connect_viewset.py

示例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) 
開發者ID:CodeForPoznan,項目名稱:volontulo,代碼行數:27,代碼來源:api.py

示例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')) 
開發者ID:bilalzaib,項目名稱:AutoGrader,代碼行數:18,代碼來源:views.py

示例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)) 
開發者ID:incuna,項目名稱:django-user-management,代碼行數:22,代碼來源:test_views.py

示例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)) 
開發者ID:incuna,項目名稱:django-user-management,代碼行數:26,代碼來源:test_views.py

示例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',
            }) 
開發者ID:erigones,項目名稱:esdc-ce,代碼行數:19,代碼來源:forms.py

示例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)) 
開發者ID:CroceRossaItaliana,項目名稱:jorvik,代碼行數:22,代碼來源:tests.py

示例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 
開發者ID:openwisp,項目名稱:openwisp-radius,代碼行數:21,代碼來源:views.py

示例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,
    ) 
開發者ID:GamesDoneQuick,項目名稱:donation-tracker,代碼行數:29,代碼來源:auth.py

示例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')) 
開發者ID:GamesDoneQuick,項目名稱:donation-tracker,代碼行數:36,代碼來源:test_auth.py

示例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 
開發者ID:worthwhile,項目名稱:django-herald,代碼行數:35,代碼來源:notifications.py

示例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) 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:32,代碼來源:forms.py

示例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.") 
開發者ID:awemulya,項目名稱:kobo-predict,代碼行數:8,代碼來源:test_password_reset_serializer.py

示例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) 
開發者ID:awemulya,項目名稱:kobo-predict,代碼行數:6,代碼來源:models.py


注:本文中的django.utils.http.urlsafe_base64_encode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。