Skip to content

Commit

Permalink
Use event decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
nadzyah committed Aug 11, 2023
1 parent 4a8ab9e commit fe32585
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
10 changes: 4 additions & 6 deletions backend/test_observer/data_access/setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from os import environ
from sqlalchemy import create_engine, event
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from .models import Artefact
from .validators import validate_artefact
# We need to import this to enable event listeners
import test_observer.data_access.validators # noqa: F401


DEFAULT_DB_URL = "postgresql+pg8000://postgres:password@test-observer-db:5432/postgres"
DB_URL = environ.get("DB_URL", DEFAULT_DB_URL)
Expand All @@ -12,9 +13,6 @@
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


event.listen(Artefact, "before_insert", validate_artefact)


# Dependency
def get_db():
db = SessionLocal()
Expand Down
3 changes: 2 additions & 1 deletion backend/test_observer/data_access/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
# Nadzeya Hutsko <[email protected]>
"""Functions to validate the whole models (not just specific field)"""

from sqlalchemy import select
from sqlalchemy import select, event
from sqlalchemy.engine.base import Connection
from sqlalchemy.orm import Mapper
from .models import Stage, Family, Artefact


@event.listens_for(Artefact, "before_insert")
def validate_artefact(
mapper: Mapper, connection: Connection, artefact: Artefact # noqa: ARG001
) -> None:
Expand Down

0 comments on commit fe32585

Please sign in to comment.