hack: pull changes from old rpi

master
q3k 2020-02-23 10:57:44 +01:00
parent 4459130370
commit 9ea05b5c70
8 changed files with 69 additions and 23 deletions

View File

@ -1,6 +1,7 @@
from flask import Blueprint, render_template, redirect, request, flash, url_for
from flask_login import current_user, fresh_login_required
from bitvend import dev
from bitvend.models import db, Transaction
from bitvend.forms import ManualForm
from spaceauth import cap_required
@ -31,3 +32,21 @@ def transactions(page):
return render_template('admin/transactions.html',
transactions=Transaction.query.paginate(page)
)
@bp.route('/begin')
@fresh_login_required
@admin_required
def begin():
dev.begin_session(500)
flash('Operation successful.', 'success')
return redirect('/')
@bp.route('/cancel')
@fresh_login_required
@admin_required
def cancel():
dev.cancel_session()
flash('Operation successful.', 'success')
return redirect('/')

View File

@ -40,4 +40,4 @@ ITEMS = [
},
]
DEBT_LIMIT = 2500
DEBT_LIMIT = 1500

View File

@ -45,5 +45,4 @@ class TransferForm(FlaskForm):
class ManualForm(FlaskForm):
amount = DecimalUnityField("Amount", default=0, validators=[
NumberRange(min=1),
])

View File

@ -77,10 +77,10 @@
{% endif %}
<p class="navbar-text navbar-right">
1zł = {{ format_btc(from_local_currency(100)) }}
1zł = {{ format_btc(from_local_currency(100, True)) }}
</p>
<p class="navbar-text navbar-right">
<b>Rate:</b> {{ to_local_currency(100000000) / 100 }}zł
<b>Rate:</b> {{ to_local_currency(100000000, True) / 100 }}zł
</p>
</div>

View File

@ -53,8 +53,8 @@
<thead><tr>
<th>Name</th><th class="text-right">Balance</th>
</tr></thead>
{% for user in hallofshame %}
<tr><td>{{ user }}</td><td class="text-right">{{ format_currency(user.balance) }}</td></tr>
{% for user, balance in hallofshame %}
<tr><td>{{ user }}</td><td class="text-right">{{ format_currency(balance) }}</td></tr>
{% else %}
<tr><td colspan=2 class="placeholder">Wow! Nobody's due!</td></tr>
{% endfor %}
@ -65,8 +65,8 @@
<thead><tr>
<th>Name</th><th class="text-right">Amount</th><th>Purchases</th>
</tr></thead>
{% for user in hallofaddicts %}
<tr><td>{{ user }}</td><td class="text-right">{{ format_currency(user.purchase_amount) }}</td><td>{{ user.purchase_count }}</td></tr>
{% for user, purchase_amount, purchase_count in hallofaddicts %}
<tr><td>{{ user }}</td><td class="text-right">{{ format_currency(purchase_amount) }}</td><td>{{ purchase_count }}</td></tr>
{% else %}
<tr><td colspan=3 class="placeholder">Huh?</td></tr>
{% endfor %}
@ -132,7 +132,7 @@
<div class="col-md-12">
<div class="pull-right">
<span class="label label-info">{{ format_currency(item.value) }}</span>
<span class="label label-primary">{{ format_btc(from_local_currency(item.value*1.03)) }}</span>
<span class="label label-primary">{{ format_btc(from_local_currency(item.value*1.03, True)) }}</span>
</div>
<h3>{{ item.name }}</h3>
</div>
@ -140,7 +140,7 @@
<img src="{{ item.image }}" class="img-responsive center-block" />
</div>
<div class="col-xs-6 text-center">
{% with btc_uri = 'bitcoin:%s?amount=%s' % (config['INPUT_ADDRESS'], sat_to_btc(from_local_currency(item.value*1.03))) %}
{% with btc_uri = 'bitcoin:%s?amount=%s' % (config['INPUT_ADDRESS'], sat_to_btc(from_local_currency(item.value*1.03, True))) %}
<a href="{{ btc_uri }}">
<img src="{{ qrcode(btc_uri) }}" class="img-responsive center-block"/>
<code><small>{{ config['INPUT_ADDRESS'] }}</small></code>

