This commit is contained in:
koksnuss 2020-05-11 14:01:15 +02:00
parent 72b5dad648
commit 31137d33e8
2 changed files with 116 additions and 112 deletions

View File

@ -23,7 +23,7 @@ let settings = {
found: true, found: true,
found_by: '', found_by: '',
found_on: '', found_on: '',
circulation: '', circulation: 0,
buy_only: false, buy_only: false,
checked: false, checked: false,
ec: false, ec: false,
@ -77,7 +77,7 @@ function render_login_color(color) {
// show/update #found_by and #found_on // show/update #found_by and #found_on
function render_found() { function render_found() {
let checked = $('#found').prop('checked'); let checked = $('#found').prop('checked');
if (checked) { if (checked === true) {
$('#found_by option[selected=selected]').html(settings['found_by']); $('#found_by option[selected=selected]').html(settings['found_by']);
let found_on = '' let found_on = ''
if (settings['found_on']) { if (settings['found_on']) {
@ -93,7 +93,7 @@ function render_found() {
} }
$('#found_by, #found_on').each(function() { $('#found_by, #found_on').each(function() {
if (checked) { if (checked === true) {
$(this).prop('disabled', ''); $(this).prop('disabled', '');
} else { } else {
$(this).prop('disabled', 'disabled'); $(this).prop('disabled', 'disabled');
@ -142,15 +142,6 @@ function render_response(td, set) {
// save/update global settings // save/update global settings
function save_settings() { function save_settings() {
let found_by = $('#found_by option[selected=selected]').html();
if (found_by) { settings['found_by'] = found_by; }
let found_on = $('#found_on').val();
settings['found_on'] = (found_on) ? found_on : get_datetime();
let circulation = $('#circulation').val();
if (circulation) { settings['circulation'] = circulation; }
let checkboxes = $('#found, #buy_only, #checked, #ec, #exists').map(function() { let checkboxes = $('#found, #buy_only, #checked, #ec, #exists').map(function() {
return { [$(this).prop('id')]: $(this).prop('checked') } return { [$(this).prop('id')]: $(this).prop('checked') }
}).get(); }).get();
@ -158,6 +149,26 @@ function save_settings() {
for (let item in box) { settings[item] = box[item]; } for (let item in box) { settings[item] = box[item]; }
} }
settings['exists'] = !settings['exists'] settings['exists'] = !settings['exists']
let found_by = $('#found_by option[selected=selected]').html();
if (found_by && settings['exists'] && settings['found']) {
settings['found_by'] = found_by;
}
let found_on = $('#found_on').val();
let pattern = /(\d{2})\.(\d{2})\.(\d{4})/;
if (!isNaN(found_on) && isNaN(Date.parse(found_on.replace(pattern, '$3-$2-$1')))) {;
settings['found_on'] = get_datetime();
} else {
settings['found_on'] = found_on;
}
let circulation = $('#circulation').val();
if (isNaN(parseInt(circulation)) || circulation < 0 || circulation > 1000000000) {
settings['circulation'] = 0;
} else {
settings['circulation'] = circulation;
}
} }

View File

@ -211,102 +211,57 @@ def response(status=0, message=''):
def add_coin(request): def add_coin(request):
''' add or update a coin
@params request.POST[field]
status = 0 *field* mandatory default data type (internal) with limits
message = '' ------------------------------------------------------------------------------------
value yes - int [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203]
year yes - int [1999; 2098]
country yes - obj Country
stamp no None obj Stamp
exists no False bool
name no - str
found_by no None obj User
found_on no None str %Y-%m-%d
circulation no 0 int [0; 1000000000]
buy_only no False bool
checked no False bool
ec no False bool
'''
# mandatory fields value, year, country
try: try:
value = int(request.POST['value']) value = int(request.POST['value'])
except ValueError: except ValueError:
status = 1 return response(1, f"Der Münzwert '{value}' ist keine gültige Zahl.")
message = f"Der Münzwert '{value}' ist keine gültige Zahl." if value not in [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203]:
return response(status, message) return response(1, f"Der Münzwert '{value}' muss eine Zahl aus der Menge [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203] sein.")
if value not in [1,2,5,10,20,50,100,200,201,202,203]:
status = 1
message = f"Der Münzwert '{value}' muss eine Zahl aus der Menge [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203] sein."
return response(status, message)
try: try:
year = int(request.POST['year']) year = int(request.POST['year'])
except ValueError: except ValueError:
status = 1 return response(1, f"Das Jahr '{year}' ist keine gültige Zahl.")
message = f"Das Jahr '{year}' ist keine gültige Zahl."
return response(status, message)
if year < 1999: if year < 1999:
status = 1 return response(1, f"Das Jahr '{year}' ist kleiner als 1999.")
message = "Das Jahr '{year}' ist kleiner als 1999."
return response(status, message)
if year > 2098: if year > 2098:
status = 1 return response(1, f"Bitte den 2100-Jahrhundert-Datumsbug fixen. :-)")
message = f"Bitte den 2100-Jahrhundert-Datumsbug fixen. :-)"
return response(status, messae)
try: try:
country = Country.objects.get(name_iso=request.POST['country']) country = Country.objects.get(name_iso=request.POST['country'])
except Country.DoesNotExist: except Country.DoesNotExist:
status = 1 return response(1, f"Das Land mit ISO-Kürzel '{name_iso}' existiert nicht.")
message = f"Das Land mit ISO-Kürzel '{name_iso}' existiert nicht."
return response(status, message)
# optional fields stamp, exists, name, found_by, found_on, circulation, buy_only, ec, checked
stamp = request.POST['stamp'] stamp = request.POST['stamp']
try: try:
stamp = Stamp.objects.get(name_short=stamp) stamp = Stamp.objects.get(name_short=stamp)
except Stamp.DoesNotExist: except Stamp.DoesNotExist:
if stamp: if stamp:
status = 1 return response(1, f"Die Prägerei '{stamp}' existiert nicht.")
message = f"Die Prägerei '{stamp}' existiert nicht."
return response(status, message)
stamp = None stamp = None
try:
name = str(request.POST['name'])
except Exception as e:
status = 1
message = f"Der Münzenname '{name}' ist kein gültiger Name. {e}"
return response(status, message)
found_by = request.POST['found_by'] if request.POST['found_by'] else None
if found_by:
try:
found_by = User.objects.get(name=found_by)
except User.DoesNotExist:
status = 1
message = f"Der Nutzer '{found_by}' existiert nicht."
return response(status, message)
found_on = request.POST['found_on'] if request.POST['found_on'] else None
if found_on:
try:
found_on = datetime.strptime(found_on, '%d.%m.%Y')
found_on = found_on.strftime('%Y-%m-%d')
except ValueError:
status = 1
message = f"Das Datum '{found_on}' entspricht nicht dem Format DD.MM.YYYY"
return response(status, message)
else:
found_on = date.today().strftime('%Y-%m-%d')
try:
circulation = int(request.POST['circulation'])
except ValueError:
circulation = 0
# TODO sollte es dann nicht einen Fehler geben?
if circulation != 0:
if circulation < 0:
status = 1
message = f"Die Münzauflage '{circulation}' muss 0 oder positiv sein."
return response(status, message)
elif circulation > 1000000000:
status = 1
message = f"Die Münzauflage '{circulation}' ist unrealistisch."
return response(status, message)
buy_only = True if request.POST['buy_only'] == 'true' else False
checked = True if request.POST['checked'] == 'true' else False
ec = True if request.POST['ec'] == 'true' else False
exists = True if request.POST['exists'] == 'true' else False exists = True if request.POST['exists'] == 'true' else False
if not exists: if not exists:
name = '' name = ''
@ -317,35 +272,73 @@ def add_coin(request):
checked = False checked = False
ec = False ec = False
else:
try:
name = str(request.POST['name'])
except Exception as e:
return response(1, f"Der Münzenname '{name}' ist kein gültiger Name. {e}")
found_by = request.POST['found_by'] if request.POST['found_by'] else None
if found_by:
try:
found_by = User.objects.get(name=found_by)
except User.DoesNotExist:
return response(1, f"Der Nutzer '{found_by}' existiert nicht.")
found_on = request.POST['found_on'] if request.POST['found_on'] else None
if found_on:
try:
found_on = datetime.strptime(found_on, '%d.%m.%Y')
found_on = found_on.strftime('%Y-%m-%d')
except ValueError:
return response(1, f"Das Datum '{found_on}' entspricht nicht dem Format 'DD.MM.YYYY'.")
else:
found_on = date.today().strftime('%Y-%m-%d')
circulation = request.POST['circulation'] if request.POST['circulation'] else 0
try:
circulation = int(circulation)
except ValueError:
return response(1, f"Die Münzauflage '{circulation}' muss eine Zahl sein.")
if circulation < 0:
return response(1, f"Die Münzauflage '{circulation}' muss 0 oder positiv sein.")
elif circulation > 1000000000:
return response(1, f"Die Münzauflage '{circulation}' ist unrealistisch.")
buy_only = True if request.POST['buy_only'] == 'true' else False
checked = True if request.POST['checked'] == 'true' else False
ec = True if request.POST['ec'] == 'true' else False
try: try:
coin = Coin.objects.get( coin = Coin.objects.get(
value=value, value=value,
year=year, year=year,
country=country, country=country,
stamp=stamp) stamp=stamp)
coin.name=name coin.name = name
coin.found_by=found_by coin.found_by = found_by
coin.found_on=found_on coin.found_on = found_on
coin.circulation=circulation coin.circulation = circulation
coin.buy_only=buy_only coin.buy_only = buy_only
coin.checked=checked coin.checked = checked
coin.in_collector=ec coin.in_collector = ec
coin.exists=exists coin.exists = exists
coin.save() coin.save()
except Coin.DoesNotExist: except Coin.DoesNotExist:
Coin.objects.create( Coin.objects.create(
value=value, value = value,
year=year, year = year,
country=country, country = country,
stamp=stamp, stamp = stamp,
name=name, name = name,
found_by=found_by, found_by = found_by,
found_on=found_on, found_on = found_on,
circulation=circulation, circulation = circulation,
buy_only=buy_only, buy_only = buy_only,
checked=checked, checked = checked,
in_collector=ec, in_collector = ec,
exists=exists) exists = exists)
return response() return response()