def _load_template_source(self, template_name, template_dirs=None): # The logic should work like this: # * Try to find the template in the cache. If found, return it. # * Now check the cache if a lookup for the given template # has failed lately and hand over control to the next template # loader waiting in line. # * If this still did not fail we first try to find a site-specific # template in the database. # * On a failure from our last attempt we try to load the global # template from the database. # * If all of the above steps have failed we generate a new key # in the cache indicating that queries failed, with the current # timestamp. try: site = Site.objects.get_current() except ImproperlyConfigured: # todo: this probably needs its own try/catch request = get_request() site = Site.objects.get_current(request=request) cache_key = get_cache_key(template_name) if cache: try: backend_template = cache.get(cache_key) if backend_template: return backend_template, template_name except Exception: pass # Not found in cache, move on. cache_notfound_key = get_cache_notfound_key(template_name) if cache: try: notfound = cache.get(cache_notfound_key) if notfound: raise TemplateDoesNotExist(template_name) except Exception: raise TemplateDoesNotExist(template_name) # Not marked as not-found, move on... try: return self._load_and_store_template(template_name, cache_key, site, sites__in=[site.id]) except (Template.MultipleObjectsReturned, Template.DoesNotExist): try: return self._load_and_store_template(template_name, cache_key, site, sites__isnull=True) except (Template.MultipleObjectsReturned, Template.DoesNotExist): pass # Mark as not-found in cache. cache.set(cache_notfound_key, '1') raise TemplateDoesNotExist(template_name)
def load_template_source(self, template_name, template_dirs=None): # The logic should work like this: # * Try to find the template in the cache. If found, return it. # * Now check the cache if a lookup for the given template # has failed lately and hand over control to the next template # loader waiting in line. # * If this still did not fail we first try to find a site-specific # template in the database. # * On a failure from our last attempt we try to load the global # template from the database. # * If all of the above steps have failed we generate a new key # in the cache indicating that queries failed, with the current # timestamp. site = Site.objects.get_current() display_name = 'dbtemplates:%s:%s:%s' % (settings.DATABASE_ENGINE, template_name, site.domain) cache_key = get_cache_key(template_name) if cache: try: backend_template = cache.get(cache_key) if backend_template: return backend_template, template_name except: pass # Not found in cache, move on. cache_notfound_key = get_cache_notfound_key(template_name) if cache: try: notfound = cache.get(cache_notfound_key) if notfound: raise TemplateDoesNotExist(template_name) except: raise TemplateDoesNotExist(template_name) # Not marked as not-found, move on... try: template = Template.objects.get(name__exact=template_name, sites__in=[site.id]) return set_and_return(cache_key, template.content, display_name) except (Template.MultipleObjectsReturned, Template.DoesNotExist): try: template = Template.objects.get(name__exact=template_name) return set_and_return(cache_key, template.content, display_name) except Template.DoesNotExist: pass # Mark as not-found in cache. cache.set(cache_notfound_key, '1') raise TemplateDoesNotExist(template_name)
def load_template_source(self, template_name, template_dirs=None): site = Site.objects.get_current() display_name = 'dbtemplates:%s:%s:%s' % (settings.DATABASE_ENGINE, template_name, site.domain) cache_key = get_cache_key(template_name) if cache: try: backend_template = cache.get(cache_key) if backend_template: return backend_template, template_name except: pass try: template = Template.objects.get(name__exact=template_name) return set_and_return(cache_key, template.content, display_name) except (Template.MultipleObjectsReturned, Template.DoesNotExist): try: template = Template.objects.get( name__exact=template_name, sites__in=[site.id]) return set_and_return( cache_key, template.content, display_name) except Template.DoesNotExist: pass raise TemplateDoesNotExist(template_name)
def test_get_cache_name(self): self.assertEqual(get_cache_key('name with spaces'), 'dbtemplates::name-with-spaces::1')