summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bitvend/admin.py19
-rw-r--r--bitvend/default_settings.py2
-rw-r--r--bitvend/forms.py1
-rw-r--r--bitvend/templates/base.html4
-rw-r--r--bitvend/templates/index.html12
-rw-r--r--bitvend/utils.py19
-rw-r--r--bitvend/views.py2
-rw-r--r--mdb/device.py33
8 files changed, 69 insertions, 23 deletions
diff --git a/bitvend/admin.py b/bitvend/admin.py
index c803181..e36412e 100644
--- a/bitvend/admin.py
+++ b/bitvend/admin.py
@@ -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('/')
diff --git a/bitvend/default_settings.py b/bitvend/default_settings.py
index 5536877..89ad248 100644
--- a/bitvend/default_settings.py
+++ b/bitvend/default_settings.py
@@ -40,4 +40,4 @@ ITEMS = [
},
]
-DEBT_LIMIT = 2500
+DEBT_LIMIT = 1500
diff --git a/bitvend/forms.py b/bitvend/forms.py
index 203b3d5..84805e1 100644
--- a/bitvend/forms.py
+++ b/bitvend/forms.py
@@ -45,5 +45,4 @@ class TransferForm(FlaskForm):
class ManualForm(FlaskForm):
amount = DecimalUnityField("Amount", default=0, validators=[
- NumberRange(min=1),
])
diff --git a/bitvend/templates/base.html b/bitvend/templates/base.html
index 3a8b49b..4194b94 100644
--- a/bitvend/templates/base.html
+++ b/bitvend/templates/base.html
@@ -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>
diff --git a/bitvend/templates/index.html b/bitvend/templates/index.html
index 1960b67..5f63e7e 100644
--- a/bitvend/templates/index.html
+++ b/bitvend/templates/index.html
@@ -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>
diff --git a/bitvend/utils.py b/bitvend/utils.py
index d360477..e761f4a 100644
--- a/bitvend/utils.py
+++ b/bitvend/utils.py
@@ -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):
diff --git a/bitvend/views.py b/bitvend/views.py
index 7e424cb..23e8147 100644
--- a/bitvend/views.py
+++ b/bitvend/views.py
@@ -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) \
diff --git a/mdb/device.py b/mdb/device.py
index 20ea546..9ea0e6e 100644
--- a/mdb/device.py
+++ b/mdb/device.py
@@ -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: