diff --git a/.gitignore b/.gitignore index 9f11b75..2483976 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea/ +__pycache__/ diff --git a/Readme.md b/Readme.md index 5a61563..55d0c32 100644 --- a/Readme.md +++ b/Readme.md @@ -2,3 +2,9 @@ Команда для запуска: python3 manage.py runserver (в консоли находясь в папке, в которой находится manage.py) + +Сайт отображает валюты на определенный период(используются данный центробанка) + +Для запуска тестов: + python3 manage.py test Wallet +(тест на корректность добавления в базу данных и на правильное удаление, второй тест на правильную работу загрузки данных) diff --git a/WebPython/Apps/Wallet/ClearRepeatedDate.py b/WebPython/Apps/Wallet/ClearRepeatedDate.py new file mode 100644 index 0000000..ea815fe --- /dev/null +++ b/WebPython/Apps/Wallet/ClearRepeatedDate.py @@ -0,0 +1,20 @@ +from .models import Wallet_value, Wallet_indentificator + + +def clear_repeated_date(): + wallets = Wallet_indentificator.objects.all() + for wallet in wallets: + list_to_delete = [] + print(wallet.wallet_name) + values_list = Wallet_value.objects.filter(wallet=wallet) + print(len(values_list)) + for i in range(len(values_list)): + for j in range(i + 1, len(values_list)): + if values_list[i].date == values_list[j].date and i != j: + list_to_delete += [values_list[j]] + print(values_list[j].date, i, j) + for i in list_to_delete: + try: + i.delete() + except: + pass diff --git a/WebPython/Apps/Wallet/__pycache__/apps.cpython-36.pyc b/WebPython/Apps/Wallet/__pycache__/apps.cpython-36.pyc index 071fd22..8bf1c46 100644 Binary files a/WebPython/Apps/Wallet/__pycache__/apps.cpython-36.pyc and b/WebPython/Apps/Wallet/__pycache__/apps.cpython-36.pyc differ diff --git a/WebPython/Apps/Wallet/__pycache__/load_data.cpython-36.pyc b/WebPython/Apps/Wallet/__pycache__/load_data.cpython-36.pyc index 9152033..14b4616 100644 Binary files a/WebPython/Apps/Wallet/__pycache__/load_data.cpython-36.pyc and b/WebPython/Apps/Wallet/__pycache__/load_data.cpython-36.pyc differ diff --git a/WebPython/Apps/Wallet/__pycache__/models.cpython-36.pyc b/WebPython/Apps/Wallet/__pycache__/models.cpython-36.pyc index f2a7aea..ffa4313 100644 Binary files a/WebPython/Apps/Wallet/__pycache__/models.cpython-36.pyc and b/WebPython/Apps/Wallet/__pycache__/models.cpython-36.pyc differ diff --git a/WebPython/Apps/Wallet/__pycache__/urls.cpython-36.pyc b/WebPython/Apps/Wallet/__pycache__/urls.cpython-36.pyc index d1232dd..bdb7853 100644 Binary files a/WebPython/Apps/Wallet/__pycache__/urls.cpython-36.pyc and b/WebPython/Apps/Wallet/__pycache__/urls.cpython-36.pyc differ diff --git a/WebPython/Apps/Wallet/__pycache__/views.cpython-36.pyc b/WebPython/Apps/Wallet/__pycache__/views.cpython-36.pyc index 4899ca6..d0451ed 100644 Binary files a/WebPython/Apps/Wallet/__pycache__/views.cpython-36.pyc and b/WebPython/Apps/Wallet/__pycache__/views.cpython-36.pyc differ diff --git a/WebPython/Apps/Wallet/apps.pyc b/WebPython/Apps/Wallet/apps.pyc new file mode 100644 index 0000000..f07d8b3 Binary files /dev/null and b/WebPython/Apps/Wallet/apps.pyc differ diff --git a/WebPython/Apps/Wallet/load_data.py b/WebPython/Apps/Wallet/load_data.py index 9108a8c..c22bce3 100644 --- a/WebPython/Apps/Wallet/load_data.py +++ b/WebPython/Apps/Wallet/load_data.py @@ -5,17 +5,23 @@ def load_data(date_): - print("load data") req = requests.get("http://www.cbr.ru/scripts/XML_daily.asp?date_req=" + date_) soup = BeautifulSoup(req.content, 'lxml') - wallets = Wallet_indentificator.objects.all() if date_.split('/') == str(soup.find('valcurs')['date']).split('.'): ids = soup.find_all('valute') for i in ids: - print(i) - wallet = wallets[ids.index(i)] - print(wallet.wallet_name) + try: + wallet = Wallet_indentificator.objects.get( + wallet_name=i.find('name').text + ) + except: + wallet = Wallet_indentificator( + wallet_name=i.find('name').text, + wallet_id=i['id'], + wallet_char_code=i.find('charcode').text, + ) + wallet.save() data = Wallet_value( wallet=wallet, wallet_nominal=int(i.find('nominal').text), @@ -23,8 +29,6 @@ def load_data(date_): date='-'.join(soup.find('valcurs')['date'].split('.')[::-1]) ) data.save() - print(data.wallet_nominal) - print(data.wallet_value) else: raise ValueError diff --git a/WebPython/Apps/Wallet/tests.py b/WebPython/Apps/Wallet/tests.py index 7ce503c..32c2317 100644 --- a/WebPython/Apps/Wallet/tests.py +++ b/WebPython/Apps/Wallet/tests.py @@ -1,3 +1,140 @@ +from __future__ import absolute_import + +import requests +from bs4 import BeautifulSoup from django.test import TestCase +from .load_data import load_data +from .models import Wallet_value, Wallet_indentificator +from .ClearRepeatedDate import clear_repeated_date + + # Create your tests here. +class TestModelCreating(TestCase): + def setUp(self) -> None: + example = Wallet_indentificator( + wallet_name="Testing", + wallet_id="TEST_ID", + wallet_char_code="TestChar" + ) + example.save() + example_value = Wallet_value( + wallet=example, + wallet_nominal=1, + wallet_value=3.3, + date="2020-05-05" + ) + example_value.save() + example_value = Wallet_value( + wallet=example, + wallet_nominal=2, + wallet_value=5.3, + date="2021-05-05" + ) + example_value.save() + + def test_correct_addition(self): + wallet = Wallet_indentificator.objects.get(wallet_name="Testing") + wallet_array = Wallet_indentificator.objects.filter( + wallet_name="Testing") + self.assertEqual(len(wallet_array), 1) + self.assertEqual(wallet.wallet_name, "Testing") + self.assertEqual(wallet.wallet_id, "TEST_ID") + self.assertEqual(wallet.wallet_char_code, "TestChar") + wallet_value = Wallet_value.objects.filter( + wallet=wallet) + self.assertEqual(len(wallet_value), 2) + Wallet_indentificator.objects.filter( + wallet_name="Testing").delete() + wallet_array = Wallet_indentificator.objects.filter( + wallet_name="Testing") + self.assertEqual(len(wallet_array), 0) + wallet_value = Wallet_value.objects.filter( + wallet=wallet) + self.assertEqual(len(wallet_value), 0) + + +class TestSomeData(TestCase): + def setUp(self) -> None: + load_data("01/05/2020") + wallet = Wallet_indentificator.objects.all()[0] + wallet_value_list = Wallet_value.objects.filter(wallet=wallet) + wallet_value = wallet_value_list[0] + self.date = (str(wallet_value.date)) + date = '/'.join(self.date.split('-')[::-1]) + req = requests.get( + "http://www.cbr.ru/scripts/XML_daily.asp?date_req=" + date + ) + soup = BeautifulSoup(req.content, 'lxml') + self.valutes = soup.find_all('valute') + + def test_date(self): + for valute in self.valutes: + wallet = Wallet_indentificator.objects.get( + wallet_name=valute.find('name').text + ) + wallet_value = Wallet_value.objects.get(wallet=wallet, + date=self.date) + self.assertEqual( + wallet_value.wallet_value, + float('.'.join(valute.find('value').text.split(',')))) + self.assertEqual(wallet_value.wallet_nominal, + int(valute.find('nominal').text)) + + +class TestReapeatedDateCleaner(TestCase): + def setUp(self) -> None: + example = Wallet_indentificator( + wallet_name="Testing", + wallet_id="TEST_ID", + wallet_char_code="TestChar" + ) + example.save() + example_value = Wallet_value( + wallet=example, + wallet_nominal=2, + wallet_value=5.3, + date="2020-05-05" + ) + example_value.save() + example_value = Wallet_value( + wallet=example, + wallet_nominal=2, + wallet_value=5.3, + date="2020-05-05" + ) + example_value.save() + example = Wallet_indentificator( + wallet_name="Testing2", + wallet_id="TEST_ID", + wallet_char_code="TestChar" + ) + example.save() + example_value = Wallet_value( + wallet=example, + wallet_nominal=3, + wallet_value=5.3, + date="2020-05-05" + ) + example_value.save() + example_value = Wallet_value( + wallet=example, + wallet_nominal=3, + wallet_value=5.3, + date="2020-05-05" + ) + example_value.save() + example_value = Wallet_value( + wallet=example, + wallet_nominal=3, + wallet_value=5.2, + date="2020-05-05" + ) + example_value.save() + + def test_removing(self): + clear_repeated_date() + first = Wallet_indentificator.objects.get(id=1) + second = Wallet_indentificator.objects.get(id=2) + self.assertEqual(len(Wallet_value.objects.filter(wallet=first)), 1) + self.assertEqual(len(Wallet_value.objects.filter(wallet=second)), 1) diff --git a/WebPython/Apps/Wallet/urls.py b/WebPython/Apps/Wallet/urls.py index a109406..487c82b 100644 --- a/WebPython/Apps/Wallet/urls.py +++ b/WebPython/Apps/Wallet/urls.py @@ -5,13 +5,13 @@ app_name = "Wallet" urlpatterns = [ - path('main//', views.get_date, + path('/', views.get_date, name='get_date'), - path('main/', views.main_page, name='main_page'), - path('main//show_wallet/', + path('', views.main_page, name='main_page'), + path('/show_wallet/', views.show_wallet, name='show_wallet'), - path('main///show_wallet/', + path('//show_wallet/', views.show_wallet, name='show_wallet') ] diff --git a/WebPython/Apps/Wallet/views.py b/WebPython/Apps/Wallet/views.py index 1e3cf5c..70dddbf 100644 --- a/WebPython/Apps/Wallet/views.py +++ b/WebPython/Apps/Wallet/views.py @@ -1,15 +1,24 @@ from django.shortcuts import render -from django.http import HttpResponse, Http404 +from django.http import HttpResponse from .load_data import load_data, load_name from .models import Wallet_indentificator, Wallet_value +from .ClearRepeatedDate import clear_repeated_date def get_date(request, wallet_name_): print('get_date') - wallet = Wallet_indentificator.objects.get(wallet_name=wallet_name_) - wallet_values = Wallet_value.objects.filter(wallet=wallet).order_by( - '-date') + try: + wallet = Wallet_indentificator.objects.get(wallet_name=wallet_name_) + wallet_values = Wallet_value.objects.filter(wallet=wallet).order_by( + '-date') + except: + clear_repeated_date() + wallet = Wallet_indentificator.objects.get(wallet_name=wallet_name_) + wallet_values = Wallet_value.objects.filter(wallet=wallet).order_by( + '-date') + if len(wallet_values) > 10: + wallet_values = wallet_values[:10] dates = [] for i in wallet_values: dates += [str(i.date)] @@ -43,10 +52,9 @@ def show_wallet(request, wallet_name_, wallet_date_=None): return add_date(request, wallet_name_) - def main_page(request): wallet_names = [] - wallets_list = Wallet_indentificator.objects.all() + wallets_list = Wallet_indentificator.objects.all().order_by('wallet_name') if len(wallets_list) == 0: load_name() wallets_list = Wallet_indentificator.objects.all() diff --git a/WebPython/__pycache__/__init__.cpython-36.pyc b/WebPython/__pycache__/__init__.cpython-36.pyc deleted file mode 100644 index e457c76..0000000 Binary files a/WebPython/__pycache__/__init__.cpython-36.pyc and /dev/null differ diff --git a/WebPython/__pycache__/settings.cpython-36.pyc b/WebPython/__pycache__/settings.cpython-36.pyc deleted file mode 100644 index 68e357c..0000000 Binary files a/WebPython/__pycache__/settings.cpython-36.pyc and /dev/null differ diff --git a/WebPython/__pycache__/urls.cpython-36.pyc b/WebPython/__pycache__/urls.cpython-36.pyc deleted file mode 100644 index 34501b4..0000000 Binary files a/WebPython/__pycache__/urls.cpython-36.pyc and /dev/null differ diff --git a/WebPython/__pycache__/wsgi.cpython-36.pyc b/WebPython/__pycache__/wsgi.cpython-36.pyc deleted file mode 100644 index 65dd24f..0000000 Binary files a/WebPython/__pycache__/wsgi.cpython-36.pyc and /dev/null differ diff --git a/WebPython/settings.py b/WebPython/settings.py index cabf0b7..8411e4d 100644 --- a/WebPython/settings.py +++ b/WebPython/settings.py @@ -24,10 +24,8 @@ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '4@@kcvc5(3yijb@v6y&^gbh^80cpfy=2ysye8(tm#0@*)98ih=' - # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True - ALLOWED_HOSTS = [] diff --git a/WebPython/settings.pyc b/WebPython/settings.pyc index 2800551..c40ece8 100644 Binary files a/WebPython/settings.pyc and b/WebPython/settings.pyc differ diff --git a/WebPython/templates/Wallet/choose_date.html b/WebPython/templates/Wallet/choose_date.html index d08f0e7..4e1a780 100644 --- a/WebPython/templates/Wallet/choose_date.html +++ b/WebPython/templates/Wallet/choose_date.html @@ -4,7 +4,7 @@ {% block content %} -

