Skip to content

Commit

Permalink
Use get_cache_expiration(), also fix spelling and indention
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoistinen committed Mar 21, 2016
1 parent 5d4f4ad commit a6c942e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cms/cache/page.py
Expand Up @@ -51,9 +51,9 @@ def set_page_cache(response):
# Checks if there's a plugin using the legacy "cache = False"
if all(ph.cache_placeholder for ph in placeholders):
placeholder_ttl_list = [
# get_expiration() always returns:
# get_cache_expiration() always returns:
# EXPIRE_NOW <= int <= MAX_EXPIRATION_IN_SECONDS
ph.get_expiration(request, timestamp)
ph.get_cache_expiration(request, timestamp)
for ph in placeholders
]

Expand Down
10 changes: 5 additions & 5 deletions cms/models/placeholdermodel.py
Expand Up @@ -305,7 +305,7 @@ def actions(self):
self._actions_cache = getattr(field, 'actions', PlaceholderNoAction())
return self._actions_cache

def get_expiration(self, request, response_timestamp):
def get_cache_expiration(self, request, response_timestamp):
"""
Returns the number of seconds (from «response_timestamp») that this
placeholder can be cached. This is derived from the plugins it contains.
Expand All @@ -326,7 +326,7 @@ def get_expiration(self, request, response_timestamp):
# This placeholder has a plugin with an effective
# `cache = False` setting, so, no point in continuing.
return EXPIRE_NOW
plugin_expiration = plugin.get_expiration(request, instance)
plugin_expiration = plugin.get_cache_expiration(request, instance)

# The plugin_expiration should only ever be either: None, a TZ-aware
# datetime, or an integer.
Expand All @@ -345,7 +345,7 @@ def get_expiration(self, request, response_timestamp):
# this plugin.
warnings.warn(
'Plugin %(plugin_class)s (%(pk)d) returned a naive '
'datetime : %(value)s for get_expiration(), '
'datetime : %(value)s for get_cache_expiration(), '
'ignoring.' % {
'plugin_class': plugin.__class__.__name__,
'pk': instance.pk,
Expand All @@ -363,8 +363,8 @@ def get_expiration(self, request, response_timestamp):
# Looks like it was not very int-ish. Ignore this plugin.
warnings.warn(
'Plugin %(plugin_class)s (%(pk)d) return '
'unexpected value %(value)s for get_expiration(), '
'ignoring.' % {
'unexpected value %(value)s for '
'get_cache_expiration(), ignoring.' % {
'plugin_class': plugin.__class__.__name__,
'pk': instance.pk,
'value': force_text(plugin_expiration),
Expand Down
2 changes: 1 addition & 1 deletion cms/plugin_base.py
Expand Up @@ -174,7 +174,7 @@ def get_require_parent(cls, slot, page):
def parent(self):
return self.cms_plugin_instance.parent

def get_expiration(self, request, instance):
def get_cache_expiration(self, request, instance):
"""
Provides hints to the placeholder, and in turn to the page for
determining the appropriate Cache-Control headers to add to the
Expand Down
Expand Up @@ -27,7 +27,7 @@ class LegacyCachePlugin(CMSPluginBase):
render_template = "plugins/nocache.html"

# And the new...
def get_expiration(self, request, instance):
def get_cache_expiration(self, request, instance):
"""Content is only valid until for 30 seconds."""
return 30

Expand All @@ -42,7 +42,7 @@ class TTLCacheExpirationPlugin(CMSPluginBase):
render_plugin = True
render_template = "plugins/nocache.html"

def get_expiration(self, request, instance):
def get_cache_expiration(self, request, instance):
"""Content is only valid for the next 50 seconds."""
return 50

Expand All @@ -57,7 +57,7 @@ class TimeDeltaCacheExpirationPlugin(CMSPluginBase):
render_plugin = True
render_template = "plugins/nocache.html"

def get_expiration(self, request, instance):
def get_cache_expiration(self, request, instance):
"""Content is only valid for the next 45 seconds."""
return timedelta(seconds=45)

Expand All @@ -72,7 +72,7 @@ class DateTimeCacheExpirationPlugin(CMSPluginBase):
render_plugin = True
render_template = "plugins/nocache.html"

def get_expiration(self, request, instance):
def get_cache_expiration(self, request, instance):
"""Content is only valid until 40 seconds from now."""
now = timezone.now()
return now + timedelta(seconds=40)
Expand Down
17 changes: 8 additions & 9 deletions cms/tests/test_cache.py
Expand Up @@ -354,7 +354,6 @@ def test_enable_legacy_cache_plugins(self):

plugin_pool.unregister_plugin(LegacyCachePlugin)


def test_disable_legacy_cache_plugins(self):
page1 = create_page('test page 1', 'nav_playground.html', 'en',
published=True)
Expand All @@ -379,14 +378,14 @@ def test_disable_legacy_cache_plugins(self):
# from django.core.cache import cache; cache.clear()

