"""Logic functions for track_sample.""" from collections.abc import Iterable from flask import g from oto import response from backend.constants import error from backend.constants import track_sample as const from backend.models import track_sample from backend.utils import api as api_utils def create_track_sample(tuid, data): """Validate and create a new sample for this tuid. Args: tuid (id): Track unique identifier. data (dict): Data to create a new sample. Returns: response.Response: a Response object with JSON data """ for field in const.SAMPLE_REQUIRED_FIELDS: if field not in data.keys(): return api_utils.create_validation_error_response( error.VALIDATION_ERROR_MISSING_FIELD_MSG, field) response = _validate_sample_type(data['sample_type']) if not response: return response artist_data = data['artists'] response = _validate_artists_data(artist_data) if not response: return response del data['artists'] return track_sample.create_new_sample(tuid, data, artist_data) def _validate_sample_type(sample_type): """Verify if sample_type is a valid. Args: sample_type (str): Track sample type. Returns: bool: True if the value is valid. """ if sample_type not in const.CLEARANCE_TYPES: return api_utils.create_validation_error_response( error.VALIDATION_ERROR_INVALID_FIELD_MSG, 'sample_type') return response.Response() def _validate_artists_data(artist_data): """Validate if artist_data has all required fields and types. Args: artist_data (list): List of artist data. Returns: bool: True if the value is valid. """ missing_type = True for artist in artist_data: if 'artist_name' not in artist: return api_utils.create_validation_error_response( error.VALIDATION_ERROR_BLANK_NAME_MSG, 'artist_name') if artist.get('artist_type') not in const.ARTIST_TYPES: return api_utils.create_validation_error_response( error.INVALID_ARTIST_TYPE_ERROR_MSG, artist.get('artist_type')) if artist.get('artist_type') == const.ARTIST_TYPE: missing_type = False if missing_type: return api_utils.create_validation_error_response( error.MISSING_ARTIST_TYPE, const.ARTIST_TYPE) return response.Response() def get_track_sample(sample_id): """Get sample by its id. Args: sample_id (id): Track Sample unique identifier. Returns: response.Response: a Response object with JSON data """ return track_sample.get_sample_data(sample_id) def delete_sample_data(sample_id): """Delete sample by its id. Args: sample_id (id): Track Sample unique identifier. Returns: response.Response: a Response object with JSON data """ return track_sample.delete_sample_data(sample_id) def update_sample_data(sampleid, data): """Validate and patch a new data to an existing sample. Args: sampleid (id): Track Sample identifier. data (dict): Data to create a new sample. Returns: response.Response: a Response object with JSON data """ artist_data = None if 'sample_type' in data: is_valid = _validate_sample_type(data['sample_type']) if not is_valid: return is_valid if 'artists' in data: result = _validate_artists_data(data['artists']) if not result: return result artist_data = data['artists'] del data['artists'] return track_sample.update_sample_data(sampleid, data, artist_data) def bulk_delete_samples_by_tuids(tuids): """Delete performers with given tuids. Args: tuids (list): unique ids of tracks Returns: response.Response: result of deletion """ if not isinstance(tuids, Iterable): return api_utils.create_error_response( error.INVALID_VALUE_ERROR_CODE, 'invalid tuids.') return track_sample.bulk_delete_by_tuids(tuids) def copy_tracks(source_dest_tuid_list): """Copy track samples from source tracks to destination tracks. Args: source_dest_tuid_list (list): List of tuples with source to dest tuid. """ mapping = dict() for source_tuid, dest_tuid in source_dest_tuid_list: mapping[source_tuid] = dest_tuid all_samples = track_sample.get_samples_by_tuids(mapping.keys()) if not all_samples: error_msg = '{message} {error}'.format( message=error.COPY_TRACKS_ERROR_CODE, error=all_samples.errors.get('message')) g.log.error(error_msg) return all_samples result = track_sample.copy_multiple_samples( all_samples.message, mapping) if not result: error_msg = '{message} {error}'.format( message=error.COPY_TRACKS_ERROR_CODE, error=result.errors.get('message')) g.log.error(error_msg) return result