diff --git a/opentech/apply/home/models.py b/opentech/apply/home/models.py index 31a7e55b6369d4ba37d6d18d2bf06dab8f9c920a..934f425163534ac91c5b991e085f1bd1117c6e5d 100644 --- a/opentech/apply/home/models.py +++ b/opentech/apply/home/models.py @@ -1,12 +1,16 @@ +from django.db import models +from django.utils.decorators import method_decorator + from wagtail.admin.edit_handlers import FieldPanel from wagtail.core.models import Page from wagtail.search import index -from django.db import models +from wagtailcache.cache import cache_page from opentech.apply.funds.models import ApplicationBase, LabBase +@method_decorator(cache_page, name='serve') class ApplyHomePage(Page): # Only allow creating HomePages at the root level parent_page_types = ['wagtailcore.Page'] diff --git a/opentech/public/utils/models.py b/opentech/public/utils/models.py index d9f398daec148a3041c267406d3b0dd65ab240f0..d75eeb19190f1e306c7f828bda304f92f9acfef9 100644 --- a/opentech/public/utils/models.py +++ b/opentech/public/utils/models.py @@ -1,5 +1,6 @@ from django.core.exceptions import ValidationError from django.db import models +from django.utils.decorators import method_decorator from wagtail.admin.edit_handlers import ( FieldPanel, @@ -16,6 +17,8 @@ from wagtail.images.edit_handlers import ImageChooserPanel from wagtail.snippets.models import register_snippet from wagtail.contrib.settings.models import BaseSetting, register_setting +from wagtailcache.cache import cache_page + class LinkFields(models.Model): """ @@ -242,6 +245,7 @@ class SystemMessagesSettings(BaseSetting): ] +@method_decorator(cache_page, name='serve') class BasePage(SocialFields, ListingFields, Page): show_in_menus_default = True diff --git a/opentech/public/utils/wagtail_hooks.py b/opentech/public/utils/wagtail_hooks.py index bb0193b91817c1a4119660092c96b944f339dc75..0efa847d6ceabae61810a0538d81d6e707e644f1 100644 --- a/opentech/public/utils/wagtail_hooks.py +++ b/opentech/public/utils/wagtail_hooks.py @@ -3,6 +3,8 @@ from django.contrib.staticfiles.templatetags.staticfiles import static from wagtail.contrib.modeladmin.options import ModelAdminGroup, ModelAdmin, modeladmin_register from wagtail.core import hooks +from wagtailcache.cache import clear_cache + from opentech.public.news.models import NewsType from opentech.public.people.models import PersonType @@ -31,3 +33,10 @@ def editor_css(): link = '<link rel="stylesheet" href="{}">\n' path = static('css/apply/wagtail_editor.css') return link.format(path) + + +@hooks.register('after_create_page') +@hooks.register('after_edit_page') +def clear_wagtailcache(request, page): + if page.live: + clear_cache() diff --git a/opentech/settings/base.py b/opentech/settings/base.py index 06bddd79aa2591f73a63772470bdbdac59683495..56bebca012ad09ced7ae938ebc4767699f6d8de0 100644 --- a/opentech/settings/base.py +++ b/opentech/settings/base.py @@ -118,6 +118,7 @@ INSTALLED_APPS = [ 'django_fsm', 'django_pwned_passwords', 'rest_framework', + 'wagtailcache', 'hijack', 'compat', @@ -202,6 +203,12 @@ if 'REDIS_URL' in env: "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": env['REDIS_URL'], + }, + "wagtailcache": { + "BACKEND": "wagtailcache.compat_backends.django_redis.RedisCache", + "LOCATION": env['REDIS_URL'], + 'KEY_PREFIX': 'wagtailcache', + 'TIMEOUT': 3600, } } else: @@ -209,9 +216,16 @@ else: 'default': { 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', 'LOCATION': 'database_cache', + }, + 'wagtailcache': { + 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', + 'LOCATION': 'database_cache', + 'KEY_PREFIX': 'wagtailcache', + 'TIMEOUT': 3600, } } +WAGTAIL_CACHE_BACKEND = 'wagtailcache' # Set s-max-age header that is used by reverse proxy/front end cache. See # urls.py diff --git a/opentech/settings/dev.py b/opentech/settings/dev.py index 9c0c27e985f031e1c1915b1680656ca3717b2723..c41bae6bedf56004c4e9439c0484e1332642fd6e 100644 --- a/opentech/settings/dev.py +++ b/opentech/settings/dev.py @@ -6,6 +6,8 @@ DEBUG = True # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'CHANGEME!!!' +WAGTAIL_CACHE = False + INTERNAL_IPS = ('127.0.0.1', '10.0.2.2') ALLOWED_HOSTS = ['apply.localhost', 'localhost', '127.0.0.1', 'dev.otf.is', 'dev-apply.otf.is'] diff --git a/opentech/settings/local.py.example b/opentech/settings/local.py.example index f9ae5091dbea8695bf7ddd5de87ad8c2ae7986e6..19e16b1ee9f267a73fee1456247a5afafccc27b0 100644 --- a/opentech/settings/local.py.example +++ b/opentech/settings/local.py.example @@ -22,3 +22,6 @@ CACHES = { # BASE_URL = 'http://otf.test' # SECRET_KEY = 'CHANGEME!!!' + +# Enable Wagtail Cache while developing +# WAGTAIL_CACHE = True \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index ec0a4b7e3ba68864947a9d5c7b4d22361c891182..192f6e09ab86362a00dc1f4f7af179e3984463c3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -44,4 +44,5 @@ Pillow==4.3.0 psycopg2==2.7.3.1 social_auth_app_django==3.1.0 wagtail~=2.2.0 +wagtail-cache==0.5.1 whitenoise==4.0