2020-08-25 20:48:50 +01:00
|
|
|
from flask import Flask
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2020-09-11 21:59:48 +01:00
|
|
|
from flask_wtf.csrf import CSRFProtect
|
2020-08-25 20:48:50 +01:00
|
|
|
from flask_session import Session
|
|
|
|
from flask_bcrypt import Bcrypt
|
2020-08-27 06:50:08 +01:00
|
|
|
from flask_login import LoginManager
|
2020-08-25 20:48:50 +01:00
|
|
|
from redis import Redis
|
2020-08-30 20:27:35 +01:00
|
|
|
from datetime import datetime
|
2020-08-25 20:48:50 +01:00
|
|
|
from wowstash import config
|
|
|
|
|
|
|
|
|
|
|
|
app = None
|
|
|
|
db = None
|
|
|
|
bcrypt = None
|
|
|
|
|
|
|
|
def _setup_db(app: Flask):
|
|
|
|
global db
|
2020-09-11 21:59:48 +01:00
|
|
|
uri = 'postgresql+psycopg2://{user}:{pw}@{host}:{port}/{db}'.format(
|
2020-08-25 20:48:50 +01:00
|
|
|
user=config.DB_USER,
|
|
|
|
pw=config.DB_PASS,
|
2020-09-11 21:59:48 +01:00
|
|
|
host=config.DB_HOST,
|
|
|
|
port=config.DB_PORT,
|
2020-08-25 20:48:50 +01:00
|
|
|
db=config.DB_NAME
|
|
|
|
)
|
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = uri
|
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
db = SQLAlchemy(app)
|
2020-08-25 21:14:30 +01:00
|
|
|
import wowstash.models
|
2020-08-25 20:48:50 +01:00
|
|
|
db.create_all()
|
|
|
|
|
|
|
|
def _setup_session(app: Flask):
|
|
|
|
app.config['SESSION_REDIS'] = Redis(
|
|
|
|
host=app.config['REDIS_HOST'],
|
|
|
|
port=app.config['REDIS_PORT']
|
|
|
|
)
|
|
|
|
Session(app)
|
|
|
|
|
|
|
|
def _setup_bcrypt(app: Flask):
|
2020-08-25 21:14:30 +01:00
|
|
|
global bcrypt
|
2020-08-25 20:48:50 +01:00
|
|
|
bcrypt = Bcrypt(app)
|
|
|
|
|
|
|
|
def create_app():
|
|
|
|
global app
|
|
|
|
global db
|
2020-08-27 06:50:08 +01:00
|
|
|
global bcrypt
|
|
|
|
global login_manager
|
2020-08-25 20:48:50 +01:00
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_envvar('FLASK_SECRETS')
|
|
|
|
app.secret_key = app.config['SECRET_KEY']
|
|
|
|
|
|
|
|
# Setup backends
|
|
|
|
_setup_db(app)
|
|
|
|
_setup_session(app)
|
|
|
|
_setup_bcrypt(app)
|
2020-09-11 21:59:48 +01:00
|
|
|
CSRFProtect(app)
|
2020-08-25 20:48:50 +01:00
|
|
|
|
2020-08-27 06:50:08 +01:00
|
|
|
login_manager = LoginManager()
|
|
|
|
login_manager.init_app(app)
|
2020-08-27 09:06:38 +01:00
|
|
|
login_manager.login_view = 'auth.login'
|
2020-08-27 06:50:08 +01:00
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(user_id):
|
|
|
|
from wowstash.models import User
|
2020-09-11 21:59:48 +01:00
|
|
|
user = User.query.get(user_id)
|
|
|
|
return user
|
2020-08-27 06:50:08 +01:00
|
|
|
|
2020-08-30 20:27:35 +01:00
|
|
|
# template filters
|
|
|
|
@app.template_filter('datestamp')
|
|
|
|
def datestamp(s):
|
|
|
|
d = datetime.fromtimestamp(s)
|
|
|
|
return d.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
2020-09-11 21:59:48 +01:00
|
|
|
# commands
|
|
|
|
@app.cli.command('send_transfers')
|
|
|
|
def send_transfers():
|
|
|
|
from wowstash.models import Transaction, User
|
|
|
|
from wowstash.library.jsonrpc import wallet
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
txes = Transaction.query.filter_by(sent=False)
|
|
|
|
for tx in txes:
|
|
|
|
user = User.query.get(tx.from_user)
|
|
|
|
new_tx = wallet.transfer(
|
|
|
|
0, user.subaddress_index, tx.address, Decimal(tx.amount)
|
|
|
|
)
|
|
|
|
if 'message' in new_tx:
|
|
|
|
print(f'Failed to send tx {tx.id}. Reason: {new_tx["message"]}')
|
|
|
|
else:
|
|
|
|
tx.sent = True
|
|
|
|
user.funds_locked = False
|
|
|
|
db.session.commit()
|
|
|
|
print(f'Successfully sent tx! {new_tx}')
|
|
|
|
|
2020-08-25 20:48:50 +01:00
|
|
|
# Routes
|
2020-08-27 09:06:38 +01:00
|
|
|
from wowstash.blueprints.auth import auth_bp
|
2020-08-27 06:50:08 +01:00
|
|
|
from wowstash.blueprints.wallet import wallet_bp
|
2020-08-25 21:14:30 +01:00
|
|
|
from wowstash.blueprints.meta import meta_bp
|
2020-08-25 20:48:50 +01:00
|
|
|
app.register_blueprint(meta_bp)
|
2020-08-27 09:06:38 +01:00
|
|
|
app.register_blueprint(auth_bp)
|
2020-08-27 06:50:08 +01:00
|
|
|
app.register_blueprint(wallet_bp)
|
2020-08-25 20:48:50 +01:00
|
|
|
|
|
|
|
app.app_context().push()
|
|
|
|
return app
|