diff --git a/coinmanager/coinc/cache.py b/coinmanager/coinc/cache.py new file mode 100644 index 0000000..79a5ef0 --- /dev/null +++ b/coinmanager/coinc/cache.py @@ -0,0 +1,55 @@ +# 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 . + + + +from hashlib import sha256 +from random import random + +from django.core.cache import cache +from django.http import Http404 + +from .models import Coin + + +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 diff --git a/coinmanager/coinc/helper.py b/coinmanager/coinc/helper.py new file mode 100644 index 0000000..0d7aca4 --- /dev/null +++ b/coinmanager/coinc/helper.py @@ -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 . + + +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 diff --git a/coinmanager/coinc/models.py b/coinmanager/coinc/models.py index 0fb7a6f..fd7bfeb 100644 --- a/coinmanager/coinc/models.py +++ b/coinmanager/coinc/models.py @@ -20,7 +20,7 @@ from datetime import datetime, date -from django.db.models import BooleanField, CASCADE, CharField, DateField, ForeignKey, Model, PositiveIntegerField, PositiveSmallIntegerField, TextField +from django.db.models import BooleanField, CASCADE, CharField, DateField, DateTimeField, ForeignKey, Model, PositiveIntegerField, PositiveSmallIntegerField, TextField year_now = int(datetime.now().year) @@ -85,6 +85,10 @@ class Coin(Model): found_on = DateField('Eingetragen am', default=date.today) + date_added = DateTimeField('Hinzugefügt', auto_now_add=True) + + date_modified = DateTimeField('Geändert', auto_now=True) + circulation = PositiveIntegerField('Auflage', default=0) buy_only = BooleanField('Kursmünze', default=False) diff --git a/coinmanager/coinc/templates/coinc/controlbar.html b/coinmanager/coinc/templates/coinc/controlbar.html index 0dd940f..d46291e 100644 --- a/coinmanager/coinc/templates/coinc/controlbar.html +++ b/coinmanager/coinc/templates/coinc/controlbar.html @@ -1,9 +1,11 @@ +{% load cache %} +{% csrf_token %} +{% cache None controlbar user %}
- {% csrf_token %} {% if user.is_authenticated %} @@ -14,7 +16,6 @@
- +{% endcache %} {% if not user.is_authenticated %}