def get_last_instancetest(instance, url_pk):
instancetest = None
if url_pk is not None:
# find the last test
instancetest = instance.instancetest_set.select_related('certificate').filter(url=url_pk).latest('timestamp')
if instancetest is not None:
# there is one
add_viewinfo(instancetest)
else:
# there is no test
instancetest = {}
instancetest['result_class'] = 'label-default'
instancetest['result_msg'] = 'Untested'
instancetest['url'] = url
instancetest['instance_pk'] = instance.pk
return instancetest
# @cache_page(60 * 15)
python类cache_page()的实例源码
def cache_qr_code():
"""
Decorator that caches the requested page if a settings named 'QR_CODE_CACHE_ALIAS' exists and is not empty or None.
"""
def decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *view_args, **view_kwargs):
cache_enabled = request.GET.get('cache_enabled', True)
if cache_enabled and hasattr(settings, 'QR_CODE_CACHE_ALIAS') and settings.QR_CODE_CACHE_ALIAS:
# We found a cache alias for storing the generate qr code and cache is enabled, use it to cache the
# page.
timeout = settings.CACHES[settings.QR_CODE_CACHE_ALIAS]['TIMEOUT']
response = cache_page(timeout, cache=settings.QR_CODE_CACHE_ALIAS)(view_func)(request, *view_args, **view_kwargs)
else:
# No cache alias for storing the generated qr code, call the view as is.
response = (view_func)(request, *view_args, **view_kwargs)
return response
return _wrapped_view
return decorator
def cache():
return cache_page(getattr(settings, 'CACHE_TIME', 0)) \
if getattr(settings, 'ENABLE_CACHE', False) else lambda x: x
def index(request):
"""
??
:param request:
:return:
"""
post_list = Post.objects.filter(is_pub=True).filter(category__is_pub=True).order_by(
'-create_time') # ?????????
page = request.GET.get('page')
is_paginated = False
if post_list.count() > PAGE_SIZE:
is_paginated = True
paginator = Paginator(post_list, PAGE_SIZE)
try:
page_obj = paginator.page(page)
except PageNotAnInteger:
# ??page??????????
page_obj = paginator.page(1)
except EmptyPage:
# ??page?????????????
page_obj = paginator.page(paginator.num_pages)
# pagination_data????????????
pagination_data = PaginationBlogPost(paginator=paginator,
page_obj=page_obj, is_paginated=is_paginated).pagination_data()
pagination_data['post_list'] = page_obj
pagination_data['page_obj'] = page_obj
pagination_data['paginator'] = paginator
return render(request, 'blog/index.html', pagination_data)
# @cache_page(60 * 30)
def preview(request, pk):
"""
????
:param request:
:param pk: ??id
:return:
"""
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/detail.html', {'post': post}) # @cache_page(60 * 30)
# @cache_page(60 * 30)
def category(request, pk):
"""
??????????????
:param request:
:param pk: ????
:return:
"""
cate = get_object_or_404(Category, pk=pk)
if cate.is_pub:
post_list = Post.objects.filter(category=cate).filter(is_pub=True).order_by('-create_time')
else:
raise Http404('????????')
page = request.GET.get('page')
is_paginated = False
if post_list.count() > PAGE_SIZE:
is_paginated = True
paginator = Paginator(post_list, PAGE_SIZE)
try:
page_obj = paginator.page(page)
except PageNotAnInteger:
# ??page??????????
page_obj = paginator.page(1)
except EmptyPage:
# ??page?????????????
page_obj = paginator.page(paginator.num_pages)
# pagination_data????????????
pagination_data = PaginationBlogPost(paginator=paginator, page_obj=page_obj,
is_paginated=is_paginated).pagination_data()
pagination_data['post_list'] = page_obj
pagination_data['page_obj'] = page_obj
pagination_data['paginator'] = paginator
return render(request, 'blog/index.html', pagination_data)
# @cache_page(60 * 30)
def healthz(request):
"""for use with healthchecks. wrapped with `cache_page` to test redis cache."""
# here to make sure DB is working
assert Newsletter.objects.exists(), 'no newsletters exist'
return HttpResponse('OK')
def dispatch(self, *args, **kwargs):
return cache_page(self.get_cache_timeout())(
super(CacheMixin, self).dispatch)(*args, **kwargs)
def cache_on_auth(timeout):
"""Cache the response based on whether or not the user is authenticated.
Do NOT use on views that include user-specific information, e.g., CSRF tokens.
"""
# https://stackoverflow.com/questions/11661503/django-caching-for-authenticated-users-only
def _decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
key_prefix = "_auth_%s_" % request.user.is_authenticated()
return cache_page(timeout, key_prefix=key_prefix)(view_func)(request, *args, **kwargs)
return _wrapped_view
return _decorator
def cache_if_anon(timeout):
"""Cache the view if the user is not authenticated and there are no messages to display."""
# https://stackoverflow.com/questions/11661503/django-caching-for-authenticated-users-only
def _decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if request.user.is_authenticated() or messages.get_messages(request):
return view_func(request, *args, **kwargs)
else:
return cache_page(timeout)(view_func)(request, *args, **kwargs)
return _wrapped_view
return _decorator
def archives(request, year, month):
"""
????????????????????
??????????????
:param request:
:param year:
:param month:
:return:
"""
post_list = Post.objects.filter(
is_pub=True).filter(
category__is_pub=True).filter(
create_time__year=year,
create_time__month=month).order_by(
'-create_time')
page = request.GET.get('page')
is_paginated = False
if post_list.count() > PAGE_SIZE:
is_paginated = True
paginator = Paginator(post_list, PAGE_SIZE)
try:
page_obj = paginator.page(page)
except PageNotAnInteger:
# ??page??????????
page_obj = paginator.page(1)
except EmptyPage:
# ??page?????????????
page_obj = paginator.page(paginator.num_pages)
# pagination_data????????????
pagination_data = PaginationBlogPost(paginator=paginator, page_obj=page_obj,
is_paginated=is_paginated).pagination_data()
pagination_data['post_list'] = page_obj
pagination_data['page_obj'] = page_obj
pagination_data['paginator'] = paginator
return render(request, 'blog/index.html', pagination_data)
# @cache_page(60 * 30)
def tag(request, pk):
"""
????????????
:param request:
:param pk: tag?id
:return:
"""
t = get_object_or_404(Tag, pk=pk)
if t.is_pub:
post_list = Post.objects.filter(tags=t).filter(is_pub=True).filter(category__is_pub=True).order_by(
'-create_time')
page = request.GET.get('page')
is_paginated = False
if post_list.count() > PAGE_SIZE:
is_paginated = True
paginator = Paginator(post_list, PAGE_SIZE)
try:
page_obj = paginator.page(page)
except PageNotAnInteger:
# ??page??????????
page_obj = paginator.page(1)
except EmptyPage:
# ??page?????????????
page_obj = paginator.page(paginator.num_pages)
# pagination_data????????????
pagination_data = PaginationBlogPost(paginator=paginator, page_obj=page_obj,
is_paginated=is_paginated).pagination_data()
pagination_data['post_list'] = page_obj
pagination_data['page_obj'] = page_obj
pagination_data['paginator'] = paginator
return render(request, 'blog/index.html', pagination_data)
else:
# ???????tag????
return HttpResponseForbidden('Forbidden 403')
# @cache_page(60 * 60) # ????