Skip to content
This repository has been archived by the owner on Oct 29, 2019. It is now read-only.

Commit

Permalink
Enabled configurable plugin caching for CMS 3.3.0+ installations (#407)
Browse files Browse the repository at this point in the history
* Re-enable caching for CMS 3.3.0+ installations

* Updated for review comments

* Added ability to set cache_duration for plugin on CMS 3.3.0+

* Fixed tox.ini

* Updated for review comments
  • Loading branch information
mkoistinen committed Jun 24, 2016
1 parent e41e600 commit 831f614
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 14 deletions.
43 changes: 37 additions & 6 deletions aldryn_newsblog/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

from __future__ import unicode_literals

from distutils.version import LooseVersion
from django.utils.translation import ugettext_lazy as _

from cms import __version__ as cms_version
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool

from . import models, forms
from .utils import add_prefix_to_path, default_reverse

CMS_GTE_330 = LooseVersion(cms_version) >= LooseVersion('3.3.0')


class TemplatePrefixMixin(object):

Expand All @@ -27,11 +31,39 @@ class NewsBlogPlugin(TemplatePrefixMixin, CMSPluginBase):
module = 'News & Blog'


class AdjustableCacheMixin(object):
"""
For django CMS < 3.3.0 installations, we have no choice but to disable the
cache where there is time-sensitive information. However, in later CMS
versions, we can configure it with `get_cache_expiration()`.
"""
if not CMS_GTE_330:
cache = False

def get_cache_expiration(self, request, instance, placeholder):
return getattr(instance, 'cache_duration', 0)

def get_fieldsets(self, request, obj=None):
"""
Removes the cache_duration field from the displayed form if we're not
using django CMS v3.3.0 or later.
"""
fieldsets = super(AdjustableCacheMixin, self).get_fieldsets(request, obj=None)
if CMS_GTE_330:
return fieldsets

field = 'cache_duration'
for fieldset in fieldsets:
new_fieldset = [
item for item in fieldset[1]['fields'] if item != field]
fieldset[1]['fields'] = tuple(new_fieldset)
return fieldsets


@plugin_pool.register_plugin
class NewsBlogArchivePlugin(NewsBlogPlugin):
class NewsBlogArchivePlugin(AdjustableCacheMixin, NewsBlogPlugin):
render_template = 'aldryn_newsblog/plugins/archive.html'
name = _('Archive')
cache = False
model = models.NewsBlogArchivePlugin
form = forms.NewsBlogArchivePluginForm

Expand Down Expand Up @@ -112,10 +144,9 @@ def render(self, context, instance, placeholder):


@plugin_pool.register_plugin
class NewsBlogLatestArticlesPlugin(NewsBlogPlugin):
class NewsBlogLatestArticlesPlugin(AdjustableCacheMixin, NewsBlogPlugin):
render_template = 'aldryn_newsblog/plugins/latest_articles.html'
name = _('Latest Articles')
cache = False
model = models.NewsBlogLatestArticlesPlugin
form = forms.NewsBlogLatestArticlesPluginForm

Expand All @@ -127,11 +158,11 @@ def render(self, context, instance, placeholder):


@plugin_pool.register_plugin
class NewsBlogRelatedPlugin(NewsBlogPlugin):
class NewsBlogRelatedPlugin(AdjustableCacheMixin, NewsBlogPlugin):
render_template = 'aldryn_newsblog/plugins/related_articles.html'
name = _('Related Articles')
cache = False
model = models.NewsBlogRelatedPlugin
form = forms.NewsBlogRelatedPluginForm

def get_article(self, request):
if request and request.resolver_match:
Expand Down
12 changes: 10 additions & 2 deletions aldryn_newsblog/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, *args, **kwargs):
class NewsBlogArchivePluginForm(AutoAppConfigFormMixin, forms.ModelForm):
class Meta:
model = models.NewsBlogArchivePlugin
fields = ['app_config']
fields = ['app_config', 'cache_duration']


