206 lines
5.5 KiB
Python
206 lines
5.5 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 datetime import datetime
|
|
from json import dumps
|
|
|
|
from django.http import HttpResponse, Http404
|
|
from django.template import loader
|
|
from django.template.defaultfilters import register
|
|
|
|
from .models import Country, Stamp, Coin, User
|
|
|
|
|
|
def index(request):
|
|
''' index view '''
|
|
|
|
template = loader.get_template('coinc/index.html')
|
|
countrys = Country.objects.order_by('name_iso')
|
|
users = User.objects.order_by('id')
|
|
context = {
|
|
'countrys': countrys,
|
|
'users': users
|
|
}
|
|
|
|
return HttpResponse(template.render(context, request))
|
|
|
|
|
|
def detail_country(request, name_iso):
|
|
''' wrapper_view for a *single* country '''
|
|
|
|
template = loader.get_template('coinc/country.html')
|
|
try:
|
|
country = Country.objects.get(name_iso=name_iso)
|
|
except Country.DoesNotExist:
|
|
raise Http404(f"Das Land '{name_iso}' ist nicht vorhanden")
|
|
|
|
context = show_country(country, single_country=True)
|
|
|
|
return HttpResponse(template.render(context, request))
|
|
|
|
|
|
@register.inclusion_tag('coinc/country.html')
|
|
def show_country(country, single_country=False):
|
|
''' detail_view for a country '''
|
|
|
|
c = {
|
|
'name_iso': country.name_iso,
|
|
'name': country.name,
|
|
'comment': country.comment,
|
|
'years': {}
|
|
}
|
|
|
|
irregular_stamps = {}
|
|
stamps = Stamp.objects.filter(country=country.name_iso).values(
|
|
'name_short', 'years')
|
|
|
|
if stamps:
|
|
temp_stamps = {}
|
|
|
|
for stamp in stamps:
|
|
stamp_name_short = stamp['name_short']
|
|
|
|
if stamp['years'] == "":
|
|
temp_stamps[stamp_name_short] = {}
|
|
else:
|
|
for year in stamp['years'].split(","):
|
|
irregular_stamps[year] = {}
|
|
irregular_stamps[year][stamp_name_short] = {}
|
|
temp_stamps[''] = {}
|
|
|
|
stamps = temp_stamps
|
|
else:
|
|
stamps = {'': {}}
|
|
|
|
year_now = datetime.now().year
|
|
for year in [str(i) for i in range(country.euro_member_since, year_now + 1)]:
|
|
stamps_per_year = stamps
|
|
|
|
if irregular_stamps and year in irregular_stamps:
|
|
for irregular_stamp, value in irregular_stamps[year].items():
|
|
stamps_per_year[irregular_stamp] = {}
|
|
|
|
# TODO this is not a fix, when more stamps with uniqe years get added
|
|
elif country.name_iso == 'gr':
|
|
stamps_per_year = {'': {}}
|
|
|
|
year_short = year[2:4]
|
|
c['years'][year_short] = {}
|
|
|
|
for stamp in stamps_per_year:
|
|
c['years'][year_short][stamp] = {'name': stamp}
|
|
|
|
stamps_per_year = {}
|
|
|
|
return {
|
|
'country': c,
|
|
'single_country': single_country,
|
|
'users': User.objects.order_by('id').values('name','color')
|
|
}
|
|
|
|
|
|
@register.inclusion_tag('coinc/year.html')
|
|
def show_year(country, year):
|
|
''' show a year '''
|
|
|
|
return {
|
|
'year': country['years'][year],
|
|
'year_short': year,
|
|
'values': [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203],
|
|
'country': country['name_iso']
|
|
}
|
|
|
|
|
|
@register.inclusion_tag('coinc/coin.html')
|
|
def show_coin(country, year, stamp, value):
|
|
''' show a coin
|
|
@param country: Country.name_iso
|
|
@param year: int YYYY
|
|
@param stamp: stamp_id
|
|
@param value: int e [1,2,5,10,20,50,100,200,201,202,203]
|
|
'''
|
|
|
|
# TODO fix this before the end of 2098
|
|
year = int(year)
|
|
if year == 99:
|
|
year += 1900
|
|
else:
|
|
year += 2000
|
|
|
|
if stamp == "":
|
|
stamp = None
|
|
|
|
try:
|
|
coin = Coin.objects.get(country=country, year=year, stamp=stamp, value=value)
|
|
except Coin.DoesNotExist:
|
|
return {}
|
|
|
|
if coin.buy_only:
|
|
td_class = 'buy_only'
|
|
elif coin.circulation and coin.circulation < 1500000:
|
|
td_class = 'rare'
|
|
elif coin.noexist:
|
|
td_class = 'noexist'
|
|
elif coin.value in [10, 20, 50]:
|
|
td_class = 'brass'
|
|
|
|
if coin.in_collector:
|
|
td_class += ' in_collector'
|
|
elif coin.checked:
|
|
td_class += ' coin_checked'
|
|
|
|
if coin.found_by:
|
|
div_class = f"found {coin.found_by}"
|
|
marker = 'x'
|
|
|
|
return {
|
|
'marker': marker,
|
|
'td_class': td_class,
|
|
'div_class': div_class,
|
|
}
|
|
|
|
|
|
def add_user(request, username, color):
|
|
''' add a user '''
|
|
|
|
status = 0
|
|
message = ''
|
|
|
|
if username and color:
|
|
try:
|
|
existing_user = User.objects.get(name=username)
|
|
status = 1
|
|
message = f"'{username}' ist schon vergeben."
|
|
|
|
except User.DoesNotExist:
|
|
User.objects.create(name=username, color=color)
|
|
message = 'success'
|
|
else:
|
|
status = 1
|
|
message = 'Du musst einen Namen und Farbe angeben.'
|
|
|
|
return HttpResponse(dumps({ 'status': status, 'message': message }))
|
|
|
|
|
|
# TODO
|
|
def statistic(request):
|
|
''' show statistics '''
|
|
|
|
return HttpResponse('Statistik')
|