Beispiel #1
0
 def process_response(self, request, response):
     """Sets the cache, if needed."""
     if not hasattr(request, '_cache_update_cache') or not request._cache_update_cache:
         # We don't need to update the cache, just return.
         return response
     if request.method != 'GET':
         # This is a stronger requirement than above. It is needed
         # because of interactions between this middleware and the
         # HTTPMiddleware, which throws the body of a HEAD-request
         # away before this middleware gets a chance to cache it.
         return response
     if not response.status_code == 200:
         return response
     # Try to get the timeout from the "max-age" section of the "Cache-
     # Control" header before reverting to using the default cache_timeout
     # length.
     timeout = get_max_age(response)
     if timeout == None:
         timeout = self.cache_timeout
     elif timeout == 0:
         # max-age was set to 0, don't bother caching.
         return response
     
     if self.patch_headers:
         patch_response_headers(response, timeout)
     
     if timeout:            
         if callable(self.key_prefix):
             key_prefix = self.key_prefix(request)
         else:
             key_prefix = self.key_prefix
         
         cache_key = learn_cache_key(request, response, timeout, key_prefix)
         cache.set(cache_key, response, timeout)
     return response
    def process_response(self, request, response):
        """Sets the cache, if needed."""
        if not self._should_update_cache(request, response):
            # We don't need to update the cache, just return.
            return response

        if response.streaming or response.status_code != 200:
            return response

        # Don't cache responses that set a user-specific (and maybe security
        # sensitive) cookie in response to a cookie-less request.
        if not request.COOKIES and response.cookies and has_vary_header(response, 'Cookie'):
            return response

        # Try to get the timeout from the "max-age" section of the "Cache-
        # Control" header before reverting to using the default cache_timeout
        # length.
        timeout = get_max_age(response)
        if timeout is None:
            timeout = self.cache_timeout
        elif timeout == 0:
            # max-age was set to 0, don't bother caching.
            return response
        patch_response_headers(response, timeout)
        if timeout:
            cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache)
            if hasattr(response, 'render') and callable(response.render):
                response.add_post_render_callback(
                    lambda r: self.cache.set(cache_key, r, timeout)
                )
            else:
                self.cache.set(cache_key, response, timeout)
        return response
Beispiel #3
0
 def process_response(self, request, response):
     """Sets the cache, if needed."""
     if not self._should_update_cache(request, response):
         # We don't need to update the cache, just return.
         return response
     if not response.status_code == 200:
         return response
     # Try to get the timeout from the "max-age" section of the "Cache-
     # Control" header before reverting to using the default cache_timeout
     # length.
     timeout = get_max_age(response)
     if timeout == None:
         timeout = self.cache_timeout
     elif timeout == 0:
         # max-age was set to 0, don't bother caching.
         return response
     patch_response_headers(response, timeout)
     if timeout:
         cache_key = learn_cache_key(request, response, timeout, self.key_prefix, cache=self.cache)
         if hasattr(response, 'render') and callable(response.render):
             response.add_post_render_callback(
                 lambda r: self.cache.set(cache_key, r, timeout)
             )
         else:
             self.cache.set(cache_key, response, timeout)
     return response
Beispiel #4
0
    def dispatch(self, request, *args, **kwargs):
        cache_key = self.get_page_cache_key(request, request.method)

        cache = get_cache(self.cache_alias)

        # Try to fetch response from cache.
        response = cache.get(cache_key, None)

        if response is None:
            # Cache not set, render response.
            response = super(CachePageMixin, self).dispatch(
                request, *args, **kwargs)

            # Apply cache headers.
            patch_response_headers(response, self.cache_timeout)

            # Override max-age if needed.
            if self.cache_ensure_never_cache:
                add_never_cache_headers(response)

            # If cache timeout is set, cache response.
            if self.cache_timeout is not None:
                # Check if we have a TemplateResponse with render method.
                if hasattr(response, 'render') and callable(response.render):
                    response.add_post_render_callback(
                        lambda r: cache.set(cache_key, r, self.cache_timeout)
                    )
                else:
                    cache.set(cache_key, response, self.cache_timeout)

        # Return response
        return response
Beispiel #5
0
    def process_response(self, request, response):
        """Sets the cache, if needed."""
        #if not self._should_update_cache(request, response):
        #    # We don't need to update the cache, just return.
        #    return response

        if response.streaming or response.status_code != 200:
            return response
        
        # Don't cache responses that set a user-specific (and maybe security
        # sensitive) cookie in response to a cookie-less request.
        if not request.COOKIES and response.cookies and has_vary_header(response, 'Cookie'):
            return response

        # Try to get the timeout from the "max-age" section of the "Cache-
        # Control" header before reverting to using the default cache_timeout
        # length.
        timeout = get_max_age(response)
        if timeout == None:
            timeout = self.cache_timeout
        elif timeout == 0:
            # max-age was set to 0, don't bother caching.
            return response
        patch_response_headers(response, timeout)
        if timeout:
            cache_key = "%s-%s" % (self.key_prefix, request.get_full_path())
            #raise ValueError(cache_key)
            if hasattr(response, 'render') and isinstance(response.render, collections.Callable):
                response.add_post_render_callback(
                    lambda r: cache._cache.set(cache_key.encode("utf-8"), zlib.compress(r.content, 9), timeout)
                )
            else:
                # we use the highest compression level, because since it is cached we hope for it to pay off
                cache._cache.set(cache_key.encode("utf-8"), zlib.compress(response.content, 9), timeout)
        return response
