Код: Выделить всё
from contextlib import contextmanager
import peewee
proxy = peewee.proxy()
@contextmanager
def get_database(distribution_type):
if distribution_type == "local":
yield peewee.MySQLDatabase("db_local", user="root", host="test-db", port="3306")
else:
yield peewee.MySQLDatabase("db_prod", user="root", host="prod_db", port="3306")
class BaseModel(peewee.Model):
class Meta:
database = proxy
db/manager.py
Код: Выделить всё
from core import get_database, proxy
class DistributionManager(object):
def __init__(self, distribution_type="local"):
super(DistributionManager, self).__init__()
self._distribution_type = distribution_type
self._initialize_database()
def _initialize_database(self):
with get_database(self._distribuition_type) as db:
proxy.initialize(db)
def create(self, source, dest):
self._pw_model = DistributionModel.create(source=source, dest=dest)
Подробнее здесь: https://stackoverflow.com/questions/525 ... explicitly