示例#1
0
    def get_cached_urls(self):
        """
        Create the cached urls set.

        Merges the assets, the urls, removes the network urls and the external urls

        See :meth:`BaseAppCache.get_urls`, :meth:`get_network_urls`
        """
        if not self._cached:
            self._cached = self.get_urls()
            for appcache in self.registry:
                self._cached.update(appcache.get_assets(self.request))
            self._cached.update(self._external_appcaches['cached'])
            self._cached.difference_update(self.get_network_urls())
            if get_setting('DISCARD_EXTERNAL'):
                self._cached = filter(
                    lambda url: not is_external_url(url, self.request),
                    self._cached)
            if get_setting('EXCLUDE_URL'):
                new = set()
                for url in self._cached:
                    if not any(
                            url.startswith(excluded)
                            for excluded in get_setting('EXCLUDE_URL')):
                        new.add(url)
                self._cached = new
        return self._cached
    def get_network_urls(self):
        """
        Create the network urls set.

        ``*`` (wildcard entry) is added when :setting:`ADD_WILDCARD` is True (default)
        """
        if not self._network:
            for appcache in self.registry:
                self._network.update(appcache.get_network(self.request))
            self._network.update(self._external_appcaches['network'])
            self._network.update(get_setting('NETWORK_URL'))
        if get_setting("ADD_WILDCARD"):
            self._network.update(("*",))
        return self._network
示例#3
0
    def get_network_urls(self):
        """
        Create the network urls set.

        ``*`` (wildcard entry) is added when :setting:`ADD_WILDCARD` is True (default)
        """
        if not self._network:
            for appcache in self.registry:
                self._network.update(appcache.get_network(self.request))
            self._network.update(self._external_appcaches['network'])
            self._network.update(get_setting('NETWORK_URL'))
        if get_setting("ADD_WILDCARD"):
            self._network.update(("*", ))
        return self._network
示例#4
0
    def get(self, request, *args, **kwargs):
        empty_manifest = render_to_string(self.template_name,
                                          dictionary={
                                              'version': 0,
                                              'date': '-',
                                              'network_urls': ['*']
                                          })
        manifest = None
        if not get_setting("DISABLE"):
            appcache_registry.setup(request, self.template_name)
            if self.appcache_update:
                manifest = self._do_update(request, appcache_registry)
                if not manifest:
                    return HttpResponseForbidden(
                        _("Current user is not authorized for this action"))
            else:
                manifest = appcache_registry.get_manifest()
        if manifest and request.user.is_authenticated():
            manifest += "\n# version %s" % "auth"

        if (not manifest or self.force_empty_manifest
                or kwargs.get("parameter", False) == 'empty'):
            manifest = empty_manifest
        return HttpResponse(content=manifest,
                            content_type="text/cache-manifest")
示例#5
0
 def handle(self, *args, **options):
     self.language = "en"
     request = RequestFactory().get('/')
     request.LANGUAGE_CODE = self.language
     appcache_registry.setup(request, "html5_appcache/manifest")
     if get_setting("OVERRIDE_URLCONF"):
         return self.get_overridden_urls()
     else:
         return self.get_urls()
 def get_fallback_urls(self):
     """
     Creates the fallback urls set.
     """
     if not self._fallback:
         for appcache in self.registry:
             self._fallback.update(appcache.get_fallback(self.request))
         self._fallback.update(self._external_appcaches['fallback'])
         self._fallback.update(get_setting('FALLBACK_URL'))
     return self._fallback
 def render(self, context):
     from html5_appcache.settings import get_setting
     if get_setting("DISABLE"):
         return ""
     else:
         if self.parameter:
             return mark_safe('manifest="%s"' % reverse('appcache_manifest',
                                                        kwargs={'parameter': self.parameter}))
         else:
             return mark_safe('manifest="%s"' % reverse('appcache_manifest'))
示例#8
0
 def get_fallback_urls(self):
     """
     Creates the fallback urls set.
     """
     if not self._fallback:
         for appcache in self.registry:
             self._fallback.update(appcache.get_fallback(self.request))
         self._fallback.update(self._external_appcaches['fallback'])
         self._fallback.update(get_setting('FALLBACK_URL'))
     return self._fallback
 def get_urls(self):
     """
     Retrieves the urls from the sitemap and :meth:`BaseAppCache.get_urls` of
     the appcache instances
     """
     urls = set()
     if get_setting('USE_SITEMAP'):
         urls.update(self._get_sitemap())
     for appcache in self.registry:
         urls.update(appcache.get_urls(self.request))
     return urls
示例#10
0
 def render(self, context):
     from html5_appcache.settings import get_setting
     if get_setting("DISABLE"):
         return ""
     else:
         if self.parameter:
             return mark_safe('manifest="%s"' % reverse(
                 'appcache_manifest', kwargs={'parameter': self.parameter}))
         else:
             return mark_safe('manifest="%s"' %
                              reverse('appcache_manifest'))
示例#11
0
 def get_urls(self):
     """
     Retrieves the urls from the sitemap and :meth:`BaseAppCache.get_urls` of
     the appcache instances
     """
     urls = set()
     if get_setting('USE_SITEMAP'):
         urls.update(self._get_sitemap())
     for appcache in self.registry:
         urls.update(appcache.get_urls(self.request))
     return urls
