class RestApp(Flask): def __init__(self): super().__init__(__name__) # TODO: apply proper CORS values CORS(self) config = get_config() self.config.from_object(config) self.url_map.strict_slashes = False self.app_context().push() self.rebar = Rebar() self.rebar.add_handler_registry(version_1_registry) self.rebar.init_app(app=self) self.flask_marshmallow = FlaskMarshmallow(app=self) self.secret_key = config.API_SECRET_KEY self.login_manager = LoginManager() self.login_manager.init_app(app=self) self.login_manager.unauthorized_callback = raise_unauthorized self._init_logger() self._init_db_client() def _init_logger(self): base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) logging.config.fileConfig(f"{base_dir}/logging.conf") self.custom_logger = logging.getLogger("barter") self.custom_logger.setLevel( logging.DEBUG if self.config["DEBUG"] else logging.INFO) def _init_db_client(self): supported_models = () sql_db_name = self.config["POSTGRESQL_DB_NAME"] sql_host = self.config["POSTGRESQL_DB_HOST"] sql_username = self.config["POSTGRESQL_DB_USERNAME"] self.custom_logger.info( f"Binding models to PSQL DB {sql_db_name} from " f"{sql_username}@{sql_host}...") self.postgre_client = PostgresqlDatabase( sql_db_name, host=sql_host, port=self.config["POSTGRESQL_DB_PORT"], user=sql_username, password=self.config["POSTGRESQL_DB_PASSWORD"], ) self.postgre_client.bind(supported_models, bind_refs=False, bind_backrefs=False)
def _init_db_client(self): supported_models = () sql_db_name = self.config["POSTGRESQL_DB_NAME"] sql_host = self.config["POSTGRESQL_DB_HOST"] sql_username = self.config["POSTGRESQL_DB_USERNAME"] self.custom_logger.info( f"Binding models to PSQL DB {sql_db_name} from " f"{sql_username}@{sql_host}...") self.postgre_client = PostgresqlDatabase( sql_db_name, host=sql_host, port=self.config["POSTGRESQL_DB_PORT"], user=sql_username, password=self.config["POSTGRESQL_DB_PASSWORD"], ) self.postgre_client.bind(supported_models, bind_refs=False, bind_backrefs=False)
def test_db_closure(*args, **kwargs): test_db = PostgresqlDatabase(SQL_DATABASE, user=SQL_USER, password=SQL_PASSWORD, host=SQL_HOST) with test_db.bind_ctx(models): test_db.create_tables(models) try: func(*args, **kwargs) finally: test_db.drop_tables(models)
from playhouse.postgres_ext import PostgresqlDatabase from bot4likes.config import db_database, db_user, db_password, db_host database = PostgresqlDatabase(db_database, user=db_user, password=db_password, host=db_host) # TODO ADD AUTO MIGRATIONS
import inspect from datetime import datetime from playhouse.postgres_ext import Model, PostgresqlDatabase from config import get_config config = get_config() postgre_client = PostgresqlDatabase( config.POSTGRESQL_DB_NAME, host=config.POSTGRESQL_DB_HOST, port=config.POSTGRESQL_DB_PORT, user=config.POSTGRESQL_DB_USERNAME, password=config.POSTGRESQL_DB_PASSWORD, ) def ensure_connection(func): def decorated(cls, *args, **kwargs): if cls._meta.database.in_transaction(): return func(cls, *args, **kwargs) cls._meta.database.connect(reuse_if_open=True) with cls._meta.database.connection_context(): returned_value = func(cls, *args, **kwargs) return returned_value return decorated
def init_db(app): db = PostgresqlDatabase(**app.config["DB"]) db_proxy.initialize(db)
import os import peewee as pw import datetime from playhouse.postgres_ext import PostgresqlDatabase db = PostgresqlDatabase(os.getenv('DATABASE')) class BaseModel(pw.Model): created_at = pw.DateTimeField(default=datetime.datetime.now) updated_at = pw.DateTimeField(default=datetime.datetime.now) # new save def save(self, *args, **kwargs): self.errors = [] self.validate() if len(self.errors) == 0: self.updated_at = datetime.datetime.now() # inherits save() from Model return super(BaseModel, self).save(*args, **kwargs) else: return 0 def validate(self): print("validate() is not implemented for this class") class Meta: database = db legacy_table_names = False