View File

@ -6,14 +6,25 @@ def get_exchange_rate(currency='PLN'):
# Returns current exchange rate for selected currency
return requests.get('https://blockchain.info/pl/ticker').json()[currency]['last']
def to_local_currency(sat):
def to_local_currency(sat, safe=False):
# Returns satoshi in local lowest denomination currency (grosze)
rate = get_exchange_rate()
try:
rate = get_exchange_rate()
except:
if safe:
return 0
raise
return int(sat / 1000000.0 * rate)
def from_local_currency(val):
def from_local_currency(val, safe=False):
# Returns satoshi value from local currency
rate = get_exchange_rate()
try:
rate = get_exchange_rate()
except:
if safe:
return 0
raise
return int(val / rate * 1000000)
def sat_to_btc(amount):

View File

@ -19,12 +19,14 @@ bp = Blueprint('bitvend', __name__, template_folder='templates')
def index():
transactions = []
hallofshame = User.query \
.with_entities(User, User.balance) \
.order_by(User.balance.asc()) \
.filter(User.balance < 0) \
.limit(5) \
.all()
hallofaddicts = User.query \
.with_entities(User, User.purchase_amount, User.purchase_count) \
.order_by(User.purchase_amount.desc()) \
.filter(User.purchase_amount > 0) \
.limit(5) \

View File

@ -7,6 +7,12 @@ try:
except ImportError:
import Queue as queue
try:
import cygpio
except ImportError:
raise
cygpio = None
from mdb.utils import compute_checksum, compute_chk, bcd_decode
from mdb.constants import *
from mdb.backend import RaspiBackend, DummyBackend, pigpio
@ -18,6 +24,10 @@ class MDBRequest(object):
processed = False
def __init__(self, command):
self.reset(command)
def reset(self, command):
self.processed = False
self.timestamp = time.time()
self.command = command
self.data = bytearray()
@ -41,7 +51,7 @@ class MDBRequest(object):
return False
@property
def ack(self):
return self.data[-1] == 0x00
return len(self.data) and self.data[-1] == 0x00
def __repr__(self):
return '<MDBRequest 0x%02x [%s] chk:%r>' % (
@ -60,31 +70,36 @@ class MDBDevice(object):
def __init__(self, app=None):
self.logger = logging.getLogger(type(self).__name__)
self.poll_queue = queue.Queue()
if pigpio:
if cygpio:
self.backend = cygpio.CythonRaspiBackend()
self.logger.warning('Running with FAST CYTHON BACKEND')
elif pigpio:
self.backend = RaspiBackend()
else:
self.logger.warning('Running with dummy backend device')
self.backend = DummyBackend()
def initialize(self):
self.logger.info('Initializing...')
self.logger.info('Initializing... %r backend', self.backend)
self.backend.open()
# here should IO / connection initizliation go
def run(self):
self.initialize()
self.current_request = MDBRequest(0)
self.current_request.processed = True
while True:
data = self.backend.read()
for b in range(0, len(data), 2):
if data[b+1]:
if self.current_request: # and not self.current_request.processed:
self.logger.debug(self.current_request)
if self.current_request.processed and self.current_request.ack:
self.logger.info('Got response: %d',self.current_request.data[-1])
self.poll_msg = []
self.logger.debug(self.current_request)
if self.current_request.processed and self.current_request.ack:
self.logger.info('Got response: %d',self.current_request.data[-1])
self.poll_msg = []
self.current_request = MDBRequest(data[b])
self.current_request.reset(data[b])
self.send_buffer = None
elif self.current_request:
if self.current_request.processed and data[b] == 0xaa and self.send_buffer: