# builtins import time # third-party import peewee import playhouse.db_url # internals from application import app import util import rendering # NOTE: autorollback is deprecated in most current release of peewee, but development is done with 3.15.0 app.db = playhouse.db_url.connect(app.config['DATABASE'], autorollback=True) @app.before_request def db_connect(): try: app.db.connect(reuse_if_open=True) app.db.set_time_zone('UTC') except peewee.OperationalError as e: app.logger.error(f"Error connecting to database: {str(e)}") class ModelMeta(peewee.ModelBase, util.ChildAwareMeta): pass @app.abstract class Model(peewee.Model, util.ChildAware, metaclass=ModelMeta): class Meta: database = app.db def __init_subclass__(cls): # this makes subclasses sortable for use in installation. # as long as model classes are defined in the right order # in here the installation will have no trouble with # foreign keys to tables that don't yet exist. cls.__class_created__ = time.time() @app.abstract class RenderableModel(rendering.Renderable, Model): def __init__(self, *args, extra_classes=None, **kwargs): # we need the model handling from peewee super(Model, self).__init__(*args, **kwargs) # peewee.Model.__init__ # but also want base features from Renderable to work super().__init__(extra_classes=extra_classes) # Renderable.__init__