def serve(request, path, document_root=None, show_indexes=False, cache_timeout=60*60*24*14, digest=None):
    """
    Serve static files below a given point in the directory structure.

    To use, put a URL pattern such as::

        (r'^(?P<path>.*)$', 'supstream.views.serve', {'document_root' : '/path/to/my/files/'})

    in your URLconf. You must provide the ``document_root`` param. You may
    also set ``show_indexes`` to ``True`` if you'd like to serve a basic index
    of the directory.  This index view will use the template hardcoded below,
    but if you'd like to override it, you can create a template called
    ``static/directory_index.html``.
    """
    path = posixpath.normpath(urllib.unquote(path))
    path = path.lstrip('/')
    newpath = ''
    for part in path.split('/'):
        if not part:
            # Strip empty path components.
            continue
        drive, part = os.path.splitdrive(part)
        head, part = os.path.split(part)
        if part in (os.curdir, os.pardir):
            # Strip '.' and '..' in path.
            continue
        newpath = os.path.join(newpath, part).replace('\\', '/')
    if newpath and path != newpath:
        return HttpResponseRedirect(newpath)
    fullpath = os.path.join(document_root, newpath)
    if os.path.isdir(fullpath):
        if show_indexes:
            return directory_index(newpath, fullpath)
        raise Http404("Directory indexes are not allowed here.")
    if not os.path.exists(fullpath):
        raise Http404('"%s" does not exist' % fullpath)
    # Respect the If-Modified-Since header.
    statobj = os.stat(fullpath)
    mimetype, encoding = mimetypes.guess_type(fullpath)
    mimetype = mimetype or 'application/octet-stream'
    if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
                              statobj.st_mtime, statobj.st_size):
        return HttpResponseNotModified(mimetype=mimetype)
    digest, contents = file_digest(fullpath, return_contents=True)
    etag = '"%s"' % (digest,)
    if request.META.get('HTTP_IF_NONE_MATCH', '') == etag:
        return HttpResponseNotModified(mimetype=mimetype)
    response = HttpResponse(contents, mimetype=mimetype)
    response["Last-Modified"] = http_date(statobj.st_mtime)
    response["Content-Length"] = statobj.st_size
    response["ETag"] = etag
    patch_response_headers(response, cache_timeout = cache_timeout)
    if encoding:
        response["Content-Encoding"] = encoding
    for pattern, mangler in settings.SUPSTREAM_HEADER_MANGLERS.iteritems():
        if pattern.match(request.get_full_path()):
            for header_name, header_value in mangler(request, response):
                response[header_name] = header_value
    return response
def index(request):
    if request.user.is_authenticated():
        response = redirect(reverse('todo:index'))
    else:
        response = render(request, 'index.html')
    # Prevent caching so logins go to the correct page.
    patch_response_headers(response, cache_timeout=0)
    return response
Beispiel #8
0
def ical_single_event (request, event_id):
    e = get_object_or_404(Event, id=event_id)
    event_list = [e]
    cal = ical_service.create_calendar_with_metadata(event_list, request)
    response = create_httpresponse(cal.as_string())
    if e.date_time_begin < timezone.now():
        patch_response_headers(response, cache_timeout=ONE_YEAR)
    return response
    def process_response(self, request, response):
        if request.method in ['GET', 'HEAD']:
            # dump current Cache-Control headers
            del response['Cache-Control']
            # inject headers
            patch_response_headers(response)

        return response
Beispiel #10
0
def channel(request):
    provider = providers.registry.by_id(FacebookProvider.id)
    locale = provider.get_locale_for_request(request)
    response = render(request, 'facebook/channel.html',
                      {'facebook_jssdk_locale': locale})
    cache_expire = 60 * 60 * 24 * 365
    patch_response_headers(response, cache_expire)
    response['Pragma'] = 'Public'
    return response
Beispiel #11
0
    def render_to_response(self, context, **kwargs):
        response = HttpResponse(content_type="image/png")

        image = context['game'].identicon(56)
        image.save(response, 'PNG')

        patch_response_headers(response, cache_timeout=31536000)

        return response
Beispiel #12
0
def stub_attribution_code(request):
    """Return a JSON response containing the HMAC signed stub attribution value"""
    if not request.is_ajax():
        return HttpResponseJSON({'error': 'Resource only available via XHR'}, status=400)

    response = None
    rate = settings.STUB_ATTRIBUTION_RATE
    key = settings.STUB_ATTRIBUTION_HMAC_KEY
    if not rate:
        # return as though it was rate limited, since it was
        response = HttpResponseJSON({'error': 'rate limited'}, status=429)
    elif not key:
        response = HttpResponseJSON({'error': 'service not configured'}, status=403)

    if response:
        patch_response_headers(response, 300)  # 5 min
        return response

    data = request.GET
    codes = OrderedDict()
    has_value = False
    for name, default_value in STUB_VALUE_NAMES:
        val = data.get(name, '')
        # remove utm_
        name = name[4:]
        if val and STUB_VALUE_RE.match(val):
            codes[name] = val
            has_value = True
        else:
            codes[name] = default_value

    if codes['source'] == '(not set)' and 'referrer' in data:
        try:
            domain = urlparse(data['referrer']).netloc
            if domain and STUB_VALUE_RE.match(domain):
                codes['source'] = domain
                codes['medium'] = 'referral'
                has_value = True
        except Exception:
            # any problems and we should just ignore it
            pass

    if not has_value:
        codes['source'] = 'www.mozilla.org'
        codes['medium'] = '(none)'

    codes['timestamp'] = str(int(time()))
    code = '&'.join('='.join(attr) for attr in codes.items())
    code = querystringsafe_base64.encode(code)
    sig = hmac.new(key, code, hashlib.sha256).hexdigest()
    response = HttpResponseJSON({
        'attribution_code': code,
        'attribution_sig': sig,
    })
    patch_response_headers(response, 300)  # 5 min
    return response
 def dispatch(self, *args, **kwargs):
     timeout = self.get_cache_timeout()
     if timeout is None:
         return super(CacheMixin, self).dispatch(*args, **kwargs)
     if isinstance(timeout, (list, tuple)):
         timeout = randint(*timeout)
     response = cache_page(timeout, cache=self.get_cache(), key_prefix=self.get_key_prefix()
                           )(super(CacheMixin, self).dispatch)(*args, **kwargs)
     patch_response_headers(response, timeout)
     return response
    def process_response(self, request, response):
        if request.method in ['GET', 'HEAD']:
            path = request.path
            max_age = cache_lookup.get_cache_time(path)

            # dump current Cache-Control headers
            del response['Cache-Control']
            # inject headers
            patch_response_headers(response, cache_timeout=max_age)

        return response
    def render_to_response(self, context, **response_kwargs):
        # Get response from parent TemplateView class
        response = super().render_to_response(
            context, **response_kwargs
        )

        # Add Cache-Control and Expires headers
        patch_response_headers(response, cache_timeout=60 * 30)

        # Return response
        return response
Beispiel #16
0
    def process_response(self, request, response):
        if 'cache-control' in response or response.status_code >= 400:
            return response

        if (request.method in ('GET', 'HEAD')
                and settings.CACHE_MIDDLEWARE_SECONDS):
            # uses CACHE_MIDDLEWARE_SECONDS by default
            patch_response_headers(response)
        else:
            add_never_cache_headers(response)

        return response
Beispiel #17
0
def datepublisher_response_processor(page, request, response):
    """
    This response processor is automatically added when the datepublisher
    extension is registered. It sets the response headers to match with
    the publication end date of the page so that upstream caches and
    the django caching middleware know when to expunge the copy.
    """
    expires = page.publication_end_date
    if expires is not None:
        now = datetime.now()
        delta = int((expires - now).total_seconds())
        patch_response_headers(response, delta)
