Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#13093 closed (fixed)

cache_page decorator doesn't work on callable classes

Reported by: russellm Owned by: nobody
Component: Core (Cache system) Version: 1.2-beta
Severity: Keywords:
Cc: gremmie Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by russellm)

The cache_page() decorator works fine on views, but fails if you use it on a callable class::

class MyView(object):
    def __call__(self, request):
        return HttpResponse()

If you try to wrap this view in urls.py ( cache_page(MyView(), 60) ), you get an error saying "MyView doesn't have attribute __name__". cache_page uses the __name__ attribute to build a cache key, but classes dont have a __name__ attribute. cache_page should also check for __class__.__name__ (or some similar key with a per-instance feature) to allow for wrapping callable classes.

This is a problem for 1.2, because feed views have been moved to a class-based structure, so it is no longer possible to cache feed views using cache_page().

Attachments (0)

Change History (7)

comment:1 Changed 4 years ago by russellm

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Forgot to mention: Thanks to Brian Neal for the report.

comment:2 Changed 4 years ago by gremmie

  • Cc gremmie added

comment:3 Changed 4 years ago by russellm

It turns out that this is actually due to a bug in Python itself; functools.wraps doesn't check to see if an attribute is available before copying it onto the wrapped view. Luckily, the API for wraps includes a feature that can be used as a workaround; a patch should be committed shortly.

comment:4 Changed 4 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [12762]) Fixed #13093 -- Updated some decorators and the decorator_from_middleware function to allow callable classes to be decorated. Thanks to Brian Neal for the report.

comment:5 Changed 4 years ago by russellm

(In [12763]) [1.1.X] Fixed #13093 -- Updated some decorators and the decorator_from_middleware function to allow callable classes to be decorated. Thanks to Brian Neal for the report.

Backport of r12762 from trunk.

comment:6 Changed 4 years ago by kmtracey

The backport to 1.1.X here caused bunches of test errors on 1.1.X. See #13111.

comment:7 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.