with self.settings(MIDDLEWARE_CLASSES=mw_classes, CMS_IGNORE_PLUGIN_CACHE_ATTRIBUTE=['LegacyCachePlugin', ]): # noqa
page1.publish('en')
request = self.get_request('/en/')
request.current_page = Page.objects.get(pk=page1.pk)
request.toolbar = CMSToolbar(request)
with self.assertNumQueries(FuzzyInt(14, 25)): # was 14, 24
response = self.client.get('/en/')
self.assertTrue('no-cache' not in response['Cache-Control'], response['Cache-Control'])
self.assertTrue('max-age=30' in response['Cache-Control'], response['Cache-Control'])
page1.publish('en')
request = self.get_request('/en/')
request.current_page = Page.objects.get(pk=page1.pk)
request.toolbar = CMSToolbar(request)
with self.assertNumQueries(FuzzyInt(14, 25)):
response = self.client.get('/en/')
self.assertTrue('no-cache' not in response['Cache-Control'])
self.assertTrue('max-age=30' in response['Cache-Control'])

plugin_pool.unregister_plugin(LegacyCachePlugin)

Expand Down
14 changes: 7 additions & 7 deletions docs/how_to/custom_plugins.rst
Expand Up @@ -150,7 +150,7 @@ In ``cms_plugins.py``, you place your plugins. For our example, include the foll
model = CMSPlugin
render_template = "hello_plugin.html"

def get_expiration(self, **kwargs):
def get_cache_expiration(self, **kwargs):
return 0

plugin_pool.register_plugin(HelloPlugin)
Expand Down Expand Up @@ -196,22 +196,22 @@ is ``True`` (the default):
plugin with.

In addition to those attributes, you can also define a :meth:`render` and/or
:meth:`get_expiration` method on your sub-classes.
:meth:`get_cache_expiration` method on your sub-classes.

The :ref:`render` method determines the template context variables that are
used render your plugin.

The :meth:`get_expiration` method is optional and is used to determine the
The :meth:`get_cache_expiration` method is optional and is used to determine the
period of validity of the content rendered by the plugin. A value of 0 means
"do not cache". This method can return a future (timezone-aware) date and time,
a ``timedelta`` or can simply return the number of seconds that the content
is cache-able.

This method is not required. By default, a plugin will not affect the cache-
ability of the page it is on. In the `HelloPlugin` above, :meth:`get_expiration`
was used to return 0 ("no-cache") because we want each visitor to see output
that is specific to him or her, we need to tell the cms to not cache any pages
where this plugin is used.
ability of the page it is on. In the `HelloPlugin` above,
:meth:`get_cache_expiration` was used to return 0 ("no-cache") because we want
each visitor to see output that is specific to him or her, we need to tell the
cms to not cache any pages where this plugin is used.


***************
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/api_references.rst
Expand Up @@ -269,7 +269,7 @@ cms.plugin_base

.. important:: This attribute is now deprecated and will be removed in a
future release. Its functionality is replaced by the much
more capable :meth:`get_expiration`.
more capable :meth:`get_cache_expiration`.

Adding the plugin's class name to the setting :setting:`IGNORE_PLUGIN_CACHE_ATTRIBUTE`
will disable the effects of setting ``cache`` to False. This makes it
Expand Down Expand Up @@ -328,7 +328,7 @@ cms.plugin_base
Returns the URL to the icon to be used for the given instance when that
instance is used inside a text plugin.

.. method:: get_expiration(request, instance)
.. method:: get_cache_expiration(request, instance)

Provides expiration value to the placeholder, and in turn to the page
for determining the appropriate Cache-Control headers to add to the
Expand Down Expand Up @@ -377,7 +377,7 @@ cms.plugin_base

from cms.constants import EXPIRE_NOW

def get_expiration(self, **kwargs):
def get_cache_expiration(self, **kwargs):
return EXPIRE_NOW

:param request: Relevant ``HTTPRequest`` instance.
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/configuration.rst
Expand Up @@ -185,8 +185,8 @@ default

Setting the ``cache`` to ``False`` on a plugin whose class name is in
this list will have no effect. This allows plugin developers wishing to provide
support for :meth:`get_expiration` for CMS 3.3 and later while still supporting
older CMS versions with ``cache`` set to ``False`` if necessary.
support for :meth:`get_cache_expiration` for CMS 3.3 and later while still
supporting older CMS versions with ``cache`` set to ``False`` if necessary.


.. setting:: CMS_PLACEHOLDER_CONF
Expand Down
4 changes: 2 additions & 2 deletions docs/upgrade/3.3.rst
Expand Up @@ -19,8 +19,8 @@ What's new in 3.3
* Set the default value of CMSPlugin.position to 0 instead of null
* Management commands has been completely refactored for better consistency (see
`backward_incompatible_3.3`_ for details)
* Introduction of the method `get_expiration` on CMSPluginBase to be used by
plugins for declaring their rendered content's period of validity. This
* Introduction of the method `get_cache_expiration` on CMSPluginBase to be used
by plugins for declaring their rendered content's period of validity. This
replaces the functionality provided by the class attribute CMSPluginBase.cache
which is now deprecated and will be removed in version 3.4

Expand Down

0 comments on commit a6c942e

Please sign in to comment.