Beispiel #18
0
    def process_response(self, request, response):
        if 'cache-control' in response or response.status_code >= 400:
            return response

        if (request.method in ('GET', 'HEAD') and
                settings.CACHE_MIDDLEWARE_SECONDS):
            # uses CACHE_MIDDLEWARE_SECONDS by default
            patch_response_headers(response)
        else:
            add_never_cache_headers(response)

        return response
Beispiel #19
0
    def render_to_response(self, context, **response_kwargs):
        """
        Add caching headers to the standard render_to_response from the parent
        """

        response = super(CmsTemplateFinder, self).render_to_response(
            context, **response_kwargs
        )

        patch_response_headers(response, cache_timeout=300)

        return response
Beispiel #20
0
def stub_attribution_code(request):
    """Return a JSON response containing the HMAC signed stub attribution value"""
    if not request.is_ajax():
        return HttpResponseJSON({'error': 'Resource only available via XHR'}, status=400)

    response = None
    if not settings.STUB_ATTRIBUTION_RATE:
        # return as though it was rate limited, since it was
        response = HttpResponseJSON({'error': 'rate limited'}, status=429)
    elif not settings.STUB_ATTRIBUTION_HMAC_KEY:
        response = HttpResponseJSON({'error': 'service not configured'}, status=403)

    if response:
        patch_response_headers(response, 300)  # 5 min
        return response

    data = request.GET
    codes = OrderedDict()
    has_value = False
    for name, default_value in STUB_VALUE_NAMES:
        val = data.get(name, '')
        # remove utm_
        name = name[4:]
        if val and STUB_VALUE_RE.match(val):
            codes[name] = val
            has_value = True
        else:
            codes[name] = default_value

    if codes['source'] == '(not set)' and 'referrer' in data:
        try:
            domain = urlparse(data['referrer']).netloc
            if domain and STUB_VALUE_RE.match(domain):
                codes['source'] = domain
                codes['medium'] = 'referral'
                has_value = True
        except Exception:
            # any problems and we should just ignore it
            pass

    if not has_value:
        codes['source'] = 'www.mozilla.org'
        codes['medium'] = '(none)'

    code_data = sign_attribution_codes(codes)
    if code_data:
        response = HttpResponseJSON(code_data)
    else:
        response = HttpResponseJSON({'error': 'Invalid code'}, status=400)

    patch_response_headers(response, 300)  # 5 min
    return response
Beispiel #21
0
def stub_attribution_code(request):
    """Return a JSON response containing the HMAC signed stub attribution value"""
    if not request.is_ajax():
        return HttpResponseJSON({'error': 'Resource only available via XHR'}, status=400)

    response = None
    if not settings.STUB_ATTRIBUTION_RATE:
        # return as though it was rate limited, since it was
        response = HttpResponseJSON({'error': 'rate limited'}, status=429)
    elif not settings.STUB_ATTRIBUTION_HMAC_KEY:
        response = HttpResponseJSON({'error': 'service not configured'}, status=403)

    if response:
        patch_response_headers(response, 300)  # 5 min
        return response

    data = request.GET
    codes = OrderedDict()
    has_value = False
    for name, default_value in STUB_VALUE_NAMES:
        val = data.get(name, '')
        # remove utm_
        name = name[4:]
        if val and STUB_VALUE_RE.match(val):
            codes[name] = val
            has_value = True
        else:
            codes[name] = default_value

    if codes['source'] == '(not set)' and 'referrer' in data:
        try:
            domain = urlparse(data['referrer']).netloc
            if domain and STUB_VALUE_RE.match(domain):
                codes['source'] = domain
                codes['medium'] = 'referral'
                has_value = True
        except Exception:
            # any problems and we should just ignore it
            pass

    if not has_value:
        codes['source'] = 'www.mozilla.org'
        codes['medium'] = '(none)'

    code_data = sign_attribution_codes(codes)
    if code_data:
        response = HttpResponseJSON(code_data)
    else:
        response = HttpResponseJSON({'error': 'Invalid code'}, status=400)

    patch_response_headers(response, 300)  # 5 min
    return response
Beispiel #22
0
def tile(
    request: HttpRequest,
    workflow_id_or_secret_id: Union[int, str],
    step_slug: str,
    delta_id: int,
    tile_row: int,
    tile_column: int,
):
    workflow, step = _load_workflow_and_step_sync(request,
                                                  workflow_id_or_secret_id,
                                                  step_slug, "table")
    # No need for cooperative lock: the cache may always be corrupt (lock or no), and
    # we don't read from the database
    row_begin = tile_row * settings.BIG_TABLE_ROWS_PER_TILE
    row_end = row_begin + settings.BIG_TABLE_ROWS_PER_TILE  # one past end
    column_begin = tile_column * settings.BIG_TABLE_COLUMNS_PER_TILE
    column_end = column_begin + settings.BIG_TABLE_COLUMNS_PER_TILE  # one past end

    cached_result = step.cached_render_result
    if cached_result is None or cached_result.delta_id != delta_id:
        return JsonResponse({"error": "delta_id result not cached"},
                            status=status.NOT_FOUND)

    if (cached_result.table_metadata.n_rows <= row_begin
            or len(cached_result.table_metadata.columns) <= column_begin):
        return JsonResponse({"error": "tile out of bounds"},
                            status=status.NOT_FOUND)

    try:
        record_json = read_cached_render_result_slice_as_text(
            cached_result,
            "json",
            only_rows=range(row_begin, row_end),
            only_columns=range(column_begin, column_end),
        )
    except CorruptCacheError:
        # A different 404 message to help during debugging
        return JsonResponse(
            {
                "error":
                "result went away; please try again with another delta_id"
            },
            status=status.NOT_FOUND,
        )

    # Convert from [{"a": "b", "c": "d"}, ...] to [["b", "d"], ...]
    rows = json.loads(record_json,
                      object_pairs_hook=lambda pairs: [v for k, v in pairs])

    response = JsonResponse({"rows": rows})
    patch_response_headers(response, cache_timeout=600)
    return response
