#31949: ASGI - Cache Middleware and some view decorators broken
--------------------------------+--------------------------------------
     Reporter:  Michael Galler  |                    Owner:  nobody
         Type:  Bug             |                   Status:  new
    Component:  Core (Other)    |                  Version:  3.1
     Severity:  Normal          |               Resolution:
     Keywords:                  |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+--------------------------------------

Comment (by Michael Galler):

 Replying to [comment:2 felixxm]:
 > > The cache middleware is broken because it inherits from
 MiddlewareMixin and override the init method.
 >
 > An issue with the MiddlewareMixin subclasses is already handled in
 #31928.
 I hadn't opened the ticket, so that's already addressed.
 >
 > You should be able to chain them with `@sync_to_async()`, if necessary.
 Am I missing sth?


 How it works for me

 {{{
     @sync_to_async
     @xframe_options_deny
     @async_to_sync
     async def get(self, request, *args, **kw):
         slug = kw['slug']
 }}}
 But I find this complicated, wouldn't it be easier if we changed the
 decorators as follows

 {{{
 def xframe_options_deny(view_func):
     def wrapped_view(*args, **kwargs):
         resp = view_func(*args, **kwargs)
         if resp.get('X-Frame-Options') is None:
             resp['X-Frame-Options'] = 'DENY'
         return resp
     return wraps(view_func)(wrapped_view)
 }}}
 to

 {{{
 def xframe_options_deny(view_func):
       def wrapped_view(*args, **kwargs):
         resp = view_func(*args, **kwargs)
         if resp.get('X-Frame-Options') is None:
             resp['X-Frame-Options'] = 'DENY'
         return resp

     async def wrapped_view_async(*args, **kwargs):
         resp = await view_func(*args, **kwargs)
         if resp.get('X-Frame-Options') is None:
             resp['X-Frame-Options'] = 'DENY'
         return resp

     return wraps(view_func)(wrapped_view_async if
 asyncio.iscoroutinefunction(view_func) else wrapped_view)
 }}}
 This also prevents the code from being executed in a different thread,
 which again leads to context changes and slightly slows down the speed

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31949#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.7e88667c1bc658dcf6e675c67715d79f%40djangoproject.com.

Reply via email to