refactor: Move bid page logic

This commit is contained in:
tecnovert 2022-10-05 19:30:52 +02:00
parent f29eac0eb5
commit a4f4a411a9
No known key found for this signature in database
GPG key ID: 8ED6D8750C4E3F93
3 changed files with 182 additions and 160 deletions

View file

@ -17,7 +17,6 @@ from . import __version__
from .util import (
dumpj,
ensure,
toBool,
format_timestamp,
)
from .chainparams import (
@ -25,9 +24,6 @@ from .chainparams import (
chainparams,
)
from .basicswap_util import (
BidStates,
SwapTypes,
DebugTypes,
strBidState,
strTxState,
strAddressType,
@ -37,25 +33,20 @@ from .js_server import (
js_url_to_function,
)
from .ui.util import (
PAGE_LIMIT,
describeBid,
getCoinName,
listBidStates,
get_data_entry,
have_data_entry,
listOldBidStates,
get_data_entry_or,
listAvailableCoins,
set_pagination_filters,
)
from .ui.page_tor import page_tor
from .ui.page_offers import page_offers, page_offer, page_newoffer
from .ui.page_wallet import page_wallets, page_wallet
from .ui.page_automation import (
page_automation_strategies,
page_automation_strategy,
page_automation_strategy_new,
)
from .ui.page_bids import page_bids, page_bid
from .ui.page_offers import page_offers, page_offer, page_newoffer
from .ui.page_tor import page_tor
from .ui.page_wallet import page_wallets, page_wallet
env = Environment(loader=PackageLoader('basicswap', 'templates'))
@ -344,148 +335,6 @@ class HttpHandler(BaseHTTPRequestHandler):
'chains': chains_formatted,
})
def page_bid(self, url_split, post_string):
ensure(len(url_split) > 2, 'Bid ID not specified')
try:
bid_id = bytes.fromhex(url_split[2])
assert len(bid_id) == 28
except Exception:
raise ValueError('Bad bid ID')
swap_client = self.server.swap_client
messages = []
err_messages = []
show_txns = False
show_offerer_seq_diagram = False
show_bidder_seq_diagram = False
show_lock_transfers = False
edit_bid = False
view_tx_ind = None
form_data = self.checkForm(post_string, 'bid', err_messages)
if form_data:
if b'abandon_bid' in form_data:
try:
swap_client.abandonBid(bid_id)
messages.append('Bid abandoned')
except Exception as ex:
err_messages.append('Abandon failed ' + str(ex))
elif b'accept_bid' in form_data:
try:
swap_client.acceptBid(bid_id)
messages.append('Bid accepted')
except Exception as ex:
err_messages.append('Accept failed ' + str(ex))
elif b'show_txns' in form_data:
show_txns = True
elif b'show_offerer_seq_diagram' in form_data:
show_offerer_seq_diagram = True
elif b'show_bidder_seq_diagram' in form_data:
show_bidder_seq_diagram = True
elif b'edit_bid' in form_data:
edit_bid = True
elif b'edit_bid_submit' in form_data:
data = {
'bid_state': int(form_data[b'new_state'][0]),
'debug_ind': int(get_data_entry_or(form_data, 'debugind', -1)),
'kbs_other': get_data_entry_or(form_data, 'kbs_other', None),
}
try:
swap_client.manualBidUpdate(bid_id, data)
messages.append('Bid edited')
except Exception as ex:
err_messages.append('Edit failed ' + str(ex))
elif b'view_tx_submit' in form_data:
show_txns = True
view_tx_ind = form_data[b'view_tx'][0].decode('utf-8')
if len(view_tx_ind) != 64:
err_messages.append('Invalid transaction selected.')
view_tx_ind = None
elif b'view_lock_transfers' in form_data:
show_txns = True
show_lock_transfers = True
bid, xmr_swap, offer, xmr_offer, events = swap_client.getXmrBidAndOffer(bid_id)
ensure(bid, 'Unknown bid ID')
data = describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, events, edit_bid, show_txns, view_tx_ind, show_lock_transfers=show_lock_transfers)
if bid.debug_ind is not None and bid.debug_ind > 0:
messages.append('Debug flag set: {}, {}'.format(bid.debug_ind, DebugTypes(bid.debug_ind).name))
data['show_bidder_seq_diagram'] = show_bidder_seq_diagram
data['show_offerer_seq_diagram'] = show_offerer_seq_diagram
old_states = listOldBidStates(bid)
if len(data['addr_from_label']) > 0:
data['addr_from_label'] = '(' + data['addr_from_label'] + ')'
template = env.get_template('bid_xmr.html') if offer.swap_type == SwapTypes.XMR_SWAP else env.get_template('bid.html')
return self.render_template(template, {
'bid_id': bid_id.hex(),
'messages': messages,
'err_messages': err_messages,
'data': data,
'edit_bid': edit_bid,
'old_states': old_states,
})
def page_bids(self, url_split, post_string, sent=False, available=False):
swap_client = self.server.swap_client
filters = {
'page_no': 1,
'bid_state_ind': -1,
'with_expired': True,
'limit': PAGE_LIMIT,
'sort_by': 'created_at',
'sort_dir': 'desc',
}
if available:
filters['bid_state_ind'] = BidStates.BID_RECEIVED
filters['with_expired'] = False
messages = []
form_data = self.checkForm(post_string, 'bids', messages)
if form_data and have_data_entry(form_data, 'applyfilters'):
if have_data_entry(form_data, 'sort_by'):
sort_by = get_data_entry(form_data, 'sort_by')
ensure(sort_by in ['created_at', ], 'Invalid sort by')
filters['sort_by'] = sort_by
if have_data_entry(form_data, 'sort_dir'):
sort_dir = get_data_entry(form_data, 'sort_dir')
ensure(sort_dir in ['asc', 'desc'], 'Invalid sort dir')
filters['sort_dir'] = sort_dir
if have_data_entry(form_data, 'state'):
state_ind = int(get_data_entry(form_data, 'state'))
if state_ind != -1:
try:
state = BidStates(state_ind)
except Exception:
raise ValueError('Invalid state')
filters['bid_state_ind'] = state_ind
if have_data_entry(form_data, 'with_expired'):
with_expired = toBool(get_data_entry(form_data, 'with_expired'))
filters['with_expired'] = with_expired
set_pagination_filters(form_data, filters)
bids = swap_client.listBids(sent=sent, filters=filters)
page_data = {
'bid_states': listBidStates()
}
template = env.get_template('bids.html')
return self.render_template(template, {
'page_type': 'Sent' if sent else 'Received',
'messages': messages,
'filters': filters,
'data': page_data,
'bids': [(format_timestamp(b[0]),
b[2].hex(), b[3].hex(), strBidState(b[5]), strTxState(b[7]), strTxState(b[8]), b[11]) for b in bids],
})
def page_watched(self, url_split, post_string):
swap_client = self.server.swap_client
watched_outputs, last_scanned = swap_client.listWatchedOutputs()
@ -735,13 +584,13 @@ class HttpHandler(BaseHTTPRequestHandler):
if page == 'sentoffers':
return page_offers(self, url_split, post_string, sent=True)
if page == 'bid':
return self.page_bid(url_split, post_string)
return page_bid(self, url_split, post_string)
if page == 'bids':
return self.page_bids(url_split, post_string)
return page_bids(self, url_split, post_string)
if page == 'sentbids':
return self.page_bids(url_split, post_string, sent=True)
return page_bids(self, url_split, post_string, sent=True)
if page == 'availablebids':
return self.page_bids(url_split, post_string, available=True)
return page_bids(self, url_split, post_string, available=True)
if page == 'watched':
return self.page_watched(url_split, post_string)
if page == 'smsgaddresses':