Beispiel #23
0
def home(request):

    if request.method == 'POST':

        form = ContactForm(request.POST or None)

        if form.is_valid():

            print(form.cleaned_data)
            name = form.cleaned_data.get('nome')
            email = form.cleaned_data.get('email')
            subject = form.cleaned_data.get('assunto')
            message = form.cleaned_data.get('mensagem')
            from_email = '*****@*****.**'  # "%s <%s>" % (name, email) # RFC 5322
            to_email = [
                '*****@*****.**',
            ]
            reply_to = [
                email,
            ]

            if settings.EMAIL_DESTINY:
                to_email += [settings.EMAIL_DESTINY]

            contact_message = "%s <%s> \n\n %s" % (name, email, message)

            msg = EmailMessage(
                subject=subject,
                body=contact_message,
                to=to_email,
                from_email=from_email,
                reply_to=reply_to,
            )

            msg.send(fail_silently=False)

            return redirect(reverse('core:home') + '?sent=True')

    form = ContactForm()

    cs_modals = CreditServiceModal.objects.all()

    template = 'home/home.html'
    context = {'form': form, 'cs_modals': cs_modals, 'home': 'home'}

    msg_sent = request.GET.get('sent', False)
    if msg_sent:
        context['message_sent'] = 'message_sent'

    response = render(request, template, context)
    patch_response_headers(response, cache_timeout=7884000)
    return response
Beispiel #24
0
def process_response(req, res, prefix, cache_time=60 * 60):
    # update the cache using the django's CacheMiddleware
    cache_middleware = CacheMiddleware(cache_timeout=cache_time, key_prefix=prefix)
    response = cache_middleware.process_response(req, res)
    # update some header to prevent wrong client caching
    max_age = get_max_age(response)
    if max_age and max_age < max_age:
        # Remove headers so patch_response works
        for header in ("ETag", "Last-Modified", "Expires"):
            if response.has_header(header):
                del response[header]
        patch_response_headers(response, max_age)
    return response
Beispiel #25
0
def place_popup(request, place_id):
    try:
        place_id = int(place_id)
        place = Place.objects.get(id=place_id)
    except:
        return HttpResponse(status=404)

    template_list = ["richmaps/place_popup_%s.html" % place.place_type.slug, "richmaps/place_popup.html"]
    current_template = select_template(template_list)
    html = current_template.render(template.Context({"place": place, "place_type": place.place_type}))
    response = HttpResponse(html)
    patch_response_headers(response, cache_timeout=3600)
    return response
Beispiel #26
0
def user_avatar(request, user, size):
    """User avatar view."""
    user = get_object_or_404(User, username=user)

    if user.email == '*****@*****.**':
        return redirect(get_fallback_avatar_url(size))

    response = HttpResponse(content_type='image/png',
                            content=get_avatar_image(user, size))

    patch_response_headers(response, 3600 * 24 * 7)

    return response
Beispiel #27
0
def datepublisher_response_processor(page, request, response):
    """
    This response processor is automatically added when the datepublisher
    extension is registered. It sets the response headers to match with
    the publication end date of the page so that upstream caches and
    the django caching middleware know when to expunge the copy.
    """
    expires = page.publication_end_date
    if expires is not None:
        now = datetime.now()
        delta = expires - now
        delta = int(delta.days * 86400 + delta.seconds)
        patch_response_headers(response, delta)
Beispiel #28
0
def testmodule(request):
    valid, response = request_init(request)
    if not valid:
        return response

    data = {
            'request': request,
            'built': datetime.now().strftime("%H:%M:%S"),
            }

    response = render_to_response('testModule.html', data, content_type='text/html')
    patch_response_headers(response, cache_timeout=request.session['max_age_minutes'] * 60)
    return response
Beispiel #29
0
    def process_response(self, request, response):
        if (request.method in self.allowed_methods and
                response.status_code in self.allowed_statuses and
                request.REQUEST.get('cache') == '1'):
            timeout = get_max_age(response)
            if timeout is None:
                timeout = settings.CACHE_MIDDLEWARE_SECONDS or 0
            if timeout != 0:
                # Only if max-age is 0 should we bother with caching.
                patch_response_headers(response, timeout)
                patch_cache_control(response, must_revalidate=True)

        return response
Beispiel #30
0
    def process_response(self, request, response):
        if (request.method in self.allowed_methods and
                response.status_code in self.allowed_statuses and
                request.REQUEST.get('cache') == '1'):
            timeout = get_max_age(response)
            if timeout is None:
                timeout = settings.CACHE_MIDDLEWARE_SECONDS or 0
            if timeout != 0:
                # Only if max-age is 0 should we bother with caching.
                patch_response_headers(response, timeout)
                patch_cache_control(response, must_revalidate=True)

        return response
Beispiel #31
0
    def process_response(self, request, response):
        """Sets the cache, if needed."""

        # TODO
        if not self._should_update_cache(request, response):
            # We don't need to update the cache, just return.
            return response

        # 不是流响应,不是200码
        if response.streaming or response.status_code != 200:
            return response

        # Don't cache responses that set a user-specific (and maybe security
        # sensitive) cookie in response to a cookie-less request.
        # 请求没有cookie,但是处理过程中设置了cookie,并且Vary包含了Cookie,直接返回
        # 分析:因为缓存服务器收到请求是没有Cookie的,然后返回的告诉他类似的请求根据Cookie
        # 来判断是否使用缓存,这样,下一个没有Cookie的人访问该URL则会拿到之前的人
        # 的Cookie中的数据
        if not request.COOKIES and response.cookies and has_vary_header(
                response, 'Cookie'):
            return response

        # Try to get the timeout from the "max-age" section of the "Cache-
        # Control" header before reverting to using the default cache_timeout
        # length.
        # 首先使用response中设置的max_age(根据Cache-Control部分),如果没有设置
        # 则使用setting中的.
        # 分析:setting中的是最后的依据,如果设置过则按照设置过的来
        timeout = get_max_age(response)
        if timeout is None:
            timeout = self.cache_timeout
        elif timeout == 0:
            # max-age was set to 0, don't bother caching.
            return response
        # 设置ETag, Last-Modified, Expires 和 Cache-Control的max-age部分,都用于缓存控制
        patch_response_headers(response, timeout)

        if timeout:
            # 拿到cache——key,该方法设置了header_key,返回的是page_key
            cache_key = learn_cache_key(request,
                                        response,
                                        timeout,
                                        self.key_prefix,
                                        cache=self.cache)
            # 如果有render,则设置回调,在render之后缓存.
            if hasattr(response, 'render') and callable(response.render):
                response.add_post_render_callback(
                    lambda r: self.cache.set(cache_key, r, timeout))
            else:
                self.cache.set(cache_key, response, timeout)
        return response
    def process_response(self, request, response):
        if COUNT_UPDATE_CACHE:
            self._count_response(request)

        if getattr(response, "_from_cache", False):
            if COUNT_UPDATE_CACHE:
                self._count_hit()
            logger.debug("response comes from the cache, no need to update the cache")
            return response
        else:
            # used e.g. in unittests
            response._from_cache = False

        if not self.use_cache(request, response):
            if EXTRA_DEBUG:
                logger.debug(f"Don't put to cache: {request.get_full_path()}")
            return response

        # get the timeout from the "max-age" section of the "Cache-Control" header
        timeout = get_max_age(response)
        if timeout is None:
            # use default cache_timeout
            timeout = settings.CACHE_MIDDLEWARE_SECONDS
        elif timeout == 0:
            logger.debug("Don't cache this page (timeout == 0)")
            return response

        # Create a new HttpResponse for the cache, so we can skip existing
        # cookies and attributes like response.csrf_processing_done
        response2 = HttpResponse(
            content=response._container,
            status=200,
            content_type=response['Content-Type'],
        )
        if response.has_header("Content-Language"):
            response2['Content-Language'] = response['Content-Language']

        if settings.DEBUG or RUN_WITH_DEV_SERVER or request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
            # Check if we store a {% csrf_token %} into the cache, this should never happen!
            for content in response._container:
                if "csrfmiddlewaretoken" in content:
                    raise AssertionError(f"csrf_token would be put into the cache! content: {content!r}")

        # Adds ETag, Last-Modified, Expires and Cache-Control headers
        patch_response_headers(response2, timeout)

        cache_key = get_cache_key(request)
        cache.set(cache_key, response2, timeout)

        logger.debug(f"Put to cache: {cache_key!r}")
        return response
