Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea/
__pycache__/
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@

Команда для запуска:
python3 manage.py runserver (в консоли находясь в папке, в которой находится manage.py)

Сайт отображает валюты на определенный период(используются данный центробанка)

Для запуска тестов:
python3 manage.py test Wallet
(тест на корректность добавления в базу данных и на правильное удаление, второй тест на правильную работу загрузки данных)
20 changes: 20 additions & 0 deletions WebPython/Apps/Wallet/ClearRepeatedDate.py
Original file line number Diff line number Diff line change
@@ -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
Binary file modified WebPython/Apps/Wallet/__pycache__/apps.cpython-36.pyc
Binary file not shown.
Binary file modified WebPython/Apps/Wallet/__pycache__/load_data.cpython-36.pyc
Binary file not shown.
Binary file modified WebPython/Apps/Wallet/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file modified WebPython/Apps/Wallet/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file modified WebPython/Apps/Wallet/__pycache__/views.cpython-36.pyc
Binary file not shown.
Binary file added WebPython/Apps/Wallet/apps.pyc
Binary file not shown.
18 changes: 11 additions & 7 deletions WebPython/Apps/Wallet/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@


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),
wallet_value=float(".".join(i.find('value').text.split(','))),
date='-'.join(soup.find('valcurs')['date'].split('.')[::-1])
)
data.save()
print(data.wallet_nominal)
print(data.wallet_value)
else:
raise ValueError

Expand Down
137 changes: 137 additions & 0 deletions WebPython/Apps/Wallet/tests.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 4 additions & 4 deletions WebPython/Apps/Wallet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
app_name = "Wallet"

urlpatterns = [
path('main/<str:wallet_name_>/', views.get_date,
path('<str:wallet_name_>/', views.get_date,
name='get_date'),
path('main/', views.main_page, name='main_page'),
path('main/<str:wallet_name_>/show_wallet/',
path('', views.main_page, name='main_page'),
path('<str:wallet_name_>/show_wallet/',
views.show_wallet,
name='show_wallet'),
path('main/<str:wallet_name_>/<str:wallet_date_>/show_wallet/',
path('<str:wallet_name_>/<str:wallet_date_>/show_wallet/',
views.show_wallet,
name='show_wallet')
]
20 changes: 14 additions & 6 deletions WebPython/Apps/Wallet/views.py
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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()
Expand Down
Binary file removed WebPython/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file removed WebPython/__pycache__/settings.cpython-36.pyc
Binary file not shown.
Binary file removed WebPython/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file removed WebPython/__pycache__/wsgi.cpython-36.pyc
Binary file not shown.
2 changes: 0 additions & 2 deletions WebPython/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []


Expand Down
Binary file modified WebPython/settings.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion WebPython/templates/Wallet/choose_date.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{% block content %}

<h2>{{wallet.wallet_name}}({{wallet.wallet_char_code}})</h2>
<h2 align="center">{{wallet.wallet_name}}({{wallet.wallet_char_code}})</h2>
<hr>
{% if dates_in_memory %}
{% for date in dates_in_memory %}
Expand All @@ -13,6 +13,7 @@ <h2>{{wallet.wallet_name}}({{wallet.wallet_char_code}})</h2>
{% else %}
Не загружены даты
{% endif %}
<hr>

<form action="{% url 'Wallet:show_wallet' wallet.wallet_name%}" method="POST">

Expand Down
12 changes: 5 additions & 7 deletions WebPython/templates/Wallet/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
{% block title %}Главная страница{% endblock %}

{% block content %}

<em>Выберите валюту, чтобы посмотреть информацию о ней </em><br>
<hr>
<p align="center">
{% if wallets_list %}
{% for a in wallets_list %}
<a href="{% url 'Wallet:get_date' a%}">{{a}}</a><br>
{% endfor %}
{% else %}
Статья не найдена
{% endif %}


<hr>

</p>

<hr>
<hr>

<em><font size="1"> (все данные берутся с центробанка. Отсутствие каких либо данных говорит об отсутсвии их на соответствующем сайте)</font></em>
{% endblock %}

11 changes: 7 additions & 4 deletions WebPython/templates/Wallet/wallet.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

{% block content %}

<h2>{{wallet.wallet_name}}({{wallet.wallet_char_code}})</h2>
<h2 align="center">{{wallet.wallet_name}}({{wallet.wallet_char_code}})</h2>
<hr>
<p align="center">
Номинал: {{wallet_info.wallet_nominal}} <br>
Курс: {{wallet_info.wallet_value}} <br>
<em> Дата: {{wallet_info.date}}</em>
</p>
<hr>
<p> Номинал: {{wallet_info.wallet_nominal}}</p>
<p> Курс: {{wallet_info.wallet_value}}</p>
<em> Дата: {{wallet_info.date}}</em>
{% endblock %}
2 changes: 1 addition & 1 deletion WebPython/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<title>{% block title %}Это шаблон{% endblock %}</title>
</head>
<body>
<body bgcolor="#F5DEB3">
{% block content %}{% endblock %}
</body>
</html>
2 changes: 1 addition & 1 deletion WebPython/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@

urlpatterns = [
path(r'', include("Wallet.urls")),
path(r'^admin/', admin.site.urls),
path(r'admin/', admin.site.urls),
]
Binary file modified db.sqlite3
Binary file not shown.