From 8ee6ae9016d7771f82609d5417e3477a52239a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?moritz=20m=C3=BCnch?= Date: Sun, 20 Dec 2020 14:33:54 +0100 Subject: [PATCH] #13 update global cache backend, add last_modified complete, add etag for documentation reasons, will be removed in next commit --- coinmanager/coinc/views.py | 59 +++++++---------------------- coinmanager/coinmanager/settings.py | 18 +++++++-- 2 files changed, 29 insertions(+), 48 deletions(-) diff --git a/coinmanager/coinc/views.py b/coinmanager/coinc/views.py index 52d28e2..1824f43 100644 --- a/coinmanager/coinc/views.py +++ b/coinmanager/coinc/views.py @@ -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() diff --git a/coinmanager/coinmanager/settings.py b/coinmanager/coinmanager/settings.py index 2b5ddae..7181936 100644 --- a/coinmanager/coinmanager/settings.py +++ b/coinmanager/coinmanager/settings.py @@ -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