Beispiel #33
0
    def process_response(self, request, response):
        """
        RUS: Возвращает ответ сервера, если пользователь идентифицирован.
        """
        if request.user.is_authenticated():
            return response
        """Sets the cache, if needed."""
        if not self._should_update_cache(request, response):
            return response
        # RUS: Возвращает ответ сервера, если не было обновления кэша.

        if response.streaming or response.status_code != 200:
            return response
        # RUS: Возвращает ответ сервера, если, у атрибута HttpResponse.streaming статус False
        # или код ответа не равен 200.

        # Не кэширует ответы сервера, которые устанавливают специфичные для пользователя (и, возможно,
        # чувствительные к безопасности ) cookie в ответ на запрос без cookie
        # Don't cache responses that set a user-specific (and maybe security
        # sensitive) cookie in response to a cookie-less request.
        if not request.COOKIES and response.cookies and has_vary_header(
                response, 'Cookie'):
            return response

        # Try to get the timeout from the "max-age" section of the "Cache-
        # Control" header before reverting to using the default cache_timeout
        # length.
        timeout = get_max_age(response)
        if timeout is None:
            timeout = self.cache_timeout
        elif timeout == 0:
            # max-age was set to 0, don't bother caching.
            return response
        # Возвращает максимальный возраст из заголовка Cache-Control ответа в виде целого числа (
        # Пытается получить время хранения кэша из заголовка Cache-Control.
        # При его отсутствии, время хранения кэша равно значению по умолчанию,
        # если же установлено равным 0, возвращается некэшированный ответ сервера

        patch_response_headers(response, timeout)
        if timeout:
            cache_key = learn_cache_key(request,
                                        response,
                                        timeout,
                                        self.key_prefix,
                                        cache=self.cache)
            if hasattr(response, 'render') and callable(response.render):
                response.add_post_render_callback(
                    lambda r: self.cache.set(cache_key, r, timeout))
            else:
                self.cache.set(cache_key, response, timeout)
        return response
    def process_response(self, request, response):
        if COUNT_UPDATE_CACHE:
            self._count_response(request)

        if getattr(response, "_from_cache", False) == True:
            if COUNT_UPDATE_CACHE:
                self._count_hit()
            logger.debug("response comes from the cache, no need to update the cache")
            return response
        else:
            # used e.g. in unittests
            response._from_cache = False

        if not self.use_cache(request, response):
            if EXTRA_DEBUG:
                logger.debug("Don't put to cache: %s" % request.get_full_path())
            return response

        # get the timeout from the "max-age" section of the "Cache-Control" header
        timeout = get_max_age(response)
        if timeout == None:
            # use default cache_timeout
            timeout = settings.CACHE_MIDDLEWARE_SECONDS
        elif timeout == 0:
            logger.debug("Don't cache this page (timeout == 0)")
            return response

        # Create a new HttpResponse for the cache, so we can skip existing
        # cookies and attributes like response.csrf_processing_done
        response2 = HttpResponse(
            content=response._container,
            status=200,
            content_type=response['Content-Type'],
        )
        if response.has_header("Content-Language"):
            response2['Content-Language'] = response['Content-Language']

        if settings.DEBUG or RUN_WITH_DEV_SERVER or request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
            # Check if we store a {% csrf_token %} into the cache, this should never happen!
            for content in response._container:
                if "csrfmiddlewaretoken" in content:
                    raise AssertionError("csrf_token would be put into the cache! content: %r" % content)

        # Adds ETag, Last-Modified, Expires and Cache-Control headers
        patch_response_headers(response2, timeout)

        cache_key = get_cache_key(request)
        cache.set(cache_key, response2, timeout)

        logger.debug("Put to cache: %r" % cache_key)
        return response
Beispiel #35
0
    def process_response(self, request, response):
        if not self._should_update_cache(request, response):
            return super(CacheMiddleware, self).process_response(request, response)

        last_modified = 'Last-Modified' in response
        etag = 'ETag' in response

        request._cache_timeout = cache_timeout = self.get_cache_timeout(
            request,
            *request.resolver_match.args,
            **request.resolver_match.kwargs
        )

        conditional_vary_headers = [
            http_header
            for wsgi_header, http_header in self.CONDITIONAL_VARY_HEADERS.items()
            if wsgi_header in request.META
        ]
        if conditional_vary_headers:
            cache.patch_vary_headers(response, conditional_vary_headers)

        if response.status_code == 304:  # Not Modified
            cache.patch_response_headers(response, cache_timeout)
        else:
            update_response_cache = ResponseCacheUpdater(
                middleware=self,
                request=request,
                response=response,
            )
            response._closable_objects.append(update_response_cache)

            with patch(cache_middleware, 'learn_cache_key', lambda *_, **__: ''):
                # replace learn_cache_key with dummy one

                with patch(self, 'cache', dummy_cache):
                    # use dummy_cache to postpone cache update till the time
                    # when all values of Vary header are ready,
                    # see https://code.djangoproject.com/ticket/15855

                    with patch(self, 'cache_timeout', cache_timeout):
                        response = super(CacheMiddleware, self).process_response(request, response)

        if not last_modified:
            # patch_response_headers sets its own Last-Modified, remove it
            del response['Last-Modified']
        if not etag:
            # patch_response_headers sets its own ETag, remove it
            del response['ETag']

        return response
