#13 intermediate

This commit is contained in:
moritz münch 2021-05-29 09:53:42 +02:00
parent 9d3c246436
commit d9bfb40bfc
5 changed files with 18 additions and 45 deletions

View File

@ -24,13 +24,7 @@ from random import random
from django.core.cache import cache
from django.http import Http404
#todo we dont need this, no db queries
from .models import Coin
from .models import Country
def get_etag_index(request):
return cache.get(f'etag_index', set_etag_index())
def get_last_modified_index(request):
@ -46,6 +40,7 @@ def set_last_modified_index():
cache.set(f'last_modified_index', last_modified)
return last_modified
def get_last_modified_country(request, name_iso):
return cache.get(f'last_modified_{name_iso}', set_last_modified_country(name_iso))
@ -58,28 +53,3 @@ def set_last_modified_country(name_iso):
raise Http404(f'Das Land "{name_iso}" existiert nicht')
cache.set(f'last_modified_{name_iso}', last_modified)
return last_modified
def set_etag_index():
etag = ''
for country in Country.objects.order_by('name_iso'):
etag += get_etag_country_(country.name_iso)
etag = sha256(etag.encode('utf-8')).hexdigest()[0:9]
cache.set(f'etag_index', etag)
return etag
def get_etag_country_(name_iso):
return cache.get(f'etag_{name_iso}', set_etag_country(name_iso))
def get_etag_country(request, name_iso):
return get_etag_country_(name_iso)
def set_etag_country(name_iso):
print(name_iso, end='')
print(cache.get(f'etag_{name_iso}', f'error {name_iso}'))
etag = sha256(str(random()).encode('utf-8')).hexdigest()[0:9]
cache.set(f'etag_{name_iso}', etag)
return etag

View File

@ -1,9 +1,11 @@
{% load cache %}
{% csrf_token %}
{% cache None controlbar user %}
<style type="text/css">{% for user in users %}
div.{{ user.name }} { background-color: {{ user.color }} !important; }{% endfor %}
</style>
<div id="button_control">
{% csrf_token %}
{% if user.is_authenticated %}
<button id="begin_edit" class="btn btn-info" title="Hinzufügen"><i class="fas fa-plus"></i><span> Hinzufügen</span></button>
<button id="do_logout" class="btn btn-info" title="Abmelden"><i class="fas fa-sign-out-alt"></i></button>
@ -14,7 +16,6 @@
<!-- navbar -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
{% csrf_token %}
<!-- advanced options -->
<div id="advanced_options">
@ -114,7 +115,7 @@
</div>
</div>
</div>
{% endcache %}
<!-- modal login -->
{% if not user.is_authenticated %}
<div id="login" class="modal-container">

View File

@ -1,3 +1,5 @@
{% load cache %}
{% cache None header %}
<!DOCTYPE html>
<!--
@ -53,3 +55,4 @@
<title>{{ title }}</title>
</head>
<body>
{% endcache %}

View File

@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from datetime import datetime, date
from json import dumps
@ -24,18 +25,15 @@ from django.conf import settings
from django.http import HttpResponse, Http404
from django.template import loader
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,
from .cache import (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_
@condition(etag_func=get_etag_index, last_modified_func=get_last_modified_index)
@condition(last_modified_func=get_last_modified_index)
def index(request):
''' index view '''
@ -52,7 +50,7 @@ def index(request):
return HttpResponse(template.render(context, request))
@condition(etag_func=get_etag_country, last_modified_func=get_last_modified_country)
@condition(last_modified_func=get_last_modified_country)
def detail_country(request, name_iso):
''' wrapper_view for a *single* country '''
@ -181,6 +179,7 @@ def show_country(country, single_country=False):
c['years'][str(coin.year)][stamp][coin.value] = {
'marker': marker,
'name': name,
'date_modified': coin.date_modified,
'td_class': td_class,
'div_class': div_class,
'special_class': special_class,
@ -212,6 +211,7 @@ def show_coin(coin, value):
'value': value,
'marker': coin['marker'],
'name': coin['name'],
'date_modified': coin['date_modified'],
'td_class': coin['td_class'],
'div_class': coin['div_class'],
'special_class': coin['special_class'],
@ -374,10 +374,8 @@ 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)
set_last_modified_country(country.name_iso)
return response()

View File

@ -44,10 +44,11 @@ INSTALLED_APPS = [
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
# 'django.middleware.cache.UpdateCacheMiddleware', # per-site cache
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
# 'django.middleware.cache.FetchFromCacheMiddleware', # per-site cache
'django.middleware.csrf.CsrfViewMiddleware',
# 'django.middleware.http.ConditionalGetMiddleware', # set 'ETag' and 'Last Modified' headers
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',