Code Monkey home page Code Monkey logo

Comments (6)

pennersr avatar pennersr commented on June 5, 2024

allauth currently does not support async, see #3546. Likely, this needs to be applied to the middleware:

https://docs.djangoproject.com/en/4.2/topics/http/middleware/#asynchronous-support

Can you check if that fixes the issue? A PR is welcome.

from django-allauth.

pennersr avatar pennersr commented on June 5, 2024

I have a fix for this in the pipeline...

from django-allauth.

jrobichaud avatar jrobichaud commented on June 5, 2024

This could work I think:

from asgiref.sync import iscoroutinefunction, markcoroutinefunction
from asgiref import sync
from allauth.core import context
from allauth.account import middleware


class AccountMiddleware(middleware.AccountMiddleware):
    async_capable = True
    sync_capable = True

    def __init__(self, get_response):
        self.get_response = get_response
        if iscoroutinefunction(self.get_response):
            markcoroutinefunction(self)

    def __call__(self, request):
        if iscoroutinefunction(self):
            return self.__acall__(request)
        return super().__call__(request)

    async def __acall__(self, request):
        with context.request_context(request):
            response = await self.get_response(request)
            await sync.sync_to_async(self._remove_dangling_login)(request, response)
            return response

I did it using inheritance but the classes would have to be merged.

from django-allauth.

pennersr avatar pennersr commented on June 5, 2024

Doesn't sync_to_async spawn a thread to run the sync code? In any case, I have avoided that here #3568 -- also not very pretty, doubling code, but it's what Django recommends.

from django-allauth.

jrobichaud avatar jrobichaud commented on June 5, 2024

You are right, it is not required.

from django-allauth.

jrobichaud avatar jrobichaud commented on June 5, 2024

Updated:

from asgiref.sync import iscoroutinefunction, markcoroutinefunction
from allauth.core import context
from allauth.account import middleware


class AccountMiddleware(middleware.AccountMiddleware):
    async_capable = True
    sync_capable = True

    def __init__(self, get_response):
        self.get_response = get_response
        if iscoroutinefunction(self.get_response):
            markcoroutinefunction(self)

    def __call__(self, request):
        if iscoroutinefunction(self):
            return self.__acall__(request)
        return super().__call__(request)

    async def __acall__(self, request):
        with context.request_context(request):
            response = await self.get_response(request)
            self._remove_dangling_login(request, response)
            return response

from django-allauth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.