Beispiel #36
0
    def template_response_callback(self, response):
        if self.cache_middleware:
            response = self.cache_middleware.process_response(
                self.request, response)

        # We might want to cache for longer internally than we tell clients
        max_age = get_max_age(response)
        if max_age and self.max_age < max_age:
            # Remove headers so patch_response works
            for header in ('ETag', 'Last-Modified', 'Expires'):
                if response.has_header(header):
                    del response[header]
            patch_response_headers(response, self.max_age)
        return response
Beispiel #37
0
    def template_response_callback(self, response):
        if self.cache_middleware:
            response = self.cache_middleware.process_response(
                self.request, response)

        # We might want to cache for longer internally than we tell clients
        max_age = get_max_age(response)
        if max_age and self.max_age < max_age:
            # Remove headers so patch_response works
            for header in ('ETag', 'Last-Modified', 'Expires'):
                if response.has_header(header):
                    del response[header]
            patch_response_headers(response, self.max_age)
        return response
Beispiel #38
0
 def wrapper(request, *args, **kwargs):
     if request.method not in methods:
         return HttpResponseNotAllowed(methods)
     seconds_throttled = throttle_check(request)
     if seconds_throttled > 0:
         msg = u'Throttle limit exceeded. Try again in %d seconds.\n' % seconds_throttled
         response = HttpResponse(msg, status=503)
         response['Retry-After'] = str(seconds_throttled)
         response['Content-Type'] = 'text/plain'
         return response
     response = func(request, *args, **kwargs)
     if cache_timeout is not None:
         patch_response_headers(response, cache_timeout=cache_timeout)
     return response
Beispiel #39
0
    def view_page(self, request, pagename):
        try:
            view_func = _special_page_dict[pagename]
        except KeyError:
            raise Http404

        response = view_func(request, pagename)

        # special pages expire immediately since they frequently change
        from django.utils.cache import patch_cache_control, patch_response_headers
        patch_response_headers(response, cache_timeout=0)
        patch_cache_control(response, must_revalidate=True)

        return response
Beispiel #40
0
def process_response(req, res, prefix, cache_time=60 * 60):
    # update the cache using the django's CacheMiddleware
    cache_middleware = CacheMiddleware(cache_timeout=cache_time,
                                       key_prefix=prefix)
    response = cache_middleware.process_response(req, res)
    # update some header to prevent wrong client caching
    max_age = get_max_age(response)
    if max_age and max_age < max_age:
        # Remove headers so patch_response works
        for header in ('ETag', 'Last-Modified', 'Expires'):
            if response.has_header(header):
                del response[header]
        patch_response_headers(response, max_age)
    return response
Beispiel #41
0
def media_user_asset(request, user_id, filename):
    if request.user:
        user = get_object_or_404(get_user_model(), pk=user_id)
        if request.user == user:
            path = 'user/assets/%s/%s' % (user_id, filename)
            asset = get_object_or_404(UserAsset, user_id=user_id, file=path)
            full_path = asset.file.path
            fd = open(full_path, 'rb')
            file_mime = mimetypes.guess_type(asset.file.name.split('/')[-1])
            response = FileResponse(fd, content_type=file_mime)
            patch_response_headers(response, cache_timeout=60 * 60 * 24 * 7)
            return response

    raise Http404
Beispiel #42
0
    def test_middleware_limit_cache_not_set(self):
        """
        The MAX_BROWSER_CACHE_TTL setting should default to 600 if not set in the project
        """
        response = HttpResponse("OK")
        patch_response_headers(response, cache_timeout=3600)

        self.assertIn("max-age=3600", response["Cache-Control"])

        middleware = LimitBrowserCacheTTLHeaders(lambda request: response)
        response_to_test = middleware(HttpRequest())
        expected_expires = http_date(time.time() + 600)
        self.assertEqual(expected_expires, response_to_test["Expires"])
        self.assertIn("max-age=600", response_to_test["Cache-Control"])
Beispiel #43
0
    def process_response(self, request, response):
        # TODO if cache length gets long (like over 10 minutes), set a shorter
        # cache time by default on those.
        #
        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3
        # if response.status_code == 302:
        #     return response

        # Try to get the timeout from the "max-age" section of the "Cache-
        # Control" header before reverting to using the default cache_timeout
        # length inside patch_response_headers.
        timeout = get_max_age(response)
        patch_response_headers(response, timeout)
        return response
Beispiel #44
0
    def test_middleware_limit_not_reached(self):
        """
        Ensure that the middleware does not rewrite the response if the cache
        headers timeout are lower than MAX_BROWSER_CACHE_TTL
        """
        response = HttpResponse("OK")
        patch_response_headers(response, cache_timeout=30)
        expected_headers = response.serialize_headers()
        self.assertIn("max-age=30", response["Cache-Control"])

        with self.settings(MAX_BROWSER_CACHE_TTL=60):
            middleware = LimitBrowserCacheTTLHeaders(lambda request: response)
            response_to_test = middleware(HttpRequest())
            self.assertEqual(expected_headers, response_to_test.serialize_headers())
Beispiel #45
0
def user_avatar(request, user, size):
    """
    User avatar page.
    """
    user = get_object_or_404(User, username=user)

    if user.email == "*****@*****.**":
        return redirect(get_fallback_avatar_url(size))

    response = HttpResponse(content_type="image/png", content=get_avatar_image(user, size))

    patch_response_headers(response, 3600 * 24 * 7)

    return response
    def prepareReportJEDI(self, request):

        requestList = [11035, 11034, 11048, 11049, 11050, 11051, 11052, 11198, 11197, 11222, 11359]
        requestList = '(' + ','.join(map(str, requestList)) + ')'

        data = self.getCacheEntry(request, "prepareReportMC16")
        #data = None
        if data is not None:
            data = json.loads(data)
            data['request'] = request
            response = render_to_response('reportCampaign.html', data, RequestContext(request))
            patch_response_headers(response, cache_timeout=request.session['max_age_minutes'] * 60)
            return response

        (totalJobs, totalEvents, hepspecJobs, JediEventsR) = self.jobsQuery('and REQUESTID IN %s' % requestList)

        recentTasks = self.recentProgressReportDEFT('and t1.PR_ID IN %s' % requestList)
        recentTasks['title'] = 'Tasks updated during last 24 hours'
        totalTasks = self.getTasksDEFTSummary('and t1.PR_ID IN %s' % requestList)
        totalTasks['title'] = 'Tasks processing summary'

        worstSite = self.topSitesFailureRate(10, 'and t1.PR_ID IN %s' % requestList)
        siteActRun = self.topSitesActivatedRunning(10, 'and t1.PR_ID IN %s' % requestList)
        siteAssignRun = self.topSitesAssignedRunning(10, 'and t1.PR_ID IN %s' % requestList)

        data = {
                'title':'MC16a',
                "requestList":requestList,
                'viewParams': request.session['viewParams'],
                "JediEventsR":JediEventsR,
                "totalEvents": totalEvents,
                "totalTasks":totalTasks,
                "totalJobs":totalJobs,
                #"JediEventsHashsTable":jediEventsHashsTable,
                #"hashTable":hashTable,
                "recentTasks":recentTasks,
                "hepspecJobs":hepspecJobs,
                "worstSite":worstSite,
                "siteActRun":siteActRun,
                "siteAssignRun":siteAssignRun,
                "taskstatelist":self.taskstatelist,
                "taskstatelistDEFT": self.taskstatelistDEFT,
                "jobstatelist": self.jobstatelist,
                "steps":self.steps,
                "stepsLabels":self.stepsLabels,
                "taskstatelistRecent":self.taskstatelistRecent,
                "built": datetime.now().strftime("%d %b %Y %H:%M:%S")}
        self.setCacheEntry(request, "prepareReportMC16", json.dumps(data, cls=self.DateEncoder), 180*60)
        return render_to_response('reportCampaign.html', data, RequestContext(request))
