Skip to content

Commit

Permalink
Added forced cache clearing when T&C or UT&C are updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyface committed Oct 28, 2017
1 parent b538f38 commit 2199f47
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="django-termsandconditions",
version="1.2.1",
version="1.2.2",
url='https://github.com/cyface/django-termsandconditions',
license='BSD',
description="django-termsandconditions is a Django app that enables users to accept terms and conditions of a site.",
Expand Down
1 change: 1 addition & 0 deletions termsandconditions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from __future__ import unicode_literals
from future.standard_library import install_aliases
install_aliases()
default_app_config = 'termsandconditions.apps.TermsAndConditionsConfig'
8 changes: 8 additions & 0 deletions termsandconditions/apps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
"""Django Apps Config"""

# pylint: disable=C1001,E0202,W0613

from django.apps import AppConfig
import logging

LOGGER = logging.getLogger(name='termsandconditions')


class TermsAndConditionsConfig(AppConfig):
"""App config for TermsandConditions"""
name = 'termsandconditions'
verbose_name = "Terms and Conditions"

def ready(self):
import termsandconditions.signals
5 changes: 3 additions & 2 deletions termsandconditions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,17 @@ def get_active_terms_list():
def get_active_terms_not_agreed_to(user):
"""Checks to see if a specified user has agreed to all the latest terms and conditions"""

not_agreed_terms = cache.get('tandc_not_agreed_terms')
not_agreed_terms = cache.get('tandc.not_agreed_terms_' + user.username)
if not_agreed_terms is None:
try:
LOGGER.debug("Not Agreed Terms")
not_agreed_terms = TermsAndConditions.get_active_terms_list().exclude(
userterms__in=UserTermsAndConditions.objects.filter(user=user)
).order_by('slug')

cache.set('tandc_not_agreed_terms', not_agreed_terms, TERMS_CACHE_SECONDS)
cache.set('tandc.not_agreed_terms_' + user.username, not_agreed_terms, TERMS_CACHE_SECONDS)
except (TypeError, UserTermsAndConditions.DoesNotExist):
return None

return not_agreed_terms

29 changes: 29 additions & 0 deletions termsandconditions/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
""" Signals for Django """

# pylint: disable=C1001,E0202,W0613

import logging
from django.core.cache import cache
from django.dispatch import receiver
from .models import TermsAndConditions, UserTermsAndConditions
from django.db.models.signals import post_delete, post_save

LOGGER = logging.getLogger(name='termsandconditions')


@receiver(post_save, sender=UserTermsAndConditions)
@receiver(post_delete, sender=UserTermsAndConditions)
def user_terms_updated(sender, **kwargs):
"""Called when user terms and conditions is changed - to force cache clearing"""
LOGGER.debug("User T&C Updated Signal Handler")
if kwargs.get('instance').user:
cache.delete('tandc.not_agreed_terms_' + kwargs.get('instance').user.username)


@receiver(post_save, sender=TermsAndConditions)
@receiver(post_delete, sender=TermsAndConditions)
def terms_updated(sender, **kwargs):
"""Called when terms and conditions is changed - to force cache clearing"""
LOGGER.debug("T&C Updated Signal Handler")
cache.delete('tandc.active_terms_ids')
cache.delete('tandc.active_terms_list')
3 changes: 2 additions & 1 deletion termsandconditions_demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
# Cache Settings
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
#'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
}
CACHE_MIDDLEWARE_SECONDS = 30
Expand Down

0 comments on commit 2199f47

Please sign in to comment.