{{wallet.wallet_name}}({{wallet.wallet_char_code}})

+

{{wallet.wallet_name}}({{wallet.wallet_char_code}})


{% if dates_in_memory %} {% for date in dates_in_memory %} @@ -13,6 +13,7 @@

{{wallet.wallet_name}}({{wallet.wallet_char_code}})

{% else %} Не загружены даты {% endif %} +
diff --git a/WebPython/templates/Wallet/list.html b/WebPython/templates/Wallet/list.html index b634fd3..43d11d6 100644 --- a/WebPython/templates/Wallet/list.html +++ b/WebPython/templates/Wallet/list.html @@ -3,7 +3,9 @@ {% block title %}Главная страница{% endblock %} {% block content %} - +Выберите валюту, чтобы посмотреть информацию о ней
+
+

{% if wallets_list %} {% for a in wallets_list %} {{a}}
@@ -11,13 +13,9 @@ {% else %} Статья не найдена {% endif %} - - -


- +


-
- + (все данные берутся с центробанка. Отсутствие каких либо данных говорит об отсутсвии их на соответствующем сайте) {% endblock %} diff --git a/WebPython/templates/Wallet/wallet.html b/WebPython/templates/Wallet/wallet.html index dccb600..43d8846 100644 --- a/WebPython/templates/Wallet/wallet.html +++ b/WebPython/templates/Wallet/wallet.html @@ -4,9 +4,12 @@ {% block content %} -

{{wallet.wallet_name}}({{wallet.wallet_char_code}})

+

{{wallet.wallet_name}}({{wallet.wallet_char_code}})

+
+

+ Номинал: {{wallet_info.wallet_nominal}}
+ Курс: {{wallet_info.wallet_value}}
+ Дата: {{wallet_info.date}} +


-

Номинал: {{wallet_info.wallet_nominal}}

-

Курс: {{wallet_info.wallet_value}}

- Дата: {{wallet_info.date}} {% endblock %} diff --git a/WebPython/templates/base.html b/WebPython/templates/base.html index e31ff8b..2d59503 100644 --- a/WebPython/templates/base.html +++ b/WebPython/templates/base.html @@ -4,7 +4,7 @@ {% block title %}Это шаблон{% endblock %} - + {% block content %}{% endblock %} diff --git a/WebPython/urls.py b/WebPython/urls.py index 99d9346..0abbefe 100644 --- a/WebPython/urls.py +++ b/WebPython/urls.py @@ -18,5 +18,5 @@ urlpatterns = [ path(r'', include("Wallet.urls")), - path(r'^admin/', admin.site.urls), + path(r'admin/', admin.site.urls), ] diff --git a/db.sqlite3 b/db.sqlite3 index b3875ea..5b47885 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