From 0dd1ac0a2b94089e63b2afaea7cb56123570fb58 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Sun, 27 Jun 2021 16:18:08 -0700 Subject: [PATCH] switch to peewee orm --- Makefile | 7 +++++-- requirements.txt | 3 +-- xmrbackers/cli.py | 8 +++----- xmrbackers/factory.py | 21 ++++++------------- xmrbackers/models.py | 48 ++++++++++++++++++++++++++++--------------- 5 files changed, 46 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 4b46f33..6b843c7 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/requirements.txt b/requirements.txt index d2c404b..6f3758e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/xmrbackers/cli.py b/xmrbackers/cli.py index 6a8cbe1..1f2b778 100644 --- a/xmrbackers/cli.py +++ b/xmrbackers/cli.py @@ -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 diff --git a/xmrbackers/factory.py b/xmrbackers/factory.py index 93a48c6..98a88e9 100644 --- a/xmrbackers/factory.py +++ b/xmrbackers/factory.py @@ -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) diff --git a/xmrbackers/models.py b/xmrbackers/models.py index 4a07113..73cb7ec 100644 --- a/xmrbackers/models.py +++ b/xmrbackers/models.py @@ -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