示例#12
0
    def _get_sitemap(self):
        """
        Pretty ugly method that fetches the current sitemap and parses it to
        retrieve the list of available urls
        """
        from django.contrib.sites.models import get_current_site
        from django.test import Client
        from django.contrib.sitemaps.views import sitemap

        def walk_sitemap(urlset, doc):
            """
            Nested function for convenience. Recursively scans the sitemap to
            retrieve the urls
            """
            if isinstance(doc.tag, basestring):
                tag = etree.QName(doc.tag)
                if tag.localname == "loc":
                    urlset.append(doc.text)
            for node in doc:
                urlset = walk_sitemap(urlset, node)
            return urlset

        req_protocol = 'https' if self.request.is_secure() else 'http'
        req_site = get_current_site(self.request)
        client = Client()
        path = get_setting('SITEMAP_URL')
        if DJANGO_1_4 and not DJANGOCMS_2_3:
            path = "/%s%s" % (self.language, get_setting('SITEMAP_URL'))
        sitemap = client.get(path, follow=True, LANGUAGE_CODE=self.language)
        local_urls = []
        lxdoc = document_fromstring(sitemap.content)
        walk_sitemap(local_urls, lxdoc)
        if get_setting('DJANGOCMS_2_3'):
            lang_fix = "/" + self.language
        else:
            lang_fix = ""
        return map(
            lambda x: string.replace(x, "%s://%s" %
                                     (req_protocol, req_site), lang_fix),
            local_urls)
    def _get_sitemap(self):
        """
        Pretty ugly method that fetches the current sitemap and parses it to
        retrieve the list of available urls
        """
        from django.contrib.sites.models import get_current_site
        from django.test import Client
        from django.contrib.sitemaps.views import sitemap

        def walk_sitemap(urlset, doc):
            """
            Nested function for convenience. Recursively scans the sitemap to
            retrieve the urls
            """
            if isinstance(doc.tag, basestring):
                tag = etree.QName(doc.tag)
                if tag.localname == "loc":
                    urlset.append(doc.text)
            for node in doc:
                urlset = walk_sitemap(urlset, node)
            return urlset
        req_protocol = 'https' if self.request.is_secure() else 'http'
        req_site = get_current_site(self.request)
        client = Client()
        path = get_setting('SITEMAP_URL')
        if DJANGO_1_4 and not DJANGOCMS_2_3:
            path = "/%s%s" % (self.language, get_setting('SITEMAP_URL'))
        sitemap = client.get(path, follow=True, LANGUAGE_CODE=self.language)
        local_urls = []
        lxdoc = document_fromstring(sitemap.content)
        walk_sitemap(local_urls, lxdoc)
        if get_setting('DJANGOCMS_2_3'):
            lang_fix = "/" + self.language
        else:
            lang_fix = ""
        return map(lambda x: string.replace(
            x,"%s://%s" % (req_protocol, req_site), lang_fix),
                   local_urls)
    def get_cached_urls(self):
        """
        Create the cached urls set.

        Merges the assets, the urls, removes the network urls and the external urls

        See :meth:`BaseAppCache.get_urls`, :meth:`get_network_urls`
        """
        if not self._cached:
            self._cached = self.get_urls()
            for appcache in self.registry:
                self._cached.update(appcache.get_assets(self.request))
            self._cached.update(self._external_appcaches['cached'])
            self._cached.difference_update(self.get_network_urls())
            if get_setting('DISCARD_EXTERNAL'):
                self._cached = filter(lambda url: not is_external_url(
                    url, self.request), self._cached)
            if get_setting('EXCLUDE_URL'):
                new = set()
                for url in self._cached:
                    if not any(url.startswith(excluded) for excluded in get_setting('EXCLUDE_URL')):
                        new.add(url)
                self._cached = new
        return self._cached
示例#15
0
    def get(self, request, *args, **kwargs):
        empty_manifest = render_to_string(self.template_name, dictionary={
            'version': 0, 'date': '-', 'network_urls': ['*']
        })
        manifest = None
        if not get_setting("DISABLE"):
            appcache_registry.setup(request, self.template_name)
            if self.appcache_update:
                manifest = self._do_update(request, appcache_registry)
                if not manifest:
                    return HttpResponseForbidden(
                        _("Current user is not authorized for this action"))
            else:
                manifest = appcache_registry.get_manifest()
        if manifest and request.user.is_authenticated():
            manifest += "\n# version %s" % "auth"

        if (not manifest or self.force_empty_manifest or
                kwargs.get("parameter", False) == 'empty'):
            manifest = empty_manifest
        return HttpResponse(content=manifest, content_type="text/cache-manifest")
示例#16
0
def set_cached_value(key, value, version=1):
    from django.core.cache import cache
    return cache.set(get_cache_key(key), value, get_setting('CACHE_DURATION'),
                     version=version)
示例#17
0
def get_cache_version_key():
    return "%s:version" % get_setting('CACHE_KEY')
示例#18
0
def get_cache_key(key):
    return "%s:%s" % (
        get_setting('CACHE_KEY'), key)