From 27929d41ec9b0c7097addc7bbc8c9d9aa609edee Mon Sep 17 00:00:00 2001 From: tecnovert Date: Fri, 26 Jul 2019 22:15:53 +0200 Subject: [PATCH] html: offer page templates. --- basicswap/http_server.py | 104 ++++++++++++----------------- basicswap/templates/offer.html | 48 +++++++++++++ basicswap/templates/offer_new.html | 36 ++++++++++ 3 files changed, 127 insertions(+), 61 deletions(-) create mode 100644 basicswap/templates/offer.html create mode 100644 basicswap/templates/offer_new.html diff --git a/basicswap/http_server.py b/basicswap/http_server.py index c3bc5ca..aeabbe1 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -161,24 +161,15 @@ class HttpHandler(BaseHTTPRequestHandler): content += '

home

' return bytes(content, 'UTF-8') - def make_coin_select(self, name, coins): - s = '' - return s - def page_newoffer(self, url_split, post_string): swap_client = self.server.swap_client - content = html_content_start(self.server.title, self.server.title) \ - + '

New Offer

' - + messages = [] if post_string != '': form_data = urllib.parse.parse_qs(post_string) form_id = form_data[b'formid'][0].decode('utf-8') if self.server.last_form_id.get('newoffer', None) == form_id: - content += '

Prevented double submit for form {}.

'.format(form_id) + messages.append('Prevented double submit for form {}.'.format(form_id)) else: self.server.last_form_id['newoffer'] = form_id @@ -206,28 +197,21 @@ class HttpHandler(BaseHTTPRequestHandler): lock_type = ABS_LOCK_TIME offer_id = swap_client.postOffer(coin_from, coin_to, value_from, rate, min_bid, SwapTypes.SELLER_FIRST, auto_accept_bids=autoaccept, lock_type=lock_type, lock_value=lock_seconds) - content += '

Sent Offer ' + offer_id.hex() + '
Rate: ' + format8(rate) + '

' + messages.append('Sent Offer ' + offer_id.hex() + '
Rate: ' + format8(rate)) coins = [] - for k, v in swap_client.coin_clients.items(): if v['connection_type'] == 'rpc': coins.append((int(k), getCoinName(k))) - content += '
' - - content += '' - content += '' - content += '' - - content += '' - content += '' - content += '
Coin From' + self.make_coin_select('coin_from', coins) + 'Amount From
Coin To' + self.make_coin_select('coin_to', coins) + 'Amount To
Contract locked (hrs)Participate txn will be locked for half the time.
Auto Accept Bids
' - - content += '' - content += '
' - content += '

home

' - return bytes(content, 'UTF-8') + template = env.get_template('offer_new.html') + return bytes(template.render( + title=self.server.title, + h2=self.server.title, + messages=messages, + coins=coins, + form_id=os.urandom(8).hex(), + ), 'UTF-8') def page_offer(self, url_split, post_string): assert(len(url_split) > 2), 'Offer ID not specified' @@ -240,56 +224,54 @@ class HttpHandler(BaseHTTPRequestHandler): offer = swap_client.getOffer(offer_id) assert(offer), 'Unknown offer ID' - content = html_content_start(self.server.title, self.server.title) \ - + '

Offer: ' + offer_id.hex() + '

' - + messages = [] + sent_bid_id = None if post_string != '': form_data = urllib.parse.parse_qs(post_string) form_id = form_data[b'formid'][0].decode('utf-8') if self.server.last_form_id.get('offer', None) == form_id: - content += '

Prevented double submit for form {}.

'.format(form_id) + messages.append('Prevented double submit for form {}.'.format(form_id)) else: self.server.last_form_id['offer'] = form_id - bid_id = swap_client.postBid(offer_id, offer.amount_from) - content += '

Sent Bid ' + bid_id.hex() + '

