【问题标题】:Django - doing work as another userDjango - 作为另一个用户工作
【发布时间】:2012-06-25 23:43:07
【问题描述】:

我正在使用 celery,并希望代表提交请求的用户而不是“root”用户来推动会话。例如一个基本任务看起来像这样(一个非常人为的例子)

@task
def process_checklist(**kwargs):

    log = process_checklist.get_logger()
    document = kwargs.get('document', None)
    company = kwargs.get('company', None)
    user = kwargs.get('user', None)

    object = Book.object.get_or_create(name=kwargs.get('name'))

这样做需要权衡取舍,但我觉得实际使用视图来执行此操作会更有益,这与我们测试事物的方式非常相似。实际上,这用于批量上传数据,其中每一行实际上是一个 CreateView。

client = Client()
client.login(user='foo', password='bar')
client.post(reverse('create_book_view', data=**kwargs))

但我想不出一个好方法来实际使用(如果可能的话)django.test.client 客户端类在不知道密码的情况下登录用户并为他们填写视图。我想到了这个,但我确定有更好的方法吗??

这是我想出的?

class AxisClient(Client):
    # The admin account is created by us.  Once that is done everything should be tested through
    # the system.

    def login_user(self, username):
        """
        Sets the Factory to appear as if it has successfully logged into a site.

        Returns True if login is possible; False if the provided credentials
        are incorrect, or the user is inactive, or if the sessions framework is
        not available.
        """
        user = User.objects.get(username=username)
        user.backend = None
        if user and user.is_active \
                and 'django.contrib.sessions' in settings.INSTALLED_APPS:
            engine = import_module(settings.SESSION_ENGINE)

            # Create a fake request to store login details.
            request = HttpRequest()
            if self.session:
                request.session = self.session
            else:
                request.session = engine.SessionStore()
            login(request, user)

            # Save the session values.
            request.session.save()

            # Set the cookie to represent the session.
            session_cookie = settings.SESSION_COOKIE_NAME
            self.cookies[session_cookie] = request.session.session_key
            cookie_data = {
                'max-age': None,
                'path': '/',
                'domain': settings.SESSION_COOKIE_DOMAIN,
                'secure': settings.SESSION_COOKIE_SECURE or None,
                'expires': None,
            }
            self.cookies[session_cookie].update(cookie_data)

            return True
        else:
            return False

【问题讨论】:

    标签: django celery


    【解决方案1】:

    这似乎有效!

    class AxisClient(Client):
        # The admin account is created by us.  Once that is done everything should be tested through
        # the system.
    
        def login_user(self, username):
            """
            Sets the Factory to appear as if it has successfully logged into a site.
    
            Returns True if login is possible; False if the provided credentials
            are incorrect, or the user is inactive, or if the sessions framework is
            not available.
            """
            user = User.objects.get(username=username)
            user.backend = None
            if user and user.is_active \
                    and 'django.contrib.sessions' in settings.INSTALLED_APPS:
                engine = import_module(settings.SESSION_ENGINE)
    
                # Create a fake request to store login details.
                request = HttpRequest()
                if self.session:
                    request.session = self.session
                else:
                    request.session = engine.SessionStore()
                login(request, user)
    
                # Save the session values.
                request.session.save()
    
                # Set the cookie to represent the session.
                session_cookie = settings.SESSION_COOKIE_NAME
                self.cookies[session_cookie] = request.session.session_key
                cookie_data = {
                    'max-age': None,
                    'path': '/',
                    'domain': settings.SESSION_COOKIE_DOMAIN,
                    'secure': settings.SESSION_COOKIE_SECURE or None,
                    'expires': None,
                }
                self.cookies[session_cookie].update(cookie_data)
    
                return True
            else:
                return False
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2020-12-14
      相关资源
      最近更新 更多