Ticket #11416: 11416-admin-caching.2.diff

File 11416-admin-caching.2.diff, 4.1 KB (added by Michael Newman, 15 years ago)

Slightly modified version of the patch Ramiro uploaded, without some unrelated import and spacing changes and because admin_site.admin_view is used in the wrapper in the ModelAdmin views, there is no need for a decorator change in the ModelAdmin

  • django/contrib/admin/sites.py

     
    182182            if not self.has_permission(request):
    183183                return self.login(request)
    184184            return view(request, *args, **kwargs)
    185         return update_wrapper(inner, view)
     185        return never_cache(update_wrapper(inner, view))
    186186
    187187    def get_urls(self):
    188188        from django.conf.urls.defaults import patterns, url, include
  • tests/regressiontests/admin_views/tests.py

     
    5454        response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit)
    5555        self.failUnlessEqual(response.status_code, 200)
    5656
     57    def testMaxAgeModelAdminView(self):
     58        """
     59        Because cache time can be set by middleware, ensure max-age is explicity 0
     60        (non-model-specific view)
     61        """
     62        response = self.client.get('/test_admin/%s/admin_views/' % self.urlbit)
     63
     64        from django.utils.cache import get_max_age
     65        self.failUnlessEqual(get_max_age(response), 0)
     66
     67    def testMaxAgeModelView(self):
     68        """
     69        Because cache time can be set by middleware, ensure max-age is explicity 0
     70        (model-specific view)
     71        """
     72        response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit)
     73
     74        from django.utils.cache import get_max_age
     75        self.failUnlessEqual(get_max_age(response), 0)
     76
    5777    def testAddWithGETArgs(self):
    5878        response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit, {'name': 'My Section'})
    5979        self.failUnlessEqual(response.status_code, 200)
  • docs/ref/contrib/admin/index.txt

     
    757757
    758758.. note::
    759759
    760     Notice that the custom patterns are included *before* the regular admin
     760    Note how we included our custom patterns *before* the regular admin
    761761    URLs: the admin URL patterns are very permissive and will match nearly
    762762    anything, so you'll usually want to prepend your custom URLs to the built-in
    763763    ones.
    764764
    765 Note, however, that the ``self.my_view`` function registered above will *not*
    766 have any permission check done; it'll be accessible to the general public. Since
    767 this is usually not what you want, Django provides a convience wrapper to check
    768 permissions. This wrapper is :meth:`AdminSite.admin_view` (i.e.
    769 ``self.admin_site.admin_view`` inside a ``ModelAdmin`` instance); use it like
    770 so::
     765However, the ``self.my_view`` function registered above suffers from two
     766problems:
    771767
     768  * It will *not* have any permission check done; it'll be accessible to the
     769    general public.
     770  * It is not being marked as a non-cacheable and so, if it gets data from the
     771    database, it could show outdated information because of content caching being
     772    applied when the caching middleware is active.
     773
     774Since this is usually not what you want, Django provides a convenience wrapper
     775to check permissions and mark the view as non-cacheable. This wrapper is
     776:meth:`AdminSite.admin_view` (i.e.  ``self.admin_site.admin_view`` inside a
     777``ModelAdmin`` instance); use it like so::
     778
    772779    class MyModelAdmin(admin.ModelAdmin):
    773780        def get_urls(self):
    774781            urls = super(MyModelAdmin, self).get_urls()
     
    781788
    782789    (r'^my_view/$', self.admin_site.admin_view(self.my_view))
    783790
    784 This wrapping will protect ``self.my_view`` from unauthorized access.
     791This wrapping will protect ``self.my_view`` from unauthorized access and will
     792apply the ``django.views.decorators.cache.never_cache`` decorator to make sure
     793it is not cached if the cache middleware is active.
    785794
    786795.. method:: ModelAdmin.formfield_for_foreignkey(self, db_field, request, **kwargs)
    787796
Back to Top