55 lines
1.7 KiB
Python

# 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