Django + Deliciouspie

编程入门 行业动态 更新时间:2024-10-26 12:31:44
本文介绍了Django + Deliciouspie-user_logged_in信号不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个趣味的ModelResource,它定义了可以按预期工作的各种端点.

I have a Tastypie ModelResource defining various endpoints which work as expected.

我现在已经将此ModelResource配置为具有BasicAuthentication:

I've now configured this ModelResource to have BasicAuthentication:

class Meta: authentication = BasicAuthentication()

我已经通过Django Admin界面定义了一些测试用户.

I've defined a couple of test users through the Django Admin Interface.

根据 Django 1.7文档,我创建了一个signals.py我在其中注册了几个测试信号:

As per the Django 1.7 Documentation, I've created a signals.py in which I register a couple of test signals:

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 *******')

这是我的AppConfig在apps.py中成功加载的:

This is loaded successfully by my AppConfig in apps.py:

from django.apps import AppConfig class MyAppConfig(AppConfig): name = 'myapp' verbose_name = 'verbose description of myapp' def ready(self): import myapp.signals

我使用Requests库成功与我的API通信,从而为我的一个测试用户提供了基本的身份验证凭据:

I use the Requests library to successfully communicate with my API, providing basic authentication credentials for one of my test users:

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

REQUEST FINISHED打印显示在Django服务器的输出中,但LOGIN DETECTED没有显示.

The REQUEST FINISHED print shows in the Django server's output, but LOGIN DETECTED does not.

在使用Tastypie时,我们是否必须手动触发登录信号,或者使用除BasicAuthentication之外的其他内置/自定义Authentication类?换句话说,是否期望user_logged_in信号不会自动触发?

Do we have to manually fire a login signal when using Tastypie, or use some other inbuilt/custom Authentication class besides BasicAuthentication? In other words, is it expected that the user_logged_in signal wouldn't fire automatically?

任何信息将不胜感激.

推荐答案

已经检查了Thastypie源代码,通过调用authenticate方法将其绑定到Django auth后端,因此不会触发通常的login周期其中authenticate是一个组件.因此,永远不会调用login方法,因此永远不会触发user_logged_in信号.

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. Consequently the login method is never called, and thus the user_logged_in signal never fires.

我最终通过扩展BasicAuthentication并覆盖is_authenticated自己提供了信号,就像这样:

I ended up providing the signal myself by extending BasicAuthentication and overriding is_authenticated like so:

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

更多推荐

Django + Deliciouspie

本文发布于:2023-11-24 11:14:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django   Deliciouspie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!