0

我正在使用 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'))

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

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
4

1 回答 1

0

这似乎有效!

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
于 2012-07-13T15:26:54.933 回答