Пример #1
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
Пример #2
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