Beispiel #47
0
    def process_response(self, request, response):
        """Sets the cache, if needed."""
        if (not hasattr(request, '_cache_update_cache')
                or not request._cache_update_cache):
            # We don't need to update the cache, just return.
            return response
        if request.method != 'GET':
            # This is a stronger requirement than above. It is needed
            # because of interactions between this middleware and the
            # HTTPMiddleware, which throws the body of a HEAD-request
            # away before this middleware gets a chance to cache it.
            return response
        if not response.status_code == 200:
            return response
        # Try to get the timeout from the "max-age" section of the "Cache-
        # Control" header before reverting to using the default cache_timeout
        # length.
        timeout = get_max_age(response)
        if timeout is None:
            timeout = self.cache_timeout
        elif timeout == 0:
            # max-age was set to 0, don't bother caching.
            return response

        if self.patch_headers:
            patch_response_headers(response, timeout)

        if timeout:
            if callable(self.key_prefix):
                key_prefix = self.key_prefix(request)
            else:
                key_prefix = self.key_prefix
            if self.post_process_response:
                response = self.post_process_response(response, request)

            with RequestPath(request, self.only_get_keys,
                             self.forget_get_keys):
                cache_key = learn_cache_key(request, response, timeout,
                                            key_prefix)

                if self.remember_all_urls:
                    self.remember_url(request, cache_key, timeout)

            self.cache.set(cache_key, response, timeout)

        if self.post_process_response_always:
            response = self.post_process_response_always(response, request)

        return response
Beispiel #48
0
    def get(self, request):
        form = TrendsPeriodForm(request.GET)

        if not form.is_valid():
            return JsonResponse(dict(errors=form.errors), status=400)

        capabilities = dsmr_backend.services.backend.get_capabilities()
        average_consumption_by_hour = dsmr_stats.services.average_consumption_by_hour(
            start=form.cleaned_data['start_date'],
            end=form.cleaned_data['end_date'],
        )
        data = {
            'hour_start': [],
            'avg_electricity': [],
            'avg_electricity_returned': [],
            'avg_gas': [],
        }

        for current in average_consumption_by_hour:
            hour_start = '{:0>2}:00'.format(int(current['hour_start']), )
            data['hour_start'].append(hour_start)

            avg_electricity = (current['avg_electricity1'] +
                               current['avg_electricity2']) / 2

            data['avg_electricity'].append(
                float(
                    dsmr_consumption.services.round_decimal(avg_electricity,
                                                            decimal_count=5)))

            if capabilities[Capability.ELECTRICITY_RETURNED]:
                avg_electricity_returned = (
                    current['avg_electricity1_returned'] +
                    current['avg_electricity2_returned']) / 2
                data['avg_electricity_returned'].append(
                    float(
                        dsmr_consumption.services.round_decimal(
                            avg_electricity_returned, decimal_count=5)))

            if capabilities[Capability.GAS]:
                data['avg_gas'].append(
                    float(
                        dsmr_consumption.services.round_decimal(
                            current['avg_gas'], decimal_count=5)))

        response = JsonResponse(data)
        patch_response_headers(response)

        return response
Beispiel #49
0
def datepublisher_response_processor(page, request, response):
    """
    This response processor is automatically added when the datepublisher
    extension is registered. It sets the response headers to match with
    the publication end date of the page so that upstream caches and
    the django caching middleware know when to expunge the copy.
    """
    expires = page.publication_end_date
    if expires is not None:
        delta = expires - timezone.now()
        delta = int(delta.days * 86400 + delta.seconds)
        try:
            patch_response_headers(response, delta)
        except InvalidTimeError:  # NonExistentTimeError and AmbiguousTimeError
            patch_response_headers(response, delta - 7200)
 def file_value(self, request, *args, **kwargs):
     attribute = kwargs['attribute']
     if attribute not in list(self._fields.keys()):
         raise Http404
     filter = {
         self.lookup_field: kwargs['pk'],
     }
     obj = get_object_or_404(self.model, **filter)
     file = getattr(obj, attribute)
     full_path = file.path
     fd = open(full_path, 'rb')
     file_mime = mimetypes.guess_type(file.name.split('/')[-1])
     response = FileResponse(fd, content_type=file_mime)
     patch_response_headers(response, cache_timeout=60 * 60 * 24 * 7)
     return response
Beispiel #51
0
async def result_json(request: HttpRequest, workflow_id: int, step_slug: str,
                      delta_id: int) -> HttpResponse:
    # raise Http404, PermissionDenied
    _, step = await _load_workflow_and_step(request, workflow_id, step_slug)
    cached_result = step.cached_render_result
    if cached_result is None or cached_result.delta_id != delta_id:
        return JsonResponse({"error": "render result not in cache"},
                            status=status.NOT_FOUND)
    if not cached_result.json:
        return JsonResponse({"error": "render result has no JSON"},
                            status=status.NOT_FOUND)

    response = JsonResponse(cached_result.json, safe=False)
    patch_response_headers(response, cache_timeout=600)
    return response
