mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-06 02:38:11 +01:00
html: More offer filtering.
This commit is contained in:
@@ -54,6 +54,8 @@ from .db import (
|
||||
SentOffer,
|
||||
SmsgAddress,
|
||||
)
|
||||
|
||||
from .explorers import ExplorerInsight, ExplorerBitAps, ExplorerChainz
|
||||
import basicswap.config as cfg
|
||||
import basicswap.segwit_addr as segwit_addr
|
||||
|
||||
@@ -361,6 +363,24 @@ class BasicSwap():
|
||||
for c in Coins:
|
||||
self.setCoinConnectParams(c)
|
||||
|
||||
if self.chain == 'mainnet':
|
||||
self.coin_clients[Coins.PART]['explorers'].append(ExplorerInsight(
|
||||
self,
|
||||
'https://explorer.particl.io/particl-insight-api/'))
|
||||
self.coin_clients[Coins.LTC]['explorers'].append(ExplorerBitAps(
|
||||
self,
|
||||
'https://api.bitaps.com/ltc/v1/blockchain'))
|
||||
self.coin_clients[Coins.LTC]['explorers'].append(ExplorerChainz(
|
||||
self,
|
||||
'http://chainz.cryptoid.info/ltc/api.dws'))
|
||||
elif self.chain == 'testnet':
|
||||
self.coin_clients[Coins.PART]['explorers'].append(ExplorerInsight(
|
||||
self,
|
||||
'https://explorer-testnet.particl.io/particl-insight-api'))
|
||||
self.coin_clients[Coins.LTC]['explorers'].append(ExplorerBitAps(
|
||||
self,
|
||||
'https://api.bitaps.com/ltc/testnet/v1/blockchain'))
|
||||
|
||||
def prepareLogging(self):
|
||||
self.log = logging.getLogger(self.log_name)
|
||||
self.log.propagate = False
|
||||
@@ -425,9 +445,13 @@ class BasicSwap():
|
||||
'use_csv': chain_client_settings.get('use_csv', True),
|
||||
'core_version_group': chain_client_settings.get('core_version_group', 0),
|
||||
'pid': None,
|
||||
'explorers': [],
|
||||
}
|
||||
|
||||
def setDaemonPID(self, name, pid):
|
||||
if isinstance(name, Coins):
|
||||
self.coin_clients[name]['pid'] = pid
|
||||
return
|
||||
for c, v in self.coin_clients.items():
|
||||
if v['name'] == name:
|
||||
v['pid'] = pid
|
||||
@@ -2379,7 +2403,12 @@ class BasicSwap():
|
||||
if filter_coin_to and filter_coin_to > -1:
|
||||
q = q.filter(Offer.coin_to == int(filter_coin_to))
|
||||
|
||||
q = q.order_by(Offer.created_at.desc())
|
||||
order_dir = filters.get('sort_dir', 'desc')
|
||||
order_by = filters.get('order_by', 'created_at')
|
||||
if order_by == 'created_at':
|
||||
q = q.order_by(Offer.created_at.desc() if order_dir == 'desc' else Offer.created_at.asc())
|
||||
elif order_by == 'rate':
|
||||
q = q.order_by(Offer.rate.desc() if order_dir == 'desc' else Offer.rate.asc())
|
||||
|
||||
limit = filters.get('limit', None)
|
||||
if limit is not None:
|
||||
|
||||
42
basicswap/explorers.py
Normal file
42
basicswap/explorers.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2019 tecnovert
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file LICENSE.txt or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
import urllib.request
|
||||
import json
|
||||
|
||||
|
||||
class Explorer():
|
||||
def __init__(self, swapclient, base_url):
|
||||
self.swapclient = swapclient
|
||||
self.base_url = base_url
|
||||
self.log = self.swapclient.log
|
||||
|
||||
|
||||
class ExplorerInsight(Explorer):
|
||||
def getChainHeight(self):
|
||||
return json.loads(urllib.request.urlopen(self.base_url + '/sync').read())['blockChainHeight']
|
||||
|
||||
def lookupUnspentByAddress(self, address):
|
||||
chain_height = self.getChainHeight()
|
||||
self.log.debug('[rm] chain_height %d', chain_height)
|
||||
|
||||
|
||||
class ExplorerBitAps(Explorer):
|
||||
def getChainHeight(self):
|
||||
return json.loads(urllib.request.urlopen(self.base_url + '/block/last').read())['data']['block']['height']
|
||||
|
||||
def lookupUnspentByAddress(self, address):
|
||||
chain_height = self.getChainHeight()
|
||||
self.log.debug('[rm] chain_height %d', chain_height)
|
||||
|
||||
|
||||
class ExplorerChainz(Explorer):
|
||||
def getChainHeight(self):
|
||||
return int(urllib.request.urlopen(self.base_url + '?q=getblockcount').read())
|
||||
|
||||
def lookupUnspentByAddress(self, address):
|
||||
chain_height = self.getChainHeight()
|
||||
self.log.debug('[rm] chain_height %d', chain_height)
|
||||
@@ -45,6 +45,7 @@ def format_timestamp(value):
|
||||
|
||||
env = Environment(loader=PackageLoader('basicswap', 'templates'))
|
||||
env.filters['formatts'] = format_timestamp
|
||||
PAGE_LIMIT = 50
|
||||
|
||||
|
||||
def getCoinName(c):
|
||||
@@ -373,6 +374,10 @@ class HttpHandler(BaseHTTPRequestHandler):
|
||||
filters = {
|
||||
'coin_from': -1,
|
||||
'coin_to': -1,
|
||||
'page_no': 1,
|
||||
'limit': PAGE_LIMIT,
|
||||
'sort_by': 'created_at',
|
||||
'sort_dir': 'desc',
|
||||
}
|
||||
messages = []
|
||||
form_data = self.checkForm(post_string, 'offers', messages)
|
||||
@@ -390,6 +395,25 @@ class HttpHandler(BaseHTTPRequestHandler):
|
||||
except Exception:
|
||||
raise ValueError('Unknown Coin From')
|
||||
|
||||
if b'sort_by' in form_data:
|
||||
sort_by = form_data[b'sort_by'][0].decode('utf-8')
|
||||
assert(sort_by in ['created_at', 'rate']), 'Invalid sort by'
|
||||
filters['sort_by'] = sort_by
|
||||
if b'sort_dir' in form_data:
|
||||
sort_dir = form_data[b'sort_dir'][0].decode('utf-8')
|
||||
assert(sort_dir in ['asc', 'desc']), 'Invalid sort dir'
|
||||
filters['sort_dir'] = sort_dir
|
||||
|
||||
if form_data and b'pageback' in form_data:
|
||||
filters['page_no'] = int(form_data[b'pageno'][0]) - 1
|
||||
if filters['page_no'] < 1:
|
||||
filters['page_no'] = 1
|
||||
if form_data and b'pageforwards' in form_data:
|
||||
filters['page_no'] = int(form_data[b'pageno'][0]) + 1
|
||||
|
||||
if filters['page_no'] > 1:
|
||||
filters['offset'] = (filters['page_no'] - 1) * PAGE_LIMIT
|
||||
|
||||
offers = swap_client.listOffers(sent, filters)
|
||||
|
||||
template = env.get_template('offers.html')
|
||||
|
||||
@@ -25,9 +25,22 @@
|
||||
</select>
|
||||
</td></tr>
|
||||
|
||||
<tr><td>Sort By</td><td>
|
||||
<select name="sort_by">
|
||||
<option value="created_at"{% if filters.sort_by=='created_at' %} selected{% endif %}>Created At</option>
|
||||
<option value="rate"{% if filters.sort_by=='rate' %} selected{% endif %}>Rate</option>
|
||||
</select>
|
||||
<select name="sort_dir">
|
||||
<option value="asc"{% if filters.sort_dir=='asc' %} selected{% endif %}>Ascending</option>
|
||||
<option value="desc"{% if filters.sort_dir=='desc' %} selected{% endif %}>Descending</option>
|
||||
</select>
|
||||
</td></tr>
|
||||
|
||||
<tr><td><input type="submit" name='applyfilters' value="Apply Filters"></td><td><input type="submit" name='clearfilters' value="Clear Filters"></td></tr>
|
||||
<tr><td><input type="submit" name='pageback' value="Page Back"></td><td>Page: {{ filters.page_no }}</td><td><input type="submit" name='pageforwards' value="Page Forwards"></td></tr>
|
||||
</table>
|
||||
<input type="hidden" name="formid" value="{{ form_id }}">
|
||||
<input type="hidden" name="pageno" value="{{ filters.page_no }}">
|
||||
</form>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user