Skip to content

Commit

Permalink
fixed some shit
Browse files Browse the repository at this point in the history
  • Loading branch information
nesb1 committed Jun 23, 2020
1 parent 983d668 commit d00fa34
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 44 deletions.
13 changes: 12 additions & 1 deletion src/DAL/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
AbstractUserCurrencyGetter,
ConcreteUserCurrencyGetter,
)
from src.DAL.user_getter import ConcreteUserGetter, AbstractUserGetter
from src.DAL.utils import run_in_threadpool
from src.models.currency import Currency, UserCurrency, CurrencyHistory
from src.models.operation import OperationToShow, OperationType
Expand All @@ -24,14 +25,16 @@ def __init__(
user_currencies_getter: AbstractUserCurrencyGetter = ConcreteUserCurrencyGetter(),
user_operations_getter: AbstractOperationsGetter = ConcreteOperationsGetter(),
currency_history_getter: AbstractCurrencyHistoryGetter = ConcreteCurrencyHistoryGetter(),
operation_maker: AbstractOperationMaker = ConcreteOperationMaker()
operation_maker: AbstractOperationMaker = ConcreteOperationMaker(),
user_getter: AbstractUserGetter = ConcreteUserGetter()
):
self._authorization: AbstractAuthorization = authorization
self._currencies_getter: AbstractCurrenciesGetter = currencies_getter
self._user_currencies_getter = user_currencies_getter
self._operations_getter = user_operations_getter
self._currency_history_getter = currency_history_getter
self._operation_maker = operation_maker
self._user_getter: AbstractUserGetter = user_getter

@run_in_threadpool
def get_operations(self, user_id: int) -> List[OperationToShow]:
Expand All @@ -56,3 +59,11 @@ def get_currency_history(self, currency_id: int) -> List[CurrencyHistory]:
@run_in_threadpool
def make_operation(self, operation_type: OperationType, user: User, currency: UserCurrency, amount: Decimal):
return self._operation_maker.make(operation_type, user, currency, amount)

@run_in_threadpool
def get_user(self, user_id: int) -> User:
return self._user_getter.get(user_id)

@run_in_threadpool
def get_user_currency(self, user_id: int, currency_id: int) -> UserCurrency:
return self._user_currencies_getter.get_currency(user_id,currency_id)
12 changes: 12 additions & 0 deletions src/DAL/user_currencies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from abc import ABC, abstractmethod
from concurrent.futures.thread import ThreadPoolExecutor
from decimal import Decimal
from http import HTTPStatus
from typing import List

import requests
Expand All @@ -16,8 +18,18 @@ class AbstractUserCurrencyGetter(ABC):
def get(self, user_id: int) -> List[UserCurrency]:
pass

@abstractmethod
def get_currency(self, user_id: int, currency_id: int) -> UserCurrency:
pass


class ConcreteUserCurrencyGetter(AbstractUserCurrencyGetter):
def get_currency(self, user_id: int, currency_id: int) -> UserCurrency:
res = requests.get(f'{Urls.USERS.value}/{user_id}/currencies/{currency_id}')
if res.status_code == HTTPStatus.BAD_REQUEST:
return self._get_currency(CurrencyItem(user_id=user_id, currency_id=currency_id, amount=Decimal('0')))
return self._get_currency(CurrencyItem.parse_obj(res.json()))

