diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-03-01 12:47:20 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-03-01 12:47:20 +0100 |
commit | e98195faf34469b2195ffc3c271cc2a830981c73 (patch) | |
tree | d9785ac31d0e54e2864faa0bab52c04337c27cea | |
parent | d356385989e1a3116896ee9578cfba3290642e97 (diff) | |
download | Ishtar-e98195faf34469b2195ffc3c271cc2a830981c73.tar.bz2 Ishtar-e98195faf34469b2195ffc3c271cc2a830981c73.zip |
Force cache menu to be regerated on profile change (refs #3746)
-rw-r--r-- | ishtar_common/menu_base.py | 5 | ||||
-rw-r--r-- | ishtar_common/menus.py | 21 | ||||
-rw-r--r-- | ishtar_common/models.py | 2 |
3 files changed, 25 insertions, 3 deletions
diff --git a/ishtar_common/menu_base.py b/ishtar_common/menu_base.py index 13eb46f99..d18964c40 100644 --- a/ishtar_common/menu_base.py +++ b/ishtar_common/menu_base.py @@ -21,9 +21,12 @@ from ishtar_common.models import get_current_profile class SectionItem: - def __init__(self, idx, label, childs=[], profile_restriction=None, css=''): + def __init__(self, idx, label, childs=None, profile_restriction=None, + css=''): self.idx = idx self._label = label + if not childs: + childs = [] self.childs = childs self.available = False self.items = {} diff --git a/ishtar_common/menus.py b/ishtar_common/menus.py index 3741c6cac..e00c6fbef 100644 --- a/ishtar_common/menus.py +++ b/ishtar_common/menus.py @@ -21,7 +21,10 @@ Menus """ +import datetime + from django.conf import settings +from django.core.cache import cache from django.core.urlresolvers import reverse _extra_menus = [] @@ -69,9 +72,22 @@ class Menu: self.session = session self.items_by_idx = {} - def init(self): - if self.initialized: + def set_menu_updated_key(self): + cache_key = u"{}-{}".format( + settings.PROJECT_SLUG, 'menu_updated') + time = unicode(datetime.datetime.now().isoformat()) + cache.set(cache_key, time, settings.CACHE_TIMEOUT) + self.initialized = time + + def init(self, force=False): + cache_key = u"{}-{}".format( + settings.PROJECT_SLUG, 'menu_updated') + menu_updated = cache.get(cache_key) + if not force and menu_updated and self.initialized \ + and self.initialized == menu_updated: return + menu_updated = cache.get(cache_key) + self.set_menu_updated_key() self.items = {} self.items_by_idx = {} for idx, main_menu in enumerate(self.childs): @@ -156,5 +172,6 @@ class Menu: self.current_sections.append([section.label, section_url, bool(subsections)]) + menu = Menu(None) menu.init() diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 6f8a66f3b..b20392656 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1686,6 +1686,8 @@ def get_current_profile(force=False): def cached_site_changed(sender, **kwargs): get_current_profile(force=True) + from ishtar_common.menus import menu + menu.init(force=True) post_save.connect(cached_site_changed, sender=IshtarSiteProfile) |