- Switch to integrated addresses (address+payment_id) -> `make_integrated_address @ RPC)

- Changed homegrown caching solution to use flask extension `flask_cache` instead
- Remove detection of out-going (sent) funds, needs to be manually registered in table "payouts" now
- Updated Flask version
This commit is contained in:
dsc 2020-06-09 04:32:51 +02:00 committed by dsc
parent c0b972edde
commit 401c26e85b
18 changed files with 311 additions and 308 deletions

View file

@ -34,8 +34,8 @@ class QrCodeGenerator:
:param color_to: gradient to color
:return:
"""
if len(address) != settings.COIN_ADDRESS_LENGTH:
raise Exception('faulty address length')
if len(address) not in settings.COIN_ADDRESS_LENGTH:
raise Exception(f'faulty address length, should be: {" or ".join(map(str, settings.COIN_ADDRESS_LENGTH))}')
if not dest:
dest = os.path.join(self.base, '%s.png' % address)

View file

@ -8,6 +8,7 @@ from flask import g, request
from flask.json import JSONEncoder
import settings
from funding.factory import cache
def json_encoder(obj):
@ -18,62 +19,50 @@ def json_encoder(obj):
class Summary:
@staticmethod
@cache.cached(timeout=300, key_prefix="fetch_prices")
def fetch_prices():
if hasattr(g, 'funding_prices') and g.coin_prices:
return g.coin_prices
from funding.factory import cache
cache_key = 'funding_prices'
data = cache.get(cache_key)
if data:
return data
data = {
return {
'coin-btc': coin_btc_value(),
'btc-usd': price_cmc_btc_usd()
}
cache.set(cache_key, data=data, expiry=1200)
g.coin_prices = data
return data
@staticmethod
def fetch_stats(purge=False):
from funding.factory import db_session
@cache.cached(timeout=300, key_prefix="funding_stats")
def fetch_stats():
from funding.factory import db
from funding.orm.orm import Proposal, User, Comment
from funding.factory import cache
cache_key = 'funding_stats'
data = cache.get(cache_key)
if data and not purge:
return data
data = {}
categories = settings.FUNDING_CATEGORIES
statuses = settings.FUNDING_STATUSES.keys()
for cat in categories:
q = db_session.query(Proposal)
q = db.session.query(Proposal)
q = q.filter(Proposal.category == cat)
res = q.count()
data.setdefault('cats', {})
data['cats'][cat] = res
for status in statuses:
q = db_session.query(Proposal)
q = db.session.query(Proposal)
q = q.filter(Proposal.status == status)
res = q.count()
data.setdefault('statuses', {})
data['statuses'][status] = res
data.setdefault('users', {})
data['users']['count'] = db_session.query(User.id).count()
cache.set(cache_key, data=data, expiry=300)
data['users']['count'] = db.session.query(User.id).count()
return data
def price_cmc_btc_usd():
headers = {'User-Agent': 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0'}
try:
print('request coinmarketcap')
r = requests.get('https://api.coinmarketcap.com/v2/ticker/1/?convert=USD', headers=headers)
r = requests.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd', headers=headers)
r.raise_for_status()
return r.json().get('data', {}).get('quotes', {}).get('USD', {}).get('price')
data = r.json()
btc = next(c for c in data if c['symbol'] == 'btc')
return btc['current_price']
except:
return
@ -81,7 +70,6 @@ def price_cmc_btc_usd():
def coin_btc_value():
headers = {'User-Agent': 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0'}
try:
print('request TO')
r = requests.get('https://tradeogre.com/api/v1/ticker/BTC-WOW', headers=headers)
r.raise_for_status()
return float(r.json().get('high'))

View file

@ -2,16 +2,16 @@ from datetime import datetime
from flask import session, g, request
import settings
from funding.bin.utils import Summary
from funding.factory import app, db_session
from funding.factory import app, db
from funding.orm.orm import Proposal, User, Comment
@app.context_processor
def templating():
from flask.ext.login import current_user
recent_comments = db_session.query(Comment).filter(Comment.automated == False).order_by(Comment.date_added.desc()).limit(8).all()
from flask_login import current_user
recent_comments = db.session.query(Comment).filter(Comment.automated == False).order_by(Comment.date_added.desc()).limit(8).all()
summary_data = Summary.fetch_stats()
newest_users = db_session.query(User).filter(User.admin == False).order_by(User.registered_on.desc()).limit(5).all()
newest_users = db.session.query(User).filter(User.admin == False).order_by(User.registered_on.desc()).limit(5).all()
return dict(logged_in=current_user.is_authenticated,
current_user=current_user,
funding_categories=settings.FUNDING_CATEGORIES,
@ -28,8 +28,6 @@ def before_request():
@app.after_request
def after_request(res):
if hasattr(g, 'funding_prices'):
delattr(g, 'funding_prices')
res.headers.add('Accept-Ranges', 'bytes')
if request.full_path.startswith('/api/'):
@ -45,7 +43,7 @@ def after_request(res):
@app.teardown_appcontext
def shutdown_session(**kwargs):
db_session.remove()
db.session.remove()
@app.errorhandler(404)