"""Logical operations for acrids.""" from connector_neo4j import Neo4jSession from sound_recordings import config from sound_recordings.models import acrids from sound_recordings.models import orchard_assets class AlreadyFingerprinted(Exception): """OrchardAsset found with different ACRID relationship.""" pass class AssetNotFound(Exception): """Unable to find OrchardAsset with Track connection by id.""" pass @Neo4jSession(transaction=True, use_v2=True, database=config.NEO4J_DATABASE_NAME) def upsert(acr_id, asset_id): """Upsert acrid to asset relationship. Args: acr_id (str): fingerprint id to match asset_id (uuid): asset uuid to match Returns: list (int): Track.id(s) attached to OrchardAsset bool: if any nodes or relationships were created Raises: AlreadyFingerprinted: asset exists with different acrid AssetNotFound: unable to find asset with track attachment """ ( asset_exists, old_acr_ids, nodes_created, rels_created ) = acrids.upsert(acr_id, asset_id) if not asset_exists: raise AssetNotFound() if old_acr_ids: raise AlreadyFingerprinted() updates = (nodes_created or rels_created) return ( orchard_assets.get_track_ids(asset_id), updates )