Beispiel #52
0
def user_avatar(request, user, size):
    """User avatar view."""
    user = get_object_or_404(User, username=user)

    if user.email == "*****@*****.**":
        return redirect(get_fallback_avatar_url(size))
    if user.email == "noreply+{}@weblate.org".format(user.pk):
        return redirect(os.path.join(settings.STATIC_URL, "state/ghost.svg"))

    response = HttpResponse(content_type="image/png",
                            content=get_avatar_image(user, size))

    patch_response_headers(response, 3600 * 24 * 7)

    return response
        def patched_view(request, *args, **kwargs):

            if settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY:
                # If the site is using the cache middleware, and it is set
                # to only cache for anonymous users, then we will check their
                # authentication status. This works because the middleware
                # supports the vary_on_view decorators.
                authenticated = request.user.is_authenticated()
            else:
                # This is the normal use-case. We want to avoid accessing the
                # request.user object as much as possible. Set authenticated
                # to None to indicate that we did not even check.
                authenticated = None

            if authenticated is False and not hasattr(viewfunc,
                                                      '_vary_on_view'):
                # If we did check the user's authentication status, and found
                # that they are anonymous, then we want ensure that we are
                # using a "vary on view" decorator. If it was not defined
                # specifically on the view, then we can assume that we just
                # need to cache all anonymous users exactly the same. For this,
                # the vary_on_authentication_status can be used.
                response = vary_on_authentication_status(viewfunc)(request,
                                                                   *args,
                                                                   **kwargs)
            else:
                # If we did not check the authentication status, or we did
                # check and found that they are authenticated, then we can
                # just use the view as it is.
                response = viewfunc(request, *args, **kwargs)

            if request.method != 'GET':
                return response
            if response.status_code != 200:
                return response
            if request.is_secure():
                return response

            if authenticated is True:
                # We checked if the user is authenticated, and they were,
                # so do not cache the response.
                add_never_cache_headers(response)
            else:
                # Either the user is not authenticated, or we did not check.
                # In either case, add the caching headers.
                patch_response_headers(response, cache_timeout=cache_timeout)

            return response
 def dispatch(self, request, *args, **kwargs):
     response = super(HttpCacheMixin,
                      self).dispatch(request, *args, **kwargs)
     if self.cacheable(request, response):
         last_modified = self.get_last_modified()
         if last_modified is not None:
             response['Last-Modified'] = last_modified
         etag = self.get_etag()
         if etag is not None:
             response['ETag'] = etag
         cache_timeout = int(self.get_cache_timeout())
         patch_response_headers(response, cache_timeout)
         cache_varies = self.get_cache_varies()
         if len(cache_varies):
             patch_vary_headers(response, cache_varies)
     return response
Beispiel #55
0
    def test_middleware_limit_reached(self):
        """
        Ensure that the middleware rewrite the response if the cache headers
        timeout are greater than MAX_BROWSER_CACHE_TTL
        """
        response = HttpResponse("OK")
        patch_response_headers(response, cache_timeout=3600)

        self.assertIn("max-age=3600", response["Cache-Control"])

        with self.settings(MAX_BROWSER_CACHE_TTL=5):
            middleware = LimitBrowserCacheTTLHeaders(lambda request: response)
            response_to_test = middleware(HttpRequest())
            expected_expires = http_date(time.time() + 5)
            self.assertEqual(expected_expires, response_to_test["Expires"])
            self.assertIn("max-age=5", response_to_test["Cache-Control"])
Beispiel #56
0
    def get_buffered_image(self):
        request = self.request
        layers = self.param_get(request.GET, 'layers')
        srs = self.param_get(request.GET, 'srs')
        bbox = self.param_get(request.GET, 'bbox', '')
        if bbox:
            try:
                bbox = list(map(float, bbox.split(',')))
            except Exception:
                bbox = []
        width = self.param_get(request.GET, 'width', '')
        height = self.param_get(request.GET, 'height', '')
        dpi = self.param_get(request.GET, 'dpi', '')

        url = self.build_url(request)
        size_matches = False
        if self.get_wms_tile_sizes():
            size_requested = '%s,%s' % (width, height)
            for line in self.get_wms_tile_sizes():
                if line == size_requested:
                    size_matches = True
                    break
        if len(bbox) == 4 and size_matches:
            image = None
            wms_options = {
                'url': url,
                'layers': layers,
                'bbox': bbox,
                'width': width,
                'height': height,
                'dpi': dpi,
                'srs': srs
            }
            buffer = list(map(int, self.get_wms_buffer_size().split(',')))
            try:
                image = tile_cache_image(wms_options, buffer)
            except Exception as e:
                msg = 'wms request is not a tile: %s' % e
                print(msg)
                if settings.DEBUG:
                    raise Exception(msg)
            else:
                response = HttpResponse(image, content_type='image/png')
                patch_response_headers(response,
                                       cache_timeout=60 * 60 * 24 * 7)
                response.status_code = 200
                return response
Beispiel #57
0
def ecpf(request):

    produtos = table(TabelaEcpf)

    infos = InfoEcpf.objects.all()

    template = 'certificado-digital/e-cpf.html'
    context = {
        'produtos': produtos,
        'certificado': 'e-cpf',
        'saiba_mais_produto': 'e-CPF',
        'Infos': infos,
    }

    response = render(request, template, context)
    patch_response_headers(response, cache_timeout=7884000)
    return response
Beispiel #58
0
def nfe(request):

    produtos, tabela_nfe = table(TabelaNFe)

    infos = InfoNFe.objects.all()
    template = 'certificado-digital/nf-e.html'
    context = {
        'produtos': produtos,
        'certificado': 'nf-e',
        'saiba_mais_produto': 'NF-e',
        'tabela_nfe': tabela_nfe,
        'Infos': infos,
    }

    response = render(request, template, context)
    patch_response_headers(response, cache_timeout=7884000)
    return response
Beispiel #59
0
def project_image(request, slug):
    """ Loads data, calculates score, displays image.
        views.project_info() will pass in a cache_timeout=0 when logout is clicked.
        Called by load of github readme page (any page containing a badge-link), views.info(), and views.project_info(). """
    cache_timeout = int(request.GET.get('cache_timeout', '5'))
    tracker = get_object_or_404(Tracker, slug=slug)
    log.debug('real tracker.score, `%s`' % tracker.score)
    log.debug('request.user.is_authenticated, `%s`' %
              request.user.is_authenticated)
    score_display = str(tracker.score)
    if tracker.score < settings_app.PUBLIC_SCORE_CUTOFF:
        if not request.user.is_authenticated:
            score_display = '&lt; %s' % settings_app.PUBLIC_SCORE_CUTOFF
    svg = image_helper.prep_svg(tracker.score, score_display)
    resp = HttpResponse(svg, content_type="image/svg+xml")
    patch_response_headers(resp, cache_timeout=cache_timeout)
    return resp