class NewsBlogArticleSearchPluginForm(AutoAppConfigFormMixin, forms.ModelForm):
Expand Down Expand Up @@ -52,9 +52,17 @@ class NewsBlogLatestArticlesPluginForm(AutoAppConfigFormMixin,
forms.ModelForm):
class Meta:
model = models.NewsBlogLatestArticlesPlugin
fields = ['app_config', 'latest_articles', 'exclude_featured']
fields = [
'app_config', 'latest_articles', 'exclude_featured',
'cache_duration'
]


class NewsBlogTagsPluginForm(AutoAppConfigFormMixin, forms.ModelForm):
class Meta:
fields = ['app_config']


class NewsBlogRelatedPluginForm(forms.ModelForm):
class Meta:
fields = ['cache_duration']
36 changes: 36 additions & 0 deletions aldryn_newsblog/migrations/0013_auto_20160623_1703.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-23 21:03
from __future__ import unicode_literals

import app_data.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('aldryn_newsblog', '0012_auto_20160503_1626'),
]

operations = [
migrations.AddField(
model_name='newsblogarchiveplugin',
name='cache_duration',
field=models.PositiveSmallIntegerField(default=0, help_text="The maximum duration (in seconds) that this plugin's contentshould be cached. Values between 5 minutes (300) and one day (84600) recommended)."),
),
migrations.AddField(
model_name='newsbloglatestarticlesplugin',
name='cache_duration',
field=models.PositiveSmallIntegerField(default=0, help_text="The maximum duration (in seconds) that this plugin's contentshould be cached. Values between 5 minutes (300) and one day (84600) recommended)."),
),
migrations.AddField(
model_name='newsblogrelatedplugin',
name='cache_duration',
field=models.PositiveSmallIntegerField(default=0, help_text="The maximum duration (in seconds) that this plugin's contentshould be cached. Values between 5 minutes (300) and one day (84600) recommended)."),
),
migrations.AlterField(
model_name='newsblogconfig',
name='app_data',
field=app_data.fields.AppDataField(default='{}', editable=False),
),
]
25 changes: 22 additions & 3 deletions aldryn_newsblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ def get_edit_mode(self, request):
request.toolbar.edit_mode)


class AdjustableCacheModelMixin(models.Model):
# NOTE: This field shouldn't even be displayed in the plugin's change form
# if using django CMS < 3.3.0
cache_duration = models.PositiveSmallIntegerField(
default=0, # not the most sensible, but consistent with older versions
blank=False,
help_text=_(
"The maximum duration (in seconds) that this plugin's content "
"should be cached.")
)

class Meta:
abstract = True


class NewsBlogCMSPlugin(CMSPlugin):
"""AppHookConfig aware abstract CMSPlugin class for Aldryn Newsblog"""
# avoid reverse relation name clashes by not adding a related_name
Expand All @@ -265,7 +280,8 @@ def copy_relations(self, old_instance):


@python_2_unicode_compatible
class NewsBlogArchivePlugin(PluginEditModeMixin, NewsBlogCMSPlugin):
class NewsBlogArchivePlugin(PluginEditModeMixin, AdjustableCacheModelMixin,
NewsBlogCMSPlugin):
# NOTE: the PluginEditModeMixin is eventually used in the cmsplugin, not
# here in the model.
def __str__(self):
Expand Down Expand Up @@ -403,7 +419,9 @@ def __str__(self):


@python_2_unicode_compatible
class NewsBlogLatestArticlesPlugin(PluginEditModeMixin, NewsBlogCMSPlugin):
class NewsBlogLatestArticlesPlugin(PluginEditModeMixin,
AdjustableCacheModelMixin,
NewsBlogCMSPlugin):
latest_articles = models.IntegerField(
default=5,
help_text=_('The maximum number of latest articles to display.')
Expand Down Expand Up @@ -447,7 +465,8 @@ def __str__(self):


@python_2_unicode_compatible
class NewsBlogRelatedPlugin(PluginEditModeMixin, CMSPlugin):
class NewsBlogRelatedPlugin(PluginEditModeMixin, AdjustableCacheModelMixin,
CMSPlugin):
# NOTE: This one does NOT subclass NewsBlogCMSPlugin. This is because this
# plugin can really only be placed on the article detail view in an apphook.
cmsplugin_ptr = models.OneToOneField(
Expand Down

0 comments on commit 831f614

Please sign in to comment.