Skip to content

Commit

Permalink
invalidate cache on model save
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Jan 31, 2020
1 parent 6fd73ae commit 9a1d65f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
------------------
* cleanups and refactoring
* faster queries
* invalidate cache on models save

0.14.0 (2020-01-28)
------------------
Expand Down
14 changes: 14 additions & 0 deletions admin_tools_stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from django.db.models import Q
from django.db.models.aggregates import Avg, Count, Max, Min, StdDev, Sum, Variance
from django.db.models.functions import Trunc
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.encoding import force_text
from django.utils.safestring import mark_safe
from django.utils.timezone import now
Expand Down Expand Up @@ -498,3 +500,15 @@ def __str__(self):
def get_active_graph(cls):
"""Returns active graphs"""
return DashboardStats.objects.filter(is_visible=1).prefetch_related('criteria')


@receiver(post_save, sender=DashboardStatsCriteria)
def clear_caches_criteria(sender, instance, **kwargs):
for dashboard_stats in instance.dashboardstats_set.all():
instance.get_dynamic_choices.invalidate(instance, dashboard_stats)


@receiver(post_save, sender=DashboardStats)
def clear_caches_stats(sender, instance, **kwargs):
for criteria in instance.criteria.all():
criteria.get_dynamic_choices.invalidate(criteria, instance)
9 changes: 1 addition & 8 deletions admin_tools_stats/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import datetime
from collections import OrderedDict

from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test.utils import override_settings
Expand Down Expand Up @@ -89,13 +88,7 @@ def test_admin_dashboard_page_post(self):
)


class ClearCacheMixin(object):
def tearDown(self):
super().tearDown()
cache.clear()


class ModelTests(ClearCacheMixin, TestCase):
class ModelTests(TestCase):
maxDiff = None

def setUp(self):
Expand Down

0 comments on commit 9a1d65f

Please sign in to comment.