#13 this one was missing still for cache stuff
This commit is contained in:
parent
8ee6ae9016
commit
9d3c246436
85
coinmanager/coinc/cache.py
Normal file
85
coinmanager/coinc/cache.py
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
# encoding: utf-8
|
||||||
|
#
|
||||||
|
# Copyright (C) 2020 willipink.eu
|
||||||
|
# Author Moritz Münch moritzmuench@mailbox.org
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
from hashlib import sha256
|
||||||
|
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):
|
||||||
|
return cache.get(f'last_modified_index', set_last_modified_index())
|
||||||
|
|
||||||
|
|
||||||
|
def set_last_modified_index():
|
||||||
|
''' set and return the datetime of the last coin added '''
|
||||||
|
try:
|
||||||
|
last_modified = Coin.objects.latest('date_modified').date_modified
|
||||||
|
except Coin.DoesNotExist:
|
||||||
|
raise Http404(f'Das Land "{name_iso}" existiert nicht')
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
|
def set_last_modified_country(name_iso):
|
||||||
|
''' return the datetime of the last coin added to a country '''
|
||||||
|
try:
|
||||||
|
last_modified = Coin.objects.filter(country=name_iso).latest('date_modified').date_modified
|
||||||
|
except Coin.DoesNotExist:
|
||||||
|
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
|
54
coinmanager/coinc/helper.py
Normal file
54
coinmanager/coinc/helper.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
# encoding: utf-8
|
||||||
|
#
|
||||||
|
# Copyright (C) 2020 willipink.eu
|
||||||
|
# Author Moritz Münch moritzmuench@mailbox.org
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
from .models import Country, Coin
|
||||||
|
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user