hope it works
This commit is contained in:
parent
447d93ea63
commit
31219c96ab
@ -18,9 +18,6 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
from hashlib import sha256
|
|
||||||
from random import random
|
|
||||||
|
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.http import Http404
|
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())
|
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 '''
|
''' set and return the datetime of the last coin added '''
|
||||||
try:
|
try:
|
||||||
last_modified = Coin.objects.latest('date_modified').date_modified
|
last_modified = Coin.objects.latest('date_modified').date_modified
|
||||||
|
@ -17,17 +17,19 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.test import TestCase, Client
|
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.views import add_coin
|
||||||
from coinc.helper import get_test_response_status
|
from coinc.helper import get_test_response_status
|
||||||
|
|
||||||
|
|
||||||
class TestCoin(TestCase):
|
class TestCoin(TestCase):
|
||||||
|
|
||||||
''' all tests for the coin model '''
|
''' all tests for the coin model '''
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.test_post_data = {
|
self.test_post_data = {
|
||||||
'value': 1,
|
'value': 1,
|
||||||
@ -38,55 +40,89 @@ class TestCoin(TestCase):
|
|||||||
'name': '',
|
'name': '',
|
||||||
'found_by': '',
|
'found_by': '',
|
||||||
'found_on': '03.05.2040',
|
'found_on': '03.05.2040',
|
||||||
# 'date-added' '':,
|
|
||||||
# 'date-modified':,
|
|
||||||
'circulation': 0,
|
'circulation': 0,
|
||||||
'bux_only': 'false',
|
'buy_only': 'false',
|
||||||
'checked': 'false',
|
'checked': 'false',
|
||||||
'ec': 'false'
|
'ec': 'false',
|
||||||
|
# 'date-added' '':,
|
||||||
|
# 'date-modified':
|
||||||
}
|
}
|
||||||
Country.objects.create(
|
Country.objects.create(
|
||||||
name_iso = 'fo',
|
name_iso = 'fo',
|
||||||
name = 'bar'
|
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 '''
|
''' shortcut: return response status of post request to /coinc/add/coin '''
|
||||||
|
|
||||||
test_data = self.test_post_data
|
test_data = deepcopy(self.test_post_data)
|
||||||
|
if data:
|
||||||
test_data[field] = data
|
test_data[field] = data
|
||||||
test_response = self.client.post(f'/coinc/add/coin', test_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)
|
return get_test_response_status(test_response)
|
||||||
|
|
||||||
|
|
||||||
def test_view_add_coin_missing_post_values(self):
|
def test_view_add_coin(self):
|
||||||
|
|
||||||
# all values right
|
|
||||||
self.assertEqual(self._return_status_wrong_field('value', '1'), 0)
|
|
||||||
|
|
||||||
|
## value
|
||||||
|
# value missing
|
||||||
|
# self.assertEqual(self._return_status_wrong_field('value', None), 1)
|
||||||
# value not a number
|
# value not a number
|
||||||
self.assertEqual(self._return_status_wrong_field('value', ''), 1)
|
# self.assertEqual(self._return_status_wrong_field('value', ''), 1)
|
||||||
# value not the right number
|
# self.assertEqual(self._return_status_wrong_field('value', 'some string'), 1)
|
||||||
self.assertEqual(self._return_status_wrong_field('value', 3), 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
|
## year
|
||||||
self.assertEqual(self._return_status_wrong_field('year', ''), 1)
|
# 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
|
# 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
|
# 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
|
## country
|
||||||
self.assertEqual(self._return_status_wrong_field('country', 12235235), 1)
|
# country missing
|
||||||
self.assertEqual(self._return_status_wrong_field('country', 'toolong'), 1)
|
# 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
|
# country does not exist
|
||||||
self.assertEqual(self._return_status_wrong_field('country', 'xx'), 1)
|
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)
|
## stamp
|
||||||
self.assertEqual(self._return_status_wrong_field('stamp', 'longer than 10 characters'), 1)
|
# 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)
|
self.assertEqual(self._return_status_wrong_field('stamp', 'xx'), 1)
|
||||||
|
|
||||||
|
## exists
|
||||||
def test_views_add_without_year(self):
|
# missing exists
|
||||||
pass
|
self.assertEqual(self._return_status_wrong_field('exists', None), 0)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
#
|
#
|
||||||
# Copyright (C) 2020 willipink.eu
|
# Copyright (C) 2020-21 willipink.eu
|
||||||
# Author Moritz Münch moritzmuench@mailbox.org
|
# Author Moritz Münch moritzmuench@mailbox.org
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# 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 django.views.decorators.cache import cache_page
|
||||||
|
|
||||||
from . import views
|
from . import views
|
||||||
@ -27,10 +27,11 @@ from . import views
|
|||||||
app_name = 'coinc'
|
app_name = 'coinc'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', cache_page(None)(views.index), name='index'),
|
path('', cache_page(None)(views.index), name='index'),
|
||||||
# path('', views.index, name='index'),
|
|
||||||
path('<str:name_iso>', cache_page(None)(views.detail_country), name='country'),
|
path('<str:name_iso>', cache_page(None)(views.detail_country), name='country'),
|
||||||
# path('<str:name_iso>', views.detail_country, name='country'),
|
|
||||||
path('add/user/<str:username>/color/<str:color>', views.add_user, name='add_user'),
|
path('add/user/<str:username>/color/<str:color>', views.add_user, name='add_user'),
|
||||||
path('add/coin', views.add_coin, name='add_coin'),
|
re_path(r'^add/coin/(?P<country>[a-z]{2})/(?P<year>(?:1999|20[0-8][0-9]|209[0-8]))/(?P<value>(?:1|2|5|10|20|50|100|200|201|202|203))$',
|
||||||
|
views.add_coin, name='add_coin'),
|
||||||
|
path('edit/coin/<str:country>/<int:year>/<int:value>',
|
||||||
|
views.edit_coin, name='edit_coin')
|
||||||
path('accounts/', include('django.contrib.auth.urls')),
|
path('accounts/', include('django.contrib.auth.urls')),
|
||||||
]
|
]
|
||||||
|
@ -26,6 +26,7 @@ from django.http import HttpResponse, Http404
|
|||||||
from django.template import loader
|
from django.template import loader
|
||||||
from django.template.defaultfilters import register
|
from django.template.defaultfilters import register
|
||||||
from django.views.decorators.http import condition
|
from django.views.decorators.http import condition
|
||||||
|
from django.utils.datastructures import MultiValueDictKeyError
|
||||||
|
|
||||||
from .cache import (get_last_modified_coin, set_last_modified_coin,
|
from .cache import (get_last_modified_coin, set_last_modified_coin,
|
||||||
get_last_modified_country, set_last_modified_country)
|
get_last_modified_country, set_last_modified_country)
|
||||||
@ -242,7 +243,11 @@ def add_user(request, username, color):
|
|||||||
return response()
|
return response()
|
||||||
|
|
||||||
|
|
||||||
def add_coin(request):
|
def edit_coin(request):
|
||||||
|
return response()
|
||||||
|
|
||||||
|
|
||||||
|
def add_coin(request, country, year, value):
|
||||||
''' add or update a coin
|
''' add or update a coin
|
||||||
@params request.POST[field]
|
@params request.POST[field]
|
||||||
|
|
||||||
@ -266,44 +271,27 @@ def add_coin(request):
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
# mandatory fields value, year, country
|
# mandatory fields value, year, country
|
||||||
value = request.POST['value']
|
|
||||||
try:
|
|
||||||
value = int(value)
|
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)
|
year = int(year)
|
||||||
except ValueError:
|
country = str(country)
|
||||||
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")
|
|
||||||
try:
|
try:
|
||||||
country = Country.objects.get(name_iso=country)
|
country = Country.objects.get(name_iso=country)
|
||||||
except Country.DoesNotExist:
|
except Country.DoesNotExist:
|
||||||
return response(1, f"Das Land mit ISO-Kürzel '{country}' existiert nicht")
|
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
|
# optional fields stamp, exists, name, found_by, found_on, circulation, buy_only, ec, checked
|
||||||
stamp = request.POST['stamp']
|
stamp = request.POST.get('stamp')
|
||||||
if len(stamp > 10):
|
stamp = str(stamp) if stamp else None
|
||||||
return response(1, f'Die Prägerei darf max. 10 Zeichen lang sein'
|
if stamp:
|
||||||
|
if len(stamp) > 10:
|
||||||
|
return response(1, f'Die Prägerei darf max. 10 Zeichen lang sein')
|
||||||
try:
|
try:
|
||||||
stamp = Stamp.objects.get(name_short=stamp)
|
stamp = Stamp.objects.get(name_short=stamp)
|
||||||
except Stamp.DoesNotExist:
|
except Stamp.DoesNotExist:
|
||||||
if stamp:
|
|
||||||
return response(1, f"Die Prägerei '{stamp}' existiert nicht.")
|
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:
|
if not exists:
|
||||||
name = ''
|
name = ''
|
||||||
found_by = None
|
found_by = None
|
||||||
@ -314,20 +302,19 @@ def add_coin(request):
|
|||||||
ec = False
|
ec = False
|
||||||
|
|
||||||
else:
|
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:
|
if found_by:
|
||||||
try:
|
try:
|
||||||
found_by = User.objects.get(name=found_by)
|
found_by = User.objects.get(name=found_by)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
return response(1, f"Der Nutzer '{found_by}' existiert nicht.")
|
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:
|
if found_on:
|
||||||
try:
|
try:
|
||||||
found_on = datetime.strptime(found_on, '%d.%m.%Y')
|
found_on = datetime.strptime(found_on, '%d.%m.%Y')
|
||||||
@ -337,7 +324,8 @@ def add_coin(request):
|
|||||||
else:
|
else:
|
||||||
found_on = date.today().strftime('%Y-%m-%d')
|
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:
|
try:
|
||||||
circulation = int(circulation.replace('.', ''))
|
circulation = int(circulation.replace('.', ''))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@ -347,9 +335,9 @@ def add_coin(request):
|
|||||||
elif circulation > 1000000000:
|
elif circulation > 1000000000:
|
||||||
return response(1, f"Die Münzauflage '{circulation}' ist unrealistisch.")
|
return response(1, f"Die Münzauflage '{circulation}' ist unrealistisch.")
|
||||||
|
|
||||||
buy_only = True if request.POST['buy_only'] == 'true' else False
|
buy_only = True if request.POST.get('buy_only') == 'true' else False
|
||||||
checked = True if request.POST['checked'] == 'true' else False
|
checked = True if request.POST.get('checked') == 'true' else False
|
||||||
ec = True if request.POST['ec'] == 'true' else False
|
ec = True if request.POST.get('ec') == 'true' else False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
coin = Coin.objects.get(
|
coin = Coin.objects.get(
|
||||||
@ -382,7 +370,7 @@ def add_coin(request):
|
|||||||
in_collector = ec,
|
in_collector = ec,
|
||||||
exists = exists)
|
exists = exists)
|
||||||
|
|
||||||
set_last_modified_coin()
|
set_last_modified_index(country.name_iso)
|
||||||
set_last_modified_country(country.name_iso)
|
set_last_modified_country(country.name_iso)
|
||||||
return response()
|
return response()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user