def _tag_script_url(self, tags, caller):
     hsh = None
     if len(tags) > 40:
         full_path = tags
         tags = hashlib.sha1(tags).hexdigest()
         hsh, new = HashForTags.query().get_or_create(hash=tags, defaults={"full_path": full_path})
     return reverse("script_tags_inc", kwargs={"tags": tags, "inc": hsh["last_inc"] or 0 if hsh else 0})
Example #2
0
    def style_tags_view(self, request, tags, inc=None):
        if ':' not in tags: # Loads hash from cache
            try:
                tags = HashForTags.query().get(hash=tags)['full_path']
            except HashForTags.DoesNotExist:
                pass

        last_modified = 0
        content = []
        for tag in tags.split(','):
            tag = tag[:-4] if tag.endswith('.css') else tag
            path = self.styles.get(tag, None)
            if not path:
                for k,v in self.styles.items():
                    if k.endswith(':'+tag):
                        path = v

            if path:
                fp = file(path)
                file_content = fp.read()
                fp.close()

                file_content = render_content(unicode(file_content, errors='ignore'), request=request)
                content.append(file_content)
                last_modified = max([last_modified, os.path.getmtime(path)])

        content = self.minify_css(u'\n'.join(content))
        resp = HttpResponse(content, mime_type='text/css')
        if last_modified:
            resp['Last-Modified'] = http_date(last_modified)
        return resp
 def _tag_script_tags(self, tags, caller):
     hsh = None
     if len(tags) > 40:
         full_path = tags
         tags = hashlib.sha1(tags).hexdigest()
         hsh, new = HashForTags.query().get_or_create(hash=tags, defaults={"full_path": full_path})
     url = reverse("script_tags_inc", kwargs={"tags": tags, "inc": hsh["last_inc"] or 0 if hsh else 0})
     return '<script src="%s" rel="%s" charset="utf-8"></script>' % (url, tags)
 def _tag_style_tags(self, tags, caller):
     hsh = None
     if len(tags) > 40:
         full_path = tags
         tags = hashlib.sha1(tags).hexdigest()
         hsh, new = HashForTags.query().get_or_create(hash=tags, defaults={"full_path": full_path})
     url = reverse("style_tags_inc", kwargs={"tags": tags, "inc": hsh["last_inc"] or 0 if hsh else 0})
     return '<link href="%s" rel="stylesheet"/>' % url
Example #5
0
    def script_tags_view(self, request, tags, inc=None):
        """
        This view can be called like this:

        /ajax/scripts/ajax:base,ajax:history,etc:etc/

        or like this:

        /ajax/scripts/6b6962d5b178232a8b8ebbad4ec9781e546221ff/

        The first situation is more elegant and explicit, but if you have a really long URL, it breaks the
        HTML rendering by the browser, so, we need a hash to represent it in cache.
        """
        if ':' not in tags: # Loads hash from cache
            try:
                tags = HashForTags.query().get(hash=tags)['full_path']
            except HashForTags.DoesNotExist:
                pass

        last_modified = 0
        content = []
        for tag in tags.split(','):
            tag = tag[:-3] if tag.endswith('.js') else tag
            path = self.scripts.get(tag, None)
            if not path:
                for k,v in self.scripts.items():
                    if k.endswith(':'+tag):
                        path = v

            if path:
                fp = file(path)
                file_content = fp.read()
                fp.close()

                file_content = render_content(unicode(file_content, errors='ignore'), request=request)
                content.append(file_content)
                last_modified = max([last_modified, os.path.getmtime(path)])

        content = self.minify_js(u'\n'.join(c for c in content))
        last_modified = http_date(last_modified)

        resp = HttpResponse(content, mime_type='text/javascript')
        if last_modified:
            resp['Last-Modified'] = last_modified
        return resp