Skip to content
Snippets Groups Projects
Commit 87132337 authored by matograine's avatar matograine
Browse files

[mod] creating tui.py

* create tui.py with license statement
* move display_amount() and display_pubkey() to tui.py to prevent circular dependencies
* change display_amount() to prevent circular dependency with money.py.
    * Now display_amount() needs the ud_value parameter.
    * display_amount() is no more async
* mod tx.py and test_unit_tx.py to match new location of the two features.
parent cf3f71da
No related branches found
No related tags found
2 merge requests!146Merge dev into master branch to complete v0.8.0 development cycle,!113Balance display
"""
Copyright 2016-2019 Maël Azimi <m.a@moul.re>
Silkaj is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Silkaj is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
"""
from silkaj import wot
def display_amount(tx, message, amount, ud_value, currency_symbol):
"""
Displays an amount in unit and relative reference.
"""
amount_UD = round((amount / ud_value), 4)
tx.append(
[
message + " (unit|relative)",
"{unit_amount} {currency_symbol} | {UD_amount} UD {currency_symbol}".format(
unit_amount=str(amount / 100),
currency_symbol=currency_symbol,
UD_amount=str(amount_UD),
),
]
)
async def display_pubkey(tx, message, pubkey):
"""
Displays a pubkey and the eventually associated id.
"""
tx.append([message + " (pubkey)", pubkey])
id = await wot.is_member(pubkey)
if id:
tx.append([message + " (id)", id["uid"]])
......@@ -26,7 +26,6 @@ from silkaj.network_tools import ClientInstance, HeadBlock
from silkaj.crypto_tools import check_public_key
from silkaj.tools import message_exit, CurrencySymbol, coroutine
from silkaj.auth import auth_method
from silkaj import wot
from silkaj.money import (
get_sources,
get_amount_from_pubkey,
......@@ -34,6 +33,7 @@ from silkaj.money import (
amount_in_current_base,
)
from silkaj.constants import SOURCES_PER_TX
from silkaj.tui import display_amount, display_pubkey
from duniterpy.api.bma.tx import process
from duniterpy.documents import BlockUID, Transaction
......@@ -149,35 +149,6 @@ def check_transaction_values(
)
async def display_amount(tx, message, amount, currency_symbol):
"""
For transaction_confirmation,
Displays an amount in unit and relative reference.
"""
amount_UD = round((amount / await UDValue().ud_value), 4)
tx.append(
[
message + " (unit|relative)",
"{unit_amount} {currency_symbol} | {UD_amount} UD {currency_symbol}".format(
unit_amount=str(amount / 100),
currency_symbol=currency_symbol,
UD_amount=str(amount_UD),
),
]
)
async def display_pubkey(tx, message, pubkey):
"""
For transaction_confirmation,
Displays a pubkey and the eventually associated id.
"""
tx.append([message + " (pubkey)", pubkey])
id = await wot.is_member(pubkey)
if id:
tx.append([message + " (id)", id["uid"]])
async def transaction_confirmation(
issuer_pubkey, pubkey_amount, tx_amount, outputAddresses, outputBackChange, comment
):
......@@ -186,13 +157,18 @@ async def transaction_confirmation(
"""
currency_symbol = await CurrencySymbol().symbol
ud_value = await UDValue().ud_value
tx = list()
tx.append(
["pubkey’s balance before tx", str(pubkey_amount / 100) + " " + currency_symbol]
)
await display_amount(
tx, "total amount", float(tx_amount * len(outputAddresses)), currency_symbol
display_amount(
tx,
"total amount",
float(tx_amount * len(outputAddresses)),
ud_value,
currency_symbol,
)
tx.append(
......@@ -207,7 +183,7 @@ async def transaction_confirmation(
await display_pubkey(tx, "from", issuer_pubkey)
for outputAddress in outputAddresses:
await display_pubkey(tx, "to", outputAddress)
await display_amount(tx, "amount", tx_amount, currency_symbol)
display_amount(tx, "amount", tx_amount, ud_value, currency_symbol)
if outputBackChange:
await display_pubkey(tx, "Backchange", outputBackChange)
......
import pytest
from silkaj.tx import (
truncBase,
display_pubkey,
display_amount,
transaction_confirmation,
)
from silkaj.tx import truncBase, transaction_confirmation
from silkaj.tui import display_pubkey, display_amount
from silkaj.money import UDValue
from silkaj.constants import G1_SYMBOL
import patched
......@@ -22,11 +18,9 @@ def test_truncBase(amount, base, expected):
@pytest.mark.parametrize(
"message, amount, currency_symbol", [("Total", 1000, G1_SYMBOL)]
)
@pytest.mark.asyncio
async def test_display_amount(message, amount, currency_symbol, monkeypatch):
monkeypatch.setattr("silkaj.money.UDValue.get_ud_value", patched.ud_value)
amount_UD = round(amount / await UDValue().ud_value, 4)
def test_display_amount(message, amount, currency_symbol, monkeypatch):
ud_value = patched.mock_ud_value
amount_UD = round(amount / ud_value, 4)
expected = [
[
message + " (unit|relative)",
......@@ -40,7 +34,7 @@ async def test_display_amount(message, amount, currency_symbol, monkeypatch):
]
]
tx = list()
await display_amount(tx, message, amount, currency_symbol)
display_amount(tx, message, amount, ud_value, currency_symbol)
assert tx == expected
......@@ -122,6 +116,7 @@ async def test_transaction_confirmation(
)
# creating expected list
ud_value = await UDValue().ud_value
expected = list()
expected.append(
[
......@@ -130,10 +125,11 @@ async def test_transaction_confirmation(
]
)
await display_amount(
display_amount(
expected,
"total amount",
float(tx_amount * len(outputAddresses)),
ud_value,
currency_symbol,
)
......@@ -149,7 +145,7 @@ async def test_transaction_confirmation(
await display_pubkey(expected, "from", issuer_pubkey)
for outputAddress in outputAddresses:
await display_pubkey(expected, "to", outputAddress)
await display_amount(expected, "amount", tx_amount, currency_symbol)
display_amount(expected, "amount", tx_amount, ud_value, currency_symbol)
if outputBackChange:
await display_pubkey(expected, "Backchange", outputBackChange)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment