Skip to content

Commit

Permalink
added operations get functioallity
Browse files Browse the repository at this point in the history
  • Loading branch information
nesb1 committed Jun 20, 2020
1 parent 68cc920 commit 93a826c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 49 deletions.
8 changes: 8 additions & 0 deletions src/DAL/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from src.DAL.auth import AbstractAuthorization, ConcreteAuthorization
from src.DAL.currencies import AbstractCurrenciesGetter, ConcreteCurrencyGetter
from src.DAL.operations import AbstractOperationsGetter, ConcreteOperationsGetter
from src.DAL.user_currencies import (
AbstractUserCurrencyGetter,
ConcreteUserCurrencyGetter,
)
from src.DAL.utils import run_in_threadpool
from src.models.currency import Currency, UserCurrency
from src.models.operation import OperationToShow
from src.models.user import User


Expand All @@ -17,10 +19,16 @@ def __init__(
authorization: AbstractAuthorization = ConcreteAuthorization(),
currencies_getter: AbstractCurrenciesGetter = ConcreteCurrencyGetter(),
user_currencies_getter: AbstractUserCurrencyGetter = ConcreteUserCurrencyGetter(),
user_operations_getter: AbstractOperationsGetter = ConcreteOperationsGetter(),
):
self._authorization: AbstractAuthorization = authorization
self._currencies_getter: AbstractCurrenciesGetter = currencies_getter
self._user_currencies_getter = user_currencies_getter
self._operations_getter = user_operations_getter

@run_in_threadpool
def get_operations(self, user_id: int) -> List[OperationToShow]:
return self._operations_getter.get(user_id)

@run_in_threadpool
def get_all_currencies(self) -> List[Currency]:
Expand Down
33 changes: 33 additions & 0 deletions src/DAL/operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from abc import ABC, abstractmethod
from concurrent.futures.thread import ThreadPoolExecutor
from typing import List

import requests
from pydantic import parse_obj_as
from requests import Response

from config import service_settings
from src.models.currency import Currency
from src.models.operation import Operation, OperationToShow
from src.urls import Urls


class AbstractOperationsGetter(ABC):
@abstractmethod
def get(self, user_id: int) -> List[OperationToShow]:
pass


class ConcreteOperationsGetter(AbstractOperationsGetter):
def _get_operation(self, operation: Operation) -> OperationToShow:
response: Response = requests.get(
f'{Urls.CURRENCIES.value}/{operation.currency_id}'
)
currency = Currency.parse_obj(response.json())
return OperationToShow(**operation.dict(), currency_name=currency.name)

def get(self, user_id: int) -> List[OperationToShow]:
response: Response = requests.get(f'{Urls.USERS.value}/{user_id}/operations')
operations = parse_obj_as(List[Operation], response.json())
with ThreadPoolExecutor(service_settings.max_workers) as executor:
return list(executor.map(self._get_operation, operations))
1 change: 0 additions & 1 deletion src/DAL/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Any, Callable



def run_in_threadpool(func: Callable[..., Any]) -> Callable[..., Any]:
def wrapper(*args: Any, **kwargs: Any):
with ThreadPoolExecutor(1) as executor:
Expand Down
19 changes: 19 additions & 0 deletions src/models/operation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from decimal import Decimal
from enum import Enum

from pydantic import BaseModel


class OperationType(Enum):
BUY = 'BUY'
SELL = 'SELL'


class Operation(BaseModel):
operation_type: OperationType
currency_id: int
amount: Decimal


class OperationToShow(Operation):
currency_name: str
2 changes: 0 additions & 2 deletions src/view/login_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ def __init__(self):
def init(self):
self.show()

"""Тут нужно добавить валидацию, возможно добавить обработку исключений сервера"""

def auth(self):
self.close()
try:
Expand Down
55 changes: 9 additions & 46 deletions src/view/main_window.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from threading import Thread
from typing import NamedTuple

from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
Expand All @@ -8,49 +6,13 @@

from src.DAL.client import Client
from src.message import Message
from src.models.operation import OperationType
from src.models.user import User
from src.view.currency_window import CurrencyWindow
from src.view.utils import show_error
from ui.main import Ui_MainWindow


class OperationData(NamedTuple):
type: str
name: str
amount: int
account: str


operation_data = [
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
OperationData('Продажа', 'Биток', 20, '+ 20 у. е.'),
]


class MainWindow(Ui_MainWindow, QMainWindow):
def __init__(self, user: User, parent):
super().__init__(parent, Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint)
Expand Down Expand Up @@ -124,17 +86,18 @@ def fill_my_currencies(self):

def fill_operations(self):
self.operationsTable.clear()
labels = ['Операция', 'Название', 'Количество', 'Счёт']
labels = ['Операция', 'Название', 'Количество']
create_headers(self.operationsTable, labels)

for i in operation_data:
operations = self._client.get_operations(self.user.id)
for i in operations:
row = self.operationsTable.rowCount()
self.operationsTable.setRowCount(row + 1)

self.operationsTable.setItem(row, 0, QTableWidgetItem(i.type))
self.operationsTable.setItem(row, 1, QTableWidgetItem(i.name))
self.operationsTable.setItem(row, 2, QTableWidgetItem(str(i.amount)))
self.operationsTable.setItem(row, 3, QTableWidgetItem(i.account))
self.operationsTable.setItem(
row, 0, QTableWidgetItem('Покупка' if i.operation_type == OperationType.BUY else 'Продажа')
)
self.operationsTable.setItem(row, 1, QTableWidgetItem(i.currency_name))
self.operationsTable.setItem(row, 2, QTableWidgetItem((str(i.amount))))

'''Логика кнопки "Подробнее"'''

Expand Down

0 comments on commit 93a826c

Please sign in to comment.