예제 #1
0
def _get_by_id(ID, model):
    with session_context() as session:
        query = session.query(model)
        resource = query.get(ID)
        if resource:
            return resource.to_dict()
        else:
            return {}
예제 #2
0
def create_spell_type(name, description=None, spell_ids=None):
    with session_context() as session:
        if spell_ids:
            query = session.query(Spell)
            spells = [query.get(spell_id) for spell_id in spell_ids]
        else:
            spells = None
        spell_type = SpellType(name, description=description, spells=spells)
        session.add(spell_type)
        session.commit()
        return spell_type.to_dict()
예제 #3
0
def create_spell(name, description, history=None, spell_type_ids=None):
    if isinstance(name, bytes):
        name = name.decode()
    if isinstance(description, bytes):
        description = description.decode()
    if isinstance(history, bytes):
        history = history.decode()
    with session_context() as session:
        if spell_type_ids:
            query = session.query(SpellType)
            spell_types = [query.get(st_id) for st_id in spell_type_ids]
        else:
            spell_types = None
        spell = Spell(name,
                      description,
                      history=history,
                      spell_types=spell_types)
        session.add(spell)
        session.commit()
        return spell.to_dict()
예제 #4
0
def session(docker_container, monkeypatch):
    env = {
        'POSTGRES_USER': '******',
        'POSTGRES_PASSWORD': '******',
        'POSTGRES_PORT': '5434',
        'POSTGRES_HOST': os.environ.get('DOCKER_IP', '192.168.99.100'),
    }
    for key, var in env.items():
        monkeypatch.setenv(key, var)
    monkeypatch.delenv('SPELLS_ELIXIR', raising=False)
    with session_context() as session:
        engine = session.get_bind()
        metadata = Base.metadata
        metadata.bind = engine
        metadata.drop_all()
        metadata.create_all()
    try:
        yield session
    finally:
        metadata.drop_all(bind=engine)
예제 #5
0
from spells_db.session_context import session_context
from spells_db.models import Base

if __name__ == "__main__":
    with session_context() as session:
        engine = session.get_bind()
        metadata = Base.metadata
        metadata.bind = engine
        # metadata.drop_all()
        metadata.create_all()
예제 #6
0
def receive_before_create(target, connection, **kw):
    with session_context() as session:
        engine = session.bind
        if not engine.dialect.has_schema(engine, 'spl'):
            engine.execute(CreateSchema('spl'))
예제 #7
0
def _get_all(model):
    with session_context() as session:
        query = session.query(model)
        resources = query.all()
        return [resource.to_dict() for resource in resources]
예제 #8
0
def _get_by_name(name, model):
    with session_context() as session:
        query = session.query(model)
        resources = query.filter(model.name.like(name))
        return [resource.to_dict() for resource in resources]
예제 #9
0
def map_spell_to_type(spell_id, spell_type_id):
    with session_context() as session:
        spell_type_mapping = SpellTypeMapping(spell_id, spell_type_id)
        session.add(spell_type_mapping)
        session.commit()
        return spell_type_mapping.to_dict()