Esempio n. 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
Esempio n. 2
0
    def process_response(self, request, response):
        if isinstance(response.content, HttpResponse):
            response = response.content

        response['Date'] = http_date()

        if response.has_header('ETag'):
            if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
            if if_none_match == response['ETag']:
                # Setting the status is enough here. The response handling path
                # automatically removes content for this status code (in
                # http.conditional_content_removal()).
                response.status_code = 304

        if response.has_header('Last-Modified'):
            if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE')
            if if_modified_since is not None:
                if_modified_since = parse_http_date_safe(if_modified_since)
            if if_modified_since is not None:
                last_modified = parse_http_date_safe(response['Last-Modified'])
                if last_modified is not None and last_modified <= if_modified_since:
                    # Setting the status code is enough here (same reasons as above).
                    response.status_code = 304

        return response
Esempio n. 3
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