예제 #1
0
    def test_signin_redirect(self):
        """
        Test redirect function which should redirect the user after a
        succesfull signin.

        """
        # Test with a requested redirect
        self.failUnlessEqual(signin_redirect(redirect="/accounts/"), "/accounts/")

        # Test with only the user specified
        user = User.objects.get(pk=1)
        self.failUnlessEqual(signin_redirect(user=user), "/accounts/%s/" % user.username)

        # The ultimate fallback, probably never used
        self.failUnlessEqual(signin_redirect(), settings.LOGIN_REDIRECT_URL)
예제 #2
0
    def test_signin_redirect(self):
        """
        Test redirect function which should redirect the user after a
        succesfull signin.

        """
        # Test with a requested redirect
        self.assertEqual(signin_redirect(redirect="/accounts/"), "/accounts/")

        # Test with only the user specified
        user = get_user_model().objects.get(pk=1)
        self.assertEqual(signin_redirect(user=user),
                         "/accounts/%s/" % user.username)

        # The ultimate fallback, probably never used
        self.assertEqual(signin_redirect(), settings.LOGIN_REDIRECT_URL)
예제 #3
0
def oauth(request):
    try:
        code = request.GET.get('code', '')
        token_str = _access_token(request, code)
        token_json = json.loads(token_str)
        user = authenticate(token=token_json['access_token'],
                            uid=token_json['uid'],
                            expire_in=(60 * 60 * 24))
        if user:
            try:
                user_str = _show_user(token_json['access_token'],
                                      token_json['uid'])
                user_json = json.loads(user_str)
                user.username = user_json['screen_name']
                user.save()
            except:
                pass

            auth_login(request, user)
            profile = get_user_profile(user)
            profile.source = UserSource.objects.get(flag=100)
            profile.save()
            return HttpResponseRedirect(signin_redirect(user=user))
    except:
        return HttpResponse("很抱歉,使用新浪微博认证登录失败,请尝试从网站注册!")
예제 #4
0
    def form_valid(self, form):
        identification, password, remember_me = (
            form.cleaned_data['identification'], form.cleaned_data['password'],
            form.cleaned_data['remember_me'])
        user = authenticate(identification=identification, password=password)
        if user.is_active:
            login(self.request, user)
            if remember_me:
                self.request.session.set_expiry(
                    userena_settings.USERENA_REMEMBER_ME_DAYS[1] * 86400)
            else:
                self.request.session.set_expiry(0)

            # Whereto now?
            redirect_to = signin_redirect(
                self.request.GET.get(REDIRECT_FIELD_NAME), user)
            return HttpResponseRedirect(redirect_to)
예제 #5
0
파일: views.py 프로젝트: bjzt1016/thirtylol
def oauth(request):
    try:
        code = request.GET.get('code', '')
        token_str = _access_token(request, code)
        token_json = json.loads(token_str)
        user = authenticate(token=token_json['access_token'], uid=token_json['uid'], expire_in=(60 * 60 * 24))
        if user:
            try:
                user_str = _show_user(token_json['access_token'], token_json['uid'])
                user_json = json.loads(user_str)
                user.username = user_json['screen_name']
                user.save()
            except:
                pass
            
            auth_login(request, user)
            profile = get_user_profile(user)
            profile.source = UserSource.objects.get(flag=100)
            profile.save()
            return HttpResponseRedirect(signin_redirect(user=user))
    except:
        return HttpResponse("很抱歉,使用新浪微博认证登录失败,请尝试从网站注册!")
def login(request):
    from forms import LoginForm
    from userena import settings as userena_settings
    form = LoginForm(request.POST)
    if form.is_valid():
            if form._user.is_active:
                login(request, form._user)
                if form.cleaned_data['remember_me']:
                    request.session.set_expiry(userena_settings.USERENA_REMEMBER_ME_DAYS[1] * 86400)
                else: request.session.set_expiry(0)
                if userena_settings.USERENA_USE_MESSAGES:
                    from django.contrib import messages
                    messages.success(request, _('Bienvenido %s' % form._user.username),
                                     fail_silently=True)

                # Whereto now?
                from userena.utils import signin_redirect
                redirect_to = signin_redirect(
                    request.REQUEST.get('next'), form._user)
                return redirect(redirect_to)
            else:
                return redirect(reverse('profiles_disabled',
                                        kwargs={'username': form._user.username}))
    return HttpResponseBadRequest()