#13 update global cache backend, add last_modified complete, add etag for documentation reasons, will be removed in next commit

This commit is contained in:
moritz münch 2020-12-20 14:33:54 +01:00
parent 2ca47411ef
commit 8ee6ae9016
2 changed files with 29 additions and 48 deletions

View File

@ -27,52 +27,15 @@ from django.template.defaultfilters import register
from django.views.decorators.cache import cache_page
from django.views.decorators.http import condition
from .cache import (get_etag_index, set_etag_index,
get_etag_country, set_etag_country,
get_last_modified_index, set_last_modified_index,
get_last_modified_country, set_last_modified_country)
from .models import Country, Stamp, Coin, User
from .helper import total_coin_sum, coin_sum_of_
def total_coin_sum():
''' add each coin_sum from each country and return the result
>>> total_coin_sum()
173.57
'''
total_coin_sum = 0
for country in Country.objects.order_by('name'):
total_coin_sum += coin_sum_of_(country)
return total_coin_sum
def coin_sum_of_(country):
''' calculate the sum of all coins from a country
>>> coin_sum_of_(germany)
346.82
'''
coin_count = {'total': 0}
for value in [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203]:
coin_count[value] = Coin.objects.filter(
country=country,
value=value
).exclude(found_by__isnull=True).count()
if value > 200:
coin_count['total'] += 200 * coin_count[value]
else:
coin_count['total'] += value * coin_count[value]
return coin_count['total'] / 100
def latest_coin_added_to_(request, name_iso):
''' return the datetime of the last coin added to a country
>>> latest_coin_added_to_(germany)
datetime.datetime
'''
return Coin.objects.filter(country=name_iso).latest('date_modified').date_modified
@condition(etag_func=get_etag_index, last_modified_func=get_last_modified_index)
def index(request):
''' index view '''
@ -83,13 +46,13 @@ def index(request):
context = {
'countrys': countrys,
'users': users,
'coin_sum': f"{coin_sum:.2f}"
'coin_sum': f'{coin_sum:.2f}'
}
return HttpResponse(template.render(context, request))
@condition(last_modified_func=latest_coin_added_to_)
@condition(etag_func=get_etag_country, last_modified_func=get_last_modified_country)
def detail_country(request, name_iso):
''' wrapper_view for a *single* country '''
@ -292,6 +255,8 @@ def add_coin(request):
name no - str
found_by no None obj User
found_on no None str %Y-%m-%d
date-added no auto str %Y-%m-%d %H:%M:%S.%s
date-modified no auto str %Y-%m-%d %H:%M:%S.%s
circulation no 0 int [0; 1000000000]
buy_only no False bool
checked no False bool
@ -409,6 +374,10 @@ def add_coin(request):
in_collector = ec,
exists = exists)
set_etag_index()
set_last_modified_index()
set_etag_country(name_iso)
set_last_modified_country(name_iso)
return response()

View File

@ -129,6 +129,18 @@ MAINTENANCE_MODE_READ_ONLY = False
# do not redirect after login
LOGIN_REDIRECT_URL = './'
# caching
CACHE_MIDDLEWARE_ALIAS = willipink_coinc
CACHE_MIDDLEWARE_SECONDS = 600
# caching modes, i.e. file based, memcached, or other fancy stuff
# https://docs.djangoproject.com/en/3.1/topics/cache/#cache-arguments
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
'TIMEOUT': None,
}
}
# per-site cache
# updatecachemiddleware: adds 'Expires' and 'Cache-Control' headers to give the
# page a maximum age CACHE_MIDDLEWARE_SECONDS
# CACHE_MIDDLEWARE_ALIAS = 'default'
# CACHE_MIDDLEWARE_SECONDS = 600