' + sent_bid_id = swap_client.postBid(offer_id, offer.amount_from).hex() coin_from = Coins(offer.coin_from) coin_to = Coins(offer.coin_to) ticker_from = swap_client.getTicker(coin_from) ticker_to = swap_client.getTicker(coin_to) - - tr = '{}{}' - content += '' - content += tr.format('Offer State', getOfferState(offer.state)) - content += tr.format('Coin From', getCoinName(coin_from)) - content += tr.format('Coin To', getCoinName(coin_to)) - content += tr.format('Amount From', format8(offer.amount_from) + ' ' + ticker_from) - content += tr.format('Amount To', format8((offer.amount_from * offer.rate) // COIN) + ' ' + ticker_to) - content += tr.format('Rate', format8(offer.rate) + ' ' + ticker_from + '/' + ticker_to) - content += tr.format('Script Lock Type', getLockName(offer.lock_type)) - content += tr.format('Script Lock Value', offer.lock_value) - content += tr.format('Address From', offer.addr_from) - content += tr.format('Created At', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(offer.created_at))) - content += tr.format('Expired At', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(offer.expire_at))) - content += tr.format('Sent', 'True' if offer.was_sent else 'False') + data = { + 'tla_from': swap_client.getTicker(coin_from), + 'tla_to': swap_client.getTicker(coin_to), + 'state': getOfferState(offer.state), + 'coin_from': getCoinName(coin_from), + 'coin_to': getCoinName(coin_to), + 'amt_from': format8(offer.amount_from), + 'amt_to': format8((offer.amount_from * offer.rate) // COIN), + 'rate': format8(offer.rate), + 'lock_type': getLockName(offer.lock_type), + 'lock_value': offer.lock_value, + 'addr_from': offer.addr_from, + 'created_at': offer.created_at, + 'expired_at': offer.expire_at, + 'sent': 'True' if offer.was_sent else 'False' + } if offer.was_sent: - content += tr.format('Auto Accept Bids', 'True' if offer.auto_accept_bids else 'False') - content += '
' + data['auto_accept'] = 'True' if offer.auto_accept_bids else 'False' bids = swap_client.listBids(offer_id=offer_id) - content += '

Bids

' - content += '' - for b in bids: - content += ''.format(b.bid_id.hex(), format8(b.amount), getBidState(b.state), getTxState(b.initiate_txn_state), getTxState(b.participate_txn_state)) - content += '
Bid IDBid AmountBid StatusITX StatusPTX Status
{0}{1}{2}{3}{4}
' - - content += '
' - content += '' - content += '
' - content += '

home

' - return bytes(content, 'UTF-8') + template = env.get_template('offer.html') + return bytes(template.render( + title=self.server.title, + h2=self.server.title, + offer_id=offer_id.hex(), + sent_bid_id=sent_bid_id, + messages=messages, + data=data, + bids=[(b.bid_id.hex(), format8(b.amount), getBidState(b.state), getTxState(b.initiate_txn_state), getTxState(b.participate_txn_state)) for b in bids], + form_id=os.urandom(8).hex(), + ), 'UTF-8') def page_offers(self, url_split, sent=False): swap_client = self.server.swap_client diff --git a/basicswap/templates/offer.html b/basicswap/templates/offer.html new file mode 100644 index 0000000..7e27891 --- /dev/null +++ b/basicswap/templates/offer.html @@ -0,0 +1,48 @@ +{% include 'header.html' %} + +

Offer {{ offer_id }}

+{% if refresh %} +

Page Refresh: {{ refresh }} seconds

+{% endif %} + +{% for m in messages %} +

{{ m }}

+{% endfor %} + +{% if sent_bid_id %} +

Sent Bid {{ sent_bid_id }}

+{% endif %} + + + + + + + + + + + + + + +{% if data.sent == 'True' %} + +{% endif %} +
Offer State{{ data.state }}
Coin From{{ data.coin_from }}
Coin To{{ data.coin_to }}
Amount From{{ data.amt_from }} {{ data.tla_from }}
Amount To{{ data.amt_to }} {{ data.tla_to }}
Rate{{ data.rate }} {{ data.amt_from }}/{{ data.tla_from }}
Script Lock Type{{ data.lock_type }}
Script Lock Value{{ data.lock_value }}
Address From{{ data.addr_from }}
Created At{{ data.created_at | formatts }}
Expired At{{ data.expired_at | formatts }}
Sent{{ data.sent }}
Auto Accept Bids{{ data.auto_accept }}
+ +

Bids

+ + +{% for b in bids %} + +{% endfor %} +
Bid IDBid AmountBid StatusITX StatusPTX Status
{{ b[0] }}{{ b[1] }}{{ b[2] }}{{ b[3] }}{{ b[4] }}
+ +
+ + +
+ +

home

+ diff --git a/basicswap/templates/offer_new.html b/basicswap/templates/offer_new.html new file mode 100644 index 0000000..3f95738 --- /dev/null +++ b/basicswap/templates/offer_new.html @@ -0,0 +1,36 @@ +{% include 'header.html' %} + +

New Offer

+{% for m in messages %} +

{{ m }}

+{% endfor %} + +
+ + + + + + + + +
Coin From +' +Amount From
Coin To +' +Amount To
Contract locked (hrs)Participate txn will be locked for half the time.
Auto Accept Bids
+ + + +
+ +

home

+