From 31137d33e8f64a745e8632d2da4dd41d0c72e6ed Mon Sep 17 00:00:00 2001 From: koksnuss Date: Mon, 11 May 2020 14:01:15 +0200 Subject: [PATCH] fix #14 --- coinmanager/coinc/static/coinc/main.js | 35 +++-- coinmanager/coinc/views.py | 193 ++++++++++++------------- 2 files changed, 116 insertions(+), 112 deletions(-) diff --git a/coinmanager/coinc/static/coinc/main.js b/coinmanager/coinc/static/coinc/main.js index 7137d15..f9a4b5b 100644 --- a/coinmanager/coinc/static/coinc/main.js +++ b/coinmanager/coinc/static/coinc/main.js @@ -23,7 +23,7 @@ let settings = { found: true, found_by: '', found_on: '', - circulation: '', + circulation: 0, buy_only: false, checked: false, ec: false, @@ -77,7 +77,7 @@ function render_login_color(color) { // show/update #found_by and #found_on function render_found() { let checked = $('#found').prop('checked'); - if (checked) { + if (checked === true) { $('#found_by option[selected=selected]').html(settings['found_by']); let found_on = '' if (settings['found_on']) { @@ -93,7 +93,7 @@ function render_found() { } $('#found_by, #found_on').each(function() { - if (checked) { + if (checked === true) { $(this).prop('disabled', ''); } else { $(this).prop('disabled', 'disabled'); @@ -142,15 +142,6 @@ function render_response(td, set) { // save/update global 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() { return { [$(this).prop('id')]: $(this).prop('checked') } }).get(); @@ -158,6 +149,26 @@ function save_settings() { for (let item in box) { settings[item] = box[item]; } } 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; + } } diff --git a/coinmanager/coinc/views.py b/coinmanager/coinc/views.py index f7cf5e6..0f69f8f 100644 --- a/coinmanager/coinc/views.py +++ b/coinmanager/coinc/views.py @@ -211,111 +211,103 @@ def response(status=0, message=''): def add_coin(request): + ''' add or update a coin + @params request.POST[field] - status = 0 - message = '' + *field* mandatory default data type (internal) with limits + ------------------------------------------------------------------------------------ + 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: value = int(request.POST['value']) except ValueError: - status = 1 - message = f"Der Münzwert '{value}' ist keine gültige Zahl." - return response(status, message) - 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) + return response(1, 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(1, f"Der Münzwert '{value}' muss eine Zahl aus der Menge [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203] sein.") try: year = int(request.POST['year']) except ValueError: - status = 1 - message = f"Das Jahr '{year}' ist keine gültige Zahl." - return response(status, message) + return response(1, f"Das Jahr '{year}' ist keine gültige Zahl.") if year < 1999: - status = 1 - message = "Das Jahr '{year}' ist kleiner als 1999." - return response(status, message) + return response(1, f"Das Jahr '{year}' ist kleiner als 1999.") if year > 2098: - status = 1 - message = f"Bitte den 2100-Jahrhundert-Datumsbug fixen. :-)" - return response(status, messae) + return response(1, f"Bitte den 2100-Jahrhundert-Datumsbug fixen. :-)") try: country = Country.objects.get(name_iso=request.POST['country']) except Country.DoesNotExist: - status = 1 - message = f"Das Land mit ISO-Kürzel '{name_iso}' existiert nicht." - return response(status, message) + return response(1, f"Das Land mit ISO-Kürzel '{name_iso}' existiert nicht.") + # optional fields stamp, exists, name, found_by, found_on, circulation, buy_only, ec, checked stamp = request.POST['stamp'] try: stamp = Stamp.objects.get(name_short=stamp) except Stamp.DoesNotExist: if stamp: - status = 1 - message = f"Die Prägerei '{stamp}' existiert nicht." - return response(status, message) + return response(1, f"Die Prägerei '{stamp}' existiert nicht.") 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: - name = '' - found_by = None - found_on = date.today().strftime('%Y-%m-%d') + name = '' + found_by = None + found_on = date.today().strftime('%Y-%m-%d') circulation = 0 - buy_only = False - checked = False - ec = False + buy_only = False + checked = 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: coin = Coin.objects.get( @@ -323,29 +315,30 @@ def add_coin(request): year=year, country=country, stamp=stamp) - coin.name=name - coin.found_by=found_by - coin.found_on=found_on - coin.circulation=circulation - coin.buy_only=buy_only - coin.checked=checked - coin.in_collector=ec - coin.exists=exists + coin.name = name + coin.found_by = found_by + coin.found_on = found_on + coin.circulation = circulation + coin.buy_only = buy_only + coin.checked = checked + coin.in_collector = ec + coin.exists = exists coin.save() + except Coin.DoesNotExist: Coin.objects.create( - value=value, - year=year, - country=country, - stamp=stamp, - name=name, - found_by=found_by, - found_on=found_on, - circulation=circulation, - buy_only=buy_only, - checked=checked, - in_collector=ec, - exists=exists) + value = value, + year = year, + country = country, + stamp = stamp, + name = name, + found_by = found_by, + found_on = found_on, + circulation = circulation, + buy_only = buy_only, + checked = checked, + in_collector = ec, + exists = exists) return response()