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.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
|
||||
|
@ -17,17 +17,19 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
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 = deepcopy(self.test_post_data)
|
||||
if 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)
|
||||
|
||||
|
||||
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)
|
||||
|
@ -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
|
||||
@ -27,10 +27,11 @@ from . import views
|
||||
app_name = 'coinc'
|
||||
urlpatterns = [
|
||||
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>', views.detail_country, name='country'),
|
||||
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')),
|
||||
]
|
||||
|
@ -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")
|
||||
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'
|
||||
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:
|
||||
if stamp:
|
||||
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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user