174
basicswap/ui/page_bids.py Normal file
View file

@ -0,0 +1,174 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2022 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
from .util import (
PAGE_LIMIT,
describeBid,
get_data_entry,
have_data_entry,
get_data_entry_or,
listBidStates,
listOldBidStates,
set_pagination_filters,
)
from basicswap.util import (
ensure,
toBool,
format_timestamp,
)
from basicswap.basicswap_util import (
BidStates,
SwapTypes,
DebugTypes,
strTxState,
strBidState,
)
def page_bid(self, url_split, post_string):
ensure(len(url_split) > 2, 'Bid ID not specified')
try:
bid_id = bytes.fromhex(url_split[2])
assert len(bid_id) == 28
except Exception:
raise ValueError('Bad bid ID')
server = self.server
swap_client = server.swap_client
messages = []
err_messages = []
show_txns = False
show_offerer_seq_diagram = False
show_bidder_seq_diagram = False
show_lock_transfers = False
edit_bid = False
view_tx_ind = None
form_data = self.checkForm(post_string, 'bid', err_messages)
if form_data:
if b'abandon_bid' in form_data:
try:
swap_client.abandonBid(bid_id)
messages.append('Bid abandoned')
except Exception as ex:
err_messages.append('Abandon failed ' + str(ex))
elif b'accept_bid' in form_data:
try:
swap_client.acceptBid(bid_id)
messages.append('Bid accepted')
except Exception as ex:
err_messages.append('Accept failed ' + str(ex))
elif b'show_txns' in form_data:
show_txns = True
elif b'show_offerer_seq_diagram' in form_data:
show_offerer_seq_diagram = True
elif b'show_bidder_seq_diagram' in form_data:
show_bidder_seq_diagram = True
elif b'edit_bid' in form_data:
edit_bid = True
elif b'edit_bid_submit' in form_data:
data = {
'bid_state': int(form_data[b'new_state'][0]),
'debug_ind': int(get_data_entry_or(form_data, 'debugind', -1)),
'kbs_other': get_data_entry_or(form_data, 'kbs_other', None),
}
try:
swap_client.manualBidUpdate(bid_id, data)
messages.append('Bid edited')
except Exception as ex:
err_messages.append('Edit failed ' + str(ex))
elif b'view_tx_submit' in form_data:
show_txns = True
view_tx_ind = form_data[b'view_tx'][0].decode('utf-8')
if len(view_tx_ind) != 64:
err_messages.append('Invalid transaction selected.')
view_tx_ind = None
elif b'view_lock_transfers' in form_data:
show_txns = True
show_lock_transfers = True
bid, xmr_swap, offer, xmr_offer, events = swap_client.getXmrBidAndOffer(bid_id)
ensure(bid, 'Unknown bid ID')
data = describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, events, edit_bid, show_txns, view_tx_ind, show_lock_transfers=show_lock_transfers)
if bid.debug_ind is not None and bid.debug_ind > 0:
messages.append('Debug flag set: {}, {}'.format(bid.debug_ind, DebugTypes(bid.debug_ind).name))
data['show_bidder_seq_diagram'] = show_bidder_seq_diagram
data['show_offerer_seq_diagram'] = show_offerer_seq_diagram
old_states = listOldBidStates(bid)
if len(data['addr_from_label']) > 0:
data['addr_from_label'] = '(' + data['addr_from_label'] + ')'
template = server.env.get_template('bid_xmr.html') if offer.swap_type == SwapTypes.XMR_SWAP else server.env.get_template('bid.html')
return self.render_template(template, {
'bid_id': bid_id.hex(),
'messages': messages,
'err_messages': err_messages,
'data': data,
'edit_bid': edit_bid,
'old_states': old_states,
})
def page_bids(self, url_split, post_string, sent=False, available=False):
server = self.server
swap_client = server.swap_client
filters = {
'page_no': 1,
'bid_state_ind': -1,
'with_expired': True,
'limit': PAGE_LIMIT,
'sort_by': 'created_at',
'sort_dir': 'desc',
}
if available:
filters['bid_state_ind'] = BidStates.BID_RECEIVED
filters['with_expired'] = False
messages = []
form_data = self.checkForm(post_string, 'bids', messages)
if form_data and have_data_entry(form_data, 'applyfilters'):
if have_data_entry(form_data, 'sort_by'):
sort_by = get_data_entry(form_data, 'sort_by')
ensure(sort_by in ['created_at', ], 'Invalid sort by')
filters['sort_by'] = sort_by
if have_data_entry(form_data, 'sort_dir'):
sort_dir = get_data_entry(form_data, 'sort_dir')
ensure(sort_dir in ['asc', 'desc'], 'Invalid sort dir')
filters['sort_dir'] = sort_dir
if have_data_entry(form_data, 'state'):
state_ind = int(get_data_entry(form_data, 'state'))
if state_ind != -1:
try:
state = BidStates(state_ind)
except Exception:
raise ValueError('Invalid state')
filters['bid_state_ind'] = state_ind
if have_data_entry(form_data, 'with_expired'):
with_expired = toBool(get_data_entry(form_data, 'with_expired'))
filters['with_expired'] = with_expired
set_pagination_filters(form_data, filters)
bids = swap_client.listBids(sent=sent, filters=filters)
page_data = {
'bid_states': listBidStates()
}
template = server.env.get_template('bids.html')
return self.render_template(template, {
'page_type': 'Sent' if sent else 'Received',
'messages': messages,
'filters': filters,
'data': page_data,
'bids': [(format_timestamp(b[0]),
b[2].hex(), b[3].hex(), strBidState(b[5]), strTxState(b[7]), strTxState(b[8]), b[11]) for b in bids],
})

View file

@ -32,7 +32,6 @@ from basicswap.basicswap_util import (
strBidState,
TxLockTypes,
strOfferState,
)
from basicswap.chainparams import (
Coins,