From c7404aa7cf9d1d60cb0157c522fc0d36cadc69ee Mon Sep 17 00:00:00 2001 From: Norman Hooper Date: Wed, 21 Feb 2024 10:28:50 +0000 Subject: [PATCH] Dependency inversion: Move util to model method Models use utils. Utils don't use models. --- hq_superset/api.py | 2 +- hq_superset/models.py | 42 ++++++++++++++++++++++++++++++++++++++++- hq_superset/utils.py | 44 ------------------------------------------- 3 files changed, 42 insertions(+), 46 deletions(-) diff --git a/hq_superset/api.py b/hq_superset/api.py index 2e3cbf5..a2c7e5d 100644 --- a/hq_superset/api.py +++ b/hq_superset/api.py @@ -106,7 +106,7 @@ def post_dataset_change(self) -> FlaskResponse: try: request_json = json.loads(request.get_data(as_text=True)) change = DataSetChange(**request_json) - update_dataset(change) + change.update_dataset() return self.json_response( 'Request accepted; updating dataset', status=HTTPStatus.ACCEPTED.value, diff --git a/hq_superset/models.py b/hq_superset/models.py index d8ffde4..05b4aca 100644 --- a/hq_superset/models.py +++ b/hq_superset/models.py @@ -9,9 +9,10 @@ from cryptography.fernet import MultiFernet from flask import current_app from superset import db +from superset.connectors.sqla.models import SqlaTable from .const import HQ_DATA -from .utils import encoded +from .utils import encoded, get_explore_database, get_hq_database @dataclass @@ -24,6 +25,45 @@ def __post_init__(self): if 'doc_id' not in self.data: raise TypeError("'data' missing required key: 'doc_id'") + def update_dataset(self): + database = get_hq_database() + explore_database = get_explore_database(database) # TODO: Necessary? + sqla_table = ( + db.session.query(SqlaTable) + .filter_by( + table_name=self.data_source_id, + database_id=explore_database.id, + ) + .one_or_none() + ) + if sqla_table is None: + raise ValueError(f'{self.data_source_id} table not found.') + + if self.action == 'delete': + stmt = ( + sqla_table + .delete() + .where(sqla_table.doc_id == self.data['doc_id']) + ) + elif self.action == 'upsert': + stmt = ( + sqla_table + .insert() + .values(self.data) # TODO: Do we need to cast anything? + .on_conflict_do_update( + index_elements=['doc_id'], + set_=self.data, + ) + ) + else: + raise ValueError(f'Invalid DataSetChange action {self.action!r}') + try: + db.session.execute(stmt) + db.session.commit() + except Exception: # pylint: disable=broad-except + db.session.rollback() + raise + class HQClient(db.Model, OAuth2ClientMixin): __bind_key__ = HQ_DATA diff --git a/hq_superset/utils.py b/hq_superset/utils.py index 18bc318..f0f72d5 100644 --- a/hq_superset/utils.py +++ b/hq_superset/utils.py @@ -10,7 +10,6 @@ from superset.utils.database import get_or_create_db from .const import HQ_DATA -from .models import DataSetChange DOMAIN_PREFIX = "hqdomain_" SESSION_USER_DOMAINS_KEY = "user_hq_domains" @@ -220,46 +219,3 @@ def get_explore_database(database): ) else: return database - - -def update_dataset(change: DataSetChange): - from superset import db - from superset.connectors.sqla.models import SqlaTable - - database = get_hq_database() - explore_database = get_explore_database(database) # TODO: Necessary? - sqla_table = ( - db.session.query(SqlaTable) - .filter_by( - table_name=change.data_source_id, - database_id=explore_database.id, - ) - .one_or_none() - ) - if sqla_table is None: - raise ValueError(f'{change.data_source_id} table not found.') - - if change.action == 'delete': - stmt = ( - sqla_table - .delete() - .where(sqla_table.doc_id == change.data['doc_id']) - ) - elif change.action == 'upsert': - stmt = ( - sqla_table - .insert() - .values(change.data) # TODO: Do we need to cast anything? - .on_conflict_do_update( - index_elements=['doc_id'], - set_=change.data, - ) - ) - else: - raise ValueError(f'Invalid DataSetChange action {change.action!r}') - try: - db.session.execute(stmt) - db.session.commit() - except Exception: # pylint: disable=broad-except - db.session.rollback() - raise