简体   繁体   English

Django + Deliciouspie-user_logged_in信号不起作用

[英]Django + Tastypie - user_logged_in signal doesn't work

I have a Tastypie ModelResource defining various endpoints which work as expected. 我有一个Tastypie ModelResource定义了可以按预期工作的各种端点。

I've now configured this ModelResource to have BasicAuthentication : 现在,我已经将此ModelResource配置为具有BasicAuthentication

class Meta:
    authentication = BasicAuthentication()

I've defined a couple of test users through the Django Admin Interface. 我已经通过Django Admin Interface定义了一些测试用户。

As per the Django 1.7 Documentation , I've created a signals.py in which I register a couple of test signals: 根据Django 1.7文档 ,我创建了一个signals.py ,其中注册了一些测试信号:

from django.core.signals import request_finished
from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver

@receiver(user_logged_in)
def on_login(sender, request, user, **kwargs):
    print('******* LOGIN DETECTED *********')

@receiver(request_finished)
def on_request_finished(sender, **kwargs):
    print('******* REQUEST FINISHED *******')

This is loaded successfully by my AppConfig in apps.py : 这是由我的AppConfigapps.py成功加载的:

from django.apps import AppConfig

class MyAppConfig(AppConfig):
    name = 'myapp'
    verbose_name = 'verbose description of myapp'

    def ready(self):
        import myapp.signals

I use the Requests library to successfully communicate with my API, providing basic authentication credentials for one of my test users: 我使用Requests库成功地与我的API通信,从而为我的一个测试用户提供了基本的身份验证凭据:

auth = HTTPBasicAuth(username, getpass('Enter password: '))
response = requests.get(self.url(endpoint), auth=self.auth, params = params)

The REQUEST FINISHED print shows in the Django server's output, but LOGIN DETECTED does not. REQUEST FINISHED打印显示在Django服务器的输出中,但LOGIN DETECTED没有显示。

Do we have to manually fire a login signal when using Tastypie, or use some other inbuilt/custom Authentication class besides BasicAuthentication ? 在使用Tastypie时,我们是否必须手动触发登录信号,或者使用除BasicAuthentication之外的其他内置/自定义Authentication类? In other words, is it expected that the user_logged_in signal wouldn't fire automatically? 换句话说,是否期望user_logged_in信号不会自动触发?

Any info would be greatly appreciated. 任何信息,将不胜感激。

Having inspected the Tastypie source code, it ties into the Django auth backend by calling the authenticate method, thus doesn't trigger the usual login cycle of which authenticate is one component. 检查完Tastypie源代码后,它通过调用authenticate方法绑定到Django auth后端,因此不会触发通常将authenticate作为一个组件的login周期。 Consequently the login method is never called, and thus the user_logged_in signal never fires. 因此,永远不会调用login方法,因此永远不会触发user_logged_in信号。

I ended up providing the signal myself by extending BasicAuthentication and overriding is_authenticated like so: 我最终通过扩展BasicAuthentication并覆盖is_authenticated自己提供了信号,如下所示:

class MyBasicAuthentication(BasicAuthentication):
    def is_authenticated(self, request, **kwargs):
        orig_user = request.user
        has_authenticated = super(MyBasicAuthentication, self).is_authenticated(request, **kwargs)
        if has_authenticated:
            was_authenticated = orig_user == request.user
            if not was_authenticated:
                user_logged_in.send(sender=self.__class__, request=request, user=request.user)
        return has_authenticated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django rest auth user_logged_in 信号 - Django rest auth user_logged_in signal 无法引用Django中注册到user_logged_in信号的回调函数中的模型 - Can't refer to models in callback function registered to user_logged_in signal in Django 在Django中使用all-auth接收到user_logged_in信号后,将用户设置为AnonymousUser - User is set to AnonymousUser after a user_logged_in signal is received in Django with all-auth 自定义user_logged_in信号之前django更新last_login - Custom user_logged_in signal BEFORE django updates last_login 运行测试用例时,Django的user_logged_in信号不起作用 - Django's user_logged_in signal not working when running a test case 我想使用在django allauth中注销后发出的信号,就像user_logged_in - I want to use a signal which emitt just after log out in django allauth, just like user_logged_in 我的 django 创建用户和配置文件信号不起作用 - My django create user and profile signal doesn't work Django Tastypie-OneToOne关系POST不起作用 - Django Tastypie - OneToOne Relationship POST doesn't work 检查登录用户是否是帖子的作者 | Django | 如果-其他不起作用 - Check if logged in user is the author of a post | Django | If-else doesn't work django post_delete()信号处理程序不起作用 - django post_delete() signal handler doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM