diff --git a/woudc_data_registry/registry.py b/woudc_data_registry/registry.py index 5688fe3..027cce4 100644 --- a/woudc_data_registry/registry.py +++ b/woudc_data_registry/registry.py @@ -124,11 +124,11 @@ def query_distinct_by_fields( :returns: list of distinct values """ - LOGGER.debug(f'Querying distinct values for {domain}') - conditions = [] target_fields = values.keys() + LOGGER.debug(f'Querying distinct values by fields {target_fields} for {domain}') + for field in target_fields: table_field = getattr(obj, field) if case_insensitive: @@ -318,32 +318,29 @@ def update_by_field( def save(self, obj=None): """ - helper function to save object to registry + Helper function to save object to registry. :param obj: object to save (default None) :returns: void """ + if obj is None: + LOGGER.warning('obj is none while trying to save, skipping') + return registry_config = config.EXTRAS.get('registry', {}) try: - if obj is not None: - flag_name = '_'.join([obj.__tablename__, 'enabled']) - if registry_config.get(flag_name, True): - self.session.add(obj) - # self.session.merge(obj) - else: - LOGGER.info(f'Registry persistence for model {obj.__tablename__} disabled, skipping') # noqa - return - - try: - self.session.commit() - except SQLAlchemyError as err: - LOGGER.error(f'Failed to persist {obj} due to: {err}') - self.session.rollback() - - LOGGER.debug(f'Saving {obj}') - except DataError as err: + flag_name = '_'.join([obj.__tablename__, 'enabled']) + if registry_config.get(flag_name, True): + self.session.add(obj) # Use merge if needed: self.session.merge(obj) + else: + LOGGER.info(f'Registry persistence for model {obj.__tablename__} disabled, skipping') + return + + LOGGER.debug(f'Committing save of {obj}') + self.session.commit() + + except (SQLAlchemyError, DataError) as err: LOGGER.error(f'Failed to save to registry: {err}') self.session.rollback()