def _get_currency(self, currency_item: CurrencyItem) -> UserCurrency:
response: Response = requests.get(
f'{Urls.CURRENCIES.value}/{currency_item.currency_id}'
Expand Down
18 changes: 18 additions & 0 deletions src/DAL/user_getter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from abc import ABC, abstractmethod

import requests

from src.models.user import User
from src.urls import Urls


class AbstractUserGetter(ABC):
@abstractmethod
def get(self, user_id: int) -> User:
pass


class ConcreteUserGetter(AbstractUserGetter):
def get(self, user_id: int) -> User:
response = requests.get(Urls.USERS.value, params={'user_id': user_id})
return User.parse_obj(response.json())
1 change: 0 additions & 1 deletion src/models/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class Currency(CurrencyHistory):


class CurrencyItem(BaseModel):
id: int
user_id: int
currency_id: int
amount: Decimal
Expand Down
14 changes: 10 additions & 4 deletions src/view/buy_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

from src.DAL.client import Client
from src.exceptions import DALError
from src.message import Message
from src.models.currency import UserCurrency
from src.models.operation import OperationType
from src.models.user import User
# from src.view.utils import show_error
from ui.buy_window import Ui_BuyWindow
from decimal import Decimal
from requests.exceptions import ConnectionError

class BuyWindow(Ui_BuyWindow, QMainWindow):
def __init__(self, parent, client: Client, user: User, currency: UserCurrency):
super().__init__(parent, Qt.WindowCloseButtonHint)
self.parent = parent
self.setupUi(self)
self._user: User = user
self._client = client
Expand All @@ -26,10 +28,14 @@ def buy(self):
d = Decimal(self.amount.text())
except Exception:
raise DALError('Кол-во должно быть числом')
self._client.make_operation(OperationType.BUY, self._user, self._currency, d)
self.close()
try:
self._client.make_operation(OperationType.BUY, self._user, self._currency, d)
self.parent.refresh()
self.close()
except ConnectionError:
QMessageBox().warning(self, 'Ошибка', str(Message.CONNECTION_ERROR.value))

except DALError as e:
# show_error(str(e))
QMessageBox().warning(self, 'Ошибка', str(e))


Expand Down
38 changes: 12 additions & 26 deletions src/view/currency_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,22 @@

import pyqtgraph as pg
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMainWindow, QMessageBox

from src.DAL.client import Client
from src.message import Message
from src.models.currency import CurrencyHistory, UserCurrency
from src.models.user import User
from src.view.buy_window import BuyWindow
from src.view.sell_wndow import SellWindow
from ui.currency import Ui_CurrencyWindow


# selling_prices = [1.337, 1.488, 1.556, 1.246, 1.889, 1.337, 1.488, 1.556, 1.246, 1.889, 1.556, 1.246, 1.889]
# purchasing_prices = [2.534, 2.679, 2.365, 2.456, 2.345, 2.534, 2.679, 2.365, 2.456, 2.345, 2.365, 2.456, 2.345]
# time = [
# datetime(2020, 5, 1, 0, 0, 0, 111),
# datetime(2020, 5, 2, 0, 0, 0),
# datetime(2020, 5, 3, 0, 0, 0),
# datetime(2020, 5, 4, 0, 0, 0),
# datetime(2020, 5, 5, 0, 0, 0),
# datetime(2020, 5, 7, 0, 0, 0),
# datetime(2020, 5, 8, 0, 0, 0),
# datetime(2020, 5, 9, 0, 0, 0),
# datetime(2020, 5, 10, 0, 0, 0),
# datetime(2020, 5, 11, 0, 0, 0),
# datetime(2020, 5, 12, 0, 0, 0),
# datetime(2020, 5, 13, 0, 0, 0),
# datetime(2020, 5, 14, 0, 0, 0),
# ]
#
# time = dict(enumerate(time))

from requests.exceptions import ConnectionError

class CurrencyWindow(Ui_CurrencyWindow, QMainWindow):
def __init__(self, parent, user: User, currency: UserCurrency):
super().__init__(parent, Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint)
self.setupUi(self)
self.parent = parent
self.buyButton.clicked.connect(self.buy)
self.sellButton.clicked.connect(self.sell)
self.refreshButton.clicked.connect(self.refresh)
Expand All @@ -53,6 +34,8 @@ def set_text_info(self, selling_price, buying_price, account):
self.account.setText('На счёте: ' + str(account) + ' у.е.')

def draw_graphs(self):
self._user = self._client.get_user(self._user.id)
self._currency = self._client.get_user_currency(self._user.id, self._currency.id)
pg.setConfigOptions(antialias=True)
history_items: List[CurrencyHistory] = self._client.get_currency_history(self._currency.id)
selling_prices = list(map(lambda item: float(item.selling_price), history_items))
Expand Down Expand Up @@ -110,16 +93,19 @@ def plot(self, x, y, plot_name, color, width, symbol, symbol_size):
)

def buy(self):
self.buy_window = BuyWindow(self,self._client, self._user, self._currency)
self.buy_window = BuyWindow(self, self._client, self._user, self._currency)
self.buy_window.init()

