From 31219c96ab16cce8ceb57242874a6be529e04089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?moritz=20m=C3=BCnch?= Date: Mon, 4 Oct 2021 13:16:04 +0200 Subject: [PATCH] hope it works --- coinmanager/coinc/cache.py | 5 +- coinmanager/coinc/test_coin.py | 96 +++++++++++++++++++++++----------- coinmanager/coinc/urls.py | 19 +++---- coinmanager/coinc/views.py | 74 +++++++++++--------------- 4 files changed, 108 insertions(+), 86 deletions(-) diff --git a/coinmanager/coinc/cache.py b/coinmanager/coinc/cache.py index 7c2fd49..62142dd 100644 --- a/coinmanager/coinc/cache.py +++ b/coinmanager/coinc/cache.py @@ -18,9 +18,6 @@ -from hashlib import sha256 -from random import random - from django.core.cache import cache from django.http import Http404 @@ -31,7 +28,7 @@ def get_last_modified_coin(request): return cache.get('last_modified_coin', set_last_modified_coin()) -def set_last_modified_coin(): +def set_last_modified_index(name_iso): ''' set and return the datetime of the last coin added ''' try: last_modified = Coin.objects.latest('date_modified').date_modified diff --git a/coinmanager/coinc/test_coin.py b/coinmanager/coinc/test_coin.py index 1f3e4bd..56ec8c8 100644 --- a/coinmanager/coinc/test_coin.py +++ b/coinmanager/coinc/test_coin.py @@ -17,17 +17,19 @@ # along with this program. If not, see . +from copy import deepcopy + from django.http import HttpResponse from django.test import TestCase, Client -from coinc.models import Coin, Country +from coinc.models import Coin, Country, Stamp from coinc.views import add_coin from coinc.helper import get_test_response_status class TestCoin(TestCase): - ''' all tests for the coin model ''' + def setUp(self): self.test_post_data = { 'value': 1, @@ -38,55 +40,89 @@ class TestCoin(TestCase): 'name': '', 'found_by': '', 'found_on': '03.05.2040', - # 'date-added' '':, - # 'date-modified':, 'circulation': 0, - 'bux_only': 'false', + 'buy_only': 'false', 'checked': 'false', - 'ec': 'false' + 'ec': 'false', +# 'date-added' '':, +# 'date-modified': } Country.objects.create( name_iso = 'fo', name = 'bar' ) + Stamp.objects.create( + name_short = '10 letters', + name = 'test name' + ) - def _return_status_wrong_field(self, field, data): + + def _return_status_wrong_field(self, field, data=None): ''' shortcut: return response status of post request to /coinc/add/coin ''' - test_data = self.test_post_data - test_data[field] = data - test_response = self.client.post(f'/coinc/add/coin', test_data) + test_data = deepcopy(self.test_post_data) + if data: + test_data[field] = data + else: + try: + test_data.pop(field) + except KeyError: + pass + test_response = self.client.post( + f"/coinc/add/coin/{test_data['country']}/{test_data['year']}/{test_data['value']}", + test_data) return get_test_response_status(test_response) - def test_view_add_coin_missing_post_values(self): - - # all values right - self.assertEqual(self._return_status_wrong_field('value', '1'), 0) + def test_view_add_coin(self): + ## value + # value missing +# self.assertEqual(self._return_status_wrong_field('value', None), 1) # value not a number - self.assertEqual(self._return_status_wrong_field('value', ''), 1) - # value not the right number - self.assertEqual(self._return_status_wrong_field('value', 3), 1) +# self.assertEqual(self._return_status_wrong_field('value', ''), 1) +# self.assertEqual(self._return_status_wrong_field('value', 'some string'), 1) + # value wrong number +# self.assertEqual(self._return_status_wrong_field('value', 3), 1) +# self.assertEqual(self._return_status_wrong_field('value', 0.02), 1) +# self.assertEqual(self._return_status_wrong_field('value', -3), 1) + # value ok + for value in [1, 2, 5, 10, 20, 50, 100, 200, 201, 202, 203]: + self.assertEqual(self._return_status_wrong_field('value', value), 0) - # year not a number - self.assertEqual(self._return_status_wrong_field('year', ''), 1) + ## year + # year missing +# self.assertEqual(self._return_status_wrong_field('year', None), 1) + # year not a number self.assertEqual(self._return_status_wrong_field('year', ''), 1) # year lower limit - self.assertEqual(self._return_status_wrong_field('year', 1998), 1) +# self.assertEqual(self._return_status_wrong_field('year', 1998), 1) # year upper limit - self.assertEqual(self._return_status_wrong_field('year', 2099), 1) +# self.assertEqual(self._return_status_wrong_field('year', 2099), 1) + # year ok + for year in range(1999, 2098): + self.assertEqual(self._return_status_wrong_field('year', year), 0) - # country not valid format xx - self.assertEqual(self._return_status_wrong_field('country', 12235235), 1) - self.assertEqual(self._return_status_wrong_field('country', 'toolong'), 1) + ## country + # country missing +# self.assertEqual(self._return_status_wrong_field('country', None), 1) + # country not valid iso format +# self.assertEqual(self._return_status_wrong_field('country', 12235235), 1) +# self.assertEqual(self._return_status_wrong_field('country', 'toolong'), 1) # country does not exist self.assertEqual(self._return_status_wrong_field('country', 'xx'), 1) + # country ok + self.assertEqual(self._return_status_wrong_field('country', 'fo'), 0) - #stamp does not exist - self.assertEqual(self._return_status_wrong_field('stamp', 'xx'), 1) - self.assertEqual(self._return_status_wrong_field('stamp', 'longer than 10 characters'), 1) + + ## stamp + # stamp missing + self.assertEqual(self._return_status_wrong_field('stamp', None), 0) + # stamp title length + self.assertEqual(self._return_status_wrong_field('stamp', '10 letters'), 0) + self.assertEqual(self._return_status_wrong_field('stamp', '11 letters '), 1) + # stamp does not exist self.assertEqual(self._return_status_wrong_field('stamp', 'xx'), 1) - - def test_views_add_without_year(self): - pass + ## exists + # missing exists + self.assertEqual(self._return_status_wrong_field('exists', None), 0) diff --git a/coinmanager/coinc/urls.py b/coinmanager/coinc/urls.py index e7d25b6..b8f5829 100644 --- a/coinmanager/coinc/urls.py +++ b/coinmanager/coinc/urls.py @@ -1,6 +1,6 @@ # encoding: utf-8 # -# Copyright (C) 2020 willipink.eu +# Copyright (C) 2020-21 willipink.eu # Author Moritz Münch moritzmuench@mailbox.org # # This program is free software: you can redistribute it and/or modify @@ -18,7 +18,7 @@ -from django.urls import path, include +from django.urls import path, re_path, include from django.views.decorators.cache import cache_page from . import views @@ -26,11 +26,12 @@ from . import views app_name = 'coinc' urlpatterns = [ - path('', cache_page(None)(views.index), name='index'), -# path('', views.index, name='index'), - path('', cache_page(None)(views.detail_country), name='country'), -# path('', views.detail_country, name='country'), - path('add/user//color/', views.add_user, name='add_user'), - path('add/coin', views.add_coin, name='add_coin'), - path('accounts/', include('django.contrib.auth.urls')), + path('', cache_page(None)(views.index), name='index'), + path('', cache_page(None)(views.detail_country), name='country'), + path('add/user//color/', views.add_user, name='add_user'), + re_path(r'^add/coin/(?P[a-z]{2})/(?P(?:1999|20[0-8][0-9]|209[0-8]))/(?P(?:1|2|5|10|20|50|100|200|201|202|203))$', + views.add_coin, name='add_coin'), + path('edit/coin///', + views.edit_coin, name='edit_coin') + path('accounts/', include('django.contrib.auth.urls')), ] diff --git a/coinmanager/coinc/views.py b/coinmanager/coinc/views.py index 167c9a9..05b4d2c 100644 --- a/coinmanager/coinc/views.py +++ b/coinmanager/coinc/views.py @@ -26,6 +26,7 @@ from django.http import HttpResponse, Http404 from django.template import loader from django.template.defaultfilters import register from django.views.decorators.http import condition +from django.utils.datastructures import MultiValueDictKeyError from .cache import (get_last_modified_coin, set_last_modified_coin, get_last_modified_country, set_last_modified_country) @@ -242,7 +243,11 @@ def add_user(request, username, color): return response() -def add_coin(request): +def edit_coin(request): + return response() + + +def add_coin(request, country, year, value): ''' add or update a coin @params request.POST[field] @@ -266,44 +271,27 @@ def add_coin(request): ''' # mandatory fields value, year, country - value = request.POST['value'] - try: - value = int(value) - except ValueError: - 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.") - - year = request.POST['year'] - try: - year = int(year) - except ValueError: - return response(1, f"Das Jahr ist keine gültige Zahl, Format: 1234") - if year < 1999: - return response(1, f"Das Jahr '{year}' ist kleiner als 1999") - if year > 2098: - return response(1, f"Bitte den 2100-Jahrhundert-Datumsbug fixen. :-)") - - country = request.POST['country'] - if not country.isalpha() or len(country) != 2: - return response(1, f"Das Land ist kein gültiges ISO-Format: XX") + value = int(value) + year = int(year) + country = str(country) try: country = Country.objects.get(name_iso=country) except Country.DoesNotExist: return response(1, f"Das Land mit ISO-Kürzel '{country}' existiert nicht") # optional fields stamp, exists, name, found_by, found_on, circulation, buy_only, ec, checked - stamp = request.POST['stamp'] - if len(stamp > 10): - return response(1, f'Die Prägerei darf max. 10 Zeichen lang sein' - try: - stamp = Stamp.objects.get(name_short=stamp) - except Stamp.DoesNotExist: - if stamp: + stamp = request.POST.get('stamp') + stamp = str(stamp) if stamp else None + if stamp: + if len(stamp) > 10: + return response(1, f'Die Prägerei darf max. 10 Zeichen lang sein') + try: + stamp = Stamp.objects.get(name_short=stamp) + except Stamp.DoesNotExist: return response(1, f"Die Prägerei '{stamp}' existiert nicht.") - stamp = None - exists = True if request.POST['exists'] == 'true' else False + exists = request.POST.get('exists', False) + exists = True if exists == 'true' else False if not exists: name = '' found_by = None @@ -314,20 +302,19 @@ def add_coin(request): ec = False else: - name = request.POST.get('name', '') - try: - name = str(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 + name = request.POST.get('name') + name = str(name) if name else None + + found_by = request.POST.get('found_by') + found_by = str(found_by) if 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 + found_on = request.POST.get('found_on') if found_on: try: found_on = datetime.strptime(found_on, '%d.%m.%Y') @@ -337,7 +324,8 @@ def add_coin(request): else: found_on = date.today().strftime('%Y-%m-%d') - circulation = request.POST['circulation'] if request.POST['circulation'] else 0 + circulation = request.POST.get('circulation', 0) + circulation = 0 if circulation == '' else circulation try: circulation = int(circulation.replace('.', '')) except ValueError: @@ -347,9 +335,9 @@ def add_coin(request): 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 + buy_only = True if request.POST.get('buy_only') == 'true' else False + checked = True if request.POST.get('checked') == 'true' else False + ec = True if request.POST.get('ec') == 'true' else False try: coin = Coin.objects.get( @@ -382,7 +370,7 @@ def add_coin(request): in_collector = ec, exists = exists) - set_last_modified_coin() + set_last_modified_index(country.name_iso) set_last_modified_country(country.name_iso) return response()