switch to peewee orm

main
lza_menace 3 years ago
parent b2468bcb21
commit 0dd1ac0a2b

@ -10,8 +10,11 @@ setup: ## Establish local environment with dependencies installed
python3 -m venv .venv python3 -m venv .venv
.venv/bin/pip install -r requirements.txt .venv/bin/pip install -r requirements.txt
up: ## Build and run the required containers by fetching binaries up: ## Start containers
docker-compose -f docker-compose.yaml up -d docker-compose up -d
down: ## Stop containers
docker-compose down
shell: ## Start Quart CLI shell shell: ## Start Quart CLI shell
QUART_APP=app.py QUART_SECRETS=config.py QUART_DEBUG=0 QUART_ENV=production .venv/bin/quart shell QUART_APP=app.py QUART_SECRETS=config.py QUART_DEBUG=0 QUART_ENV=production .venv/bin/quart shell

@ -1,5 +1,3 @@
Flask
Flask-SQLAlchemy
Flask-WTF Flask-WTF
flask-login flask-login
hypercorn hypercorn
@ -8,6 +6,7 @@ psycopg2-binary
python-dotenv python-dotenv
qrcode qrcode
redis redis
peewee
requests requests
SQLAlchemy SQLAlchemy
WTForms WTForms

@ -2,10 +2,8 @@ import click
def cli(app): def cli(app):
@app.cli.command('init') @app.cli.command('echo')
def init(): def echo():
import xmrbackers.models click.echo('Hello world!')
from xmrbackers.factory import db
db.create_all()
return app return app

@ -1,29 +1,20 @@
import quart.flask_patch import quart.flask_patch
from quart import Quart from quart import Quart
from flask_sqlalchemy import SQLAlchemy
from xmrbackers.cli import cli from xmrbackers.cli import cli
from xmrbackers import config from xmrbackers import config
db = SQLAlchemy()
async def _setup_db(app: Quart): async def _setup_db(app: Quart):
uri = 'postgresql+psycopg2://{user}:{pw}@{host}:{port}/{db}'.format( import peewee
user=config.DB_USER, import xmrbackers.models
pw=config.DB_PASS, model = peewee.Model.__subclasses__()
host=config.DB_HOST, for m in model:
port=config.DB_PORT, m.create_table()
db=config.DB_NAME
)
app.config['SQLALCHEMY_DATABASE_URI'] = uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
def create_app(): def create_app():
app = Quart(__name__) app = Quart(__name__)
app.config.from_envvar('QUART_SECRETS') app.config.from_envvar('QUART_SECRETS')
app = cli(app)
@app.before_serving @app.before_serving
async def startup(): async def startup():
from xmrbackers.routes import meta, api from xmrbackers.routes import meta, api
@ -33,4 +24,4 @@ def create_app():
app.register_blueprint(api.bp) app.register_blueprint(api.bp)
app.register_blueprint(filters.bp) app.register_blueprint(filters.bp)
return app return cli(app)

@ -1,31 +1,45 @@
from datetime import datetime from datetime import datetime
from uuid import uuid4 from uuid import uuid4
from sqlalchemy.sql import func import peewee as pw
from peewee import PostgresqlDatabase, SQL, ForeignKeyField
from xmrbackers.factory import db
from xmrbackers import config from xmrbackers import config
db = PostgresqlDatabase(
config.DB_NAME,
user=config.DB_USER,
password=config.DB_PASS,
host=config.DB_HOST,
)
def rand_id(): def rand_id():
return uuid4().hex return uuid4().hex
class Creator(db.Model):
__tablename__ = 'creators'
id = db.Column(db.Integer, primary_key=True) class Creator(pw.Model):
# id = db.Column(db.String(80), primary_key=True, default=rand_id) # hex based id id = pw.AutoField()
register_date = db.Column(db.DateTime, server_default=func.now()) register_date = pw.DateTimeField(default=datetime.now)
last_login_date = db.Column(db.DateTime, nullable=True) last_login_date = pw.DateTimeField(default=datetime.now)
wallet_address = db.Column(db.String(150)) wallet_address = pw.CharField()
password = db.Column(db.String(150)) username = pw.CharField(unique=True)
email = db.Column(db.String(150)) email = pw.CharField(unique=True)
handle = db.Column(db.String(150)) password = pw.CharField(unique=True)
bio = pw.CharField()
class Meta:
database = db
def __repr__(self):
return self.id
class Backer(db.Model): class Backer(pw.Model):
__tablename__ = 'backers' id = pw.AutoField()
register_date = pw.DateTimeField(default=datetime.now)
last_login_date = pw.DateTimeField(default=datetime.now)
wallet_address = pw.CharField()
username = pw.CharField(unique=True)
email = pw.CharField(unique=True)
password = pw.CharField(unique=True)
id = db.Column(db.Integer, primary_key=True) class Meta:
database = db

Loading…
Cancel
Save