def sell(self):
self.sell_window = SellWindow(self, self._client, self._user, self._currency)
self.sell_window.init()

def refresh(self):
self.graphicsView.plotItem.clear()
self.init()
try:
self.graphicsView.plotItem.clear()
self.init()
except ConnectionError:
QMessageBox().warning(self, 'Ошибка', str(Message.CONNECTION_ERROR.value))

def init(self):
self.draw_graphs()
Expand Down
8 changes: 6 additions & 2 deletions src/view/login_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ def init(self):
def auth(self):
self.close()
try:
user = self._authorize.sign_in(self.login.text())
text = self.login.text()
if text:
user = self._authorize.sign_in(text)
else:
QMessageBox().warning(self, 'Ошибка', 'Неверные данные')
return
except ConnectionError:
QMessageBox().warning(self, 'Ошибка', Message.CONNECTION_ERROR.value)
# show_error(Message.CONNECTION_ERROR.value)
return
self.mainWindow = MainWindow(user, self)
self.mainWindow.init()
6 changes: 3 additions & 3 deletions src/view/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def fill_all_currencies(self):
create_headers(self.allCurrenciesTable, labels)
for i in currencies:
detail_button = QPushButton('Подробнее')
detail_button.clicked.connect(lambda a,i=i: self.show_details(self.user, UserCurrency(**i.dict())))
detail_button.clicked.connect(lambda a, i=i: self.show_details(self.user, UserCurrency(**i.dict())))
row = self.allCurrenciesTable.rowCount()
self.allCurrenciesTable.setRowCount(row + 1)

Expand All @@ -64,7 +64,7 @@ def fill_my_currencies(self):
currencies = self._client.get_user_currencies(self.user.id)
for i in currencies:
detail_button = QPushButton('Подробнее')
detail_button.clicked.connect(lambda: self.show_details(self.user, i))
detail_button.clicked.connect(lambda f, i=i: self.show_details(self.user, i))
row = self.myCurrenciesTable.rowCount()
self.myCurrenciesTable.setRowCount(row + 1)

Expand All @@ -84,6 +84,7 @@ def fill_operations(self):
labels = ['Операция', 'Название', 'Количество']
create_headers(self.operationsTable, labels)
operations = self._client.get_operations(self.user.id)
operations.reverse()
for i in operations:
row = self.operationsTable.rowCount()
self.operationsTable.setRowCount(row + 1)
Expand Down Expand Up @@ -113,7 +114,6 @@ def init(self):
self.fill_operations()
except ConnectionError:
QMessageBox().warning(self, 'Ошибка', Message.CONNECTION_ERROR.value)
# show_error(Message.CONNECTION_ERROR.value)
return
self.show()

Expand Down
24 changes: 17 additions & 7 deletions src/view/sell_wndow.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from decimal import Decimal

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow

from PyQt5.QtWidgets import QMainWindow, QMessageBox
from requests.exceptions import ConnectionError
from src.DAL.client import Client
from src.exceptions import DALError
from src.message import Message
from src.models.currency import UserCurrency
from src.models.operation import OperationType
from src.models.user import User
Expand All @@ -14,6 +16,7 @@ class SellWindow(Ui_SellWindow, QMainWindow):
def __init__(self, parent, client: Client, user: User, currency: UserCurrency):
super().__init__(parent, Qt.WindowCloseButtonHint)
self.setupUi(self)
self.parent = parent
self._user: User = user
self._client = client
self._currency: UserCurrency = currency
Expand All @@ -22,11 +25,18 @@ def __init__(self, parent, client: Client, user: User, currency: UserCurrency):

def sell(self):
try:
d = Decimal(self.amount.text())
except Exception:
raise Exception()
self._client.make_operation(OperationType.SELL, self._user, self._currency, d)
self.close()
try:
d = Decimal(self.amount.text())
except Exception:
raise DALError('Кол-во должно быть числом')
try:
self._client.make_operation(OperationType.SELL, self._user, self._currency, d)
except ConnectionError:
QMessageBox().warning(self, 'Ошибка', str(Message.CONNECTION_ERROR.value))
self.parent.refresh()
self.close()
except DALError as e:
QMessageBox().warning(self, 'Ошибка', str(e))

def init(self):
self.show()

0 comments on commit d00fa34

Please sign in to comment.