switch to peewee orm

main
lza_menace 4 years ago
parent b2468bcb21
commit 0dd1ac0a2b

@ -10,8 +10,11 @@ setup: ## Establish local environment with dependencies installed
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
up: ## Build and run the required containers by fetching binaries
docker-compose -f docker-compose.yaml up -d
up: ## Start containers
docker-compose up -d
down: ## Stop containers
docker-compose down
shell: ## Start Quart CLI 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-login
hypercorn
@ -8,6 +6,7 @@ psycopg2-binary
python-dotenv
qrcode
redis
peewee
requests
SQLAlchemy
WTForms

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

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

@ -1,31 +1,45 @@
from datetime import datetime
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
db = PostgresqlDatabase(
config.DB_NAME,
user=config.DB_USER,
password=config.DB_PASS,
host=config.DB_HOST,
)
def rand_id():
return uuid4().hex
class Creator(db.Model):
__tablename__ = 'creators'
id = db.Column(db.Integer, primary_key=True)
# id = db.Column(db.String(80), primary_key=True, default=rand_id) # hex based id
register_date = db.Column(db.DateTime, server_default=func.now())
last_login_date = db.Column(db.DateTime, nullable=True)
wallet_address = db.Column(db.String(150))
password = db.Column(db.String(150))
email = db.Column(db.String(150))
handle = db.Column(db.String(150))
class Creator(pw.Model):
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)
bio = pw.CharField()
class Meta:
database = db
def __repr__(self):
return self.id
class Backer(db.Model):
__tablename__ = 'backers'
class Backer(pw.Model):
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