"""Social Profile logic.""" import time from oto import response from oto import status from social_analytics.constants import error from social_analytics.models import artist_social_profile from social_analytics.models import social_profile def get_social_profiles(): """Get all the social profiles. Returns: response.Response: containing a list of social profile dictionaries. """ return social_profile.get_social_profiles() def get_social_profiles_by_ids(social_profile_ids): """Get all social profiles with the given social_profile_id. Args: social_profile_ids (list): the ids of the social profiles to search. Returns: response.Response: containing a list of social profile dictionaries with the given social_profile_ids. """ return social_profile.get_social_profiles_by_ids(social_profile_ids) def get_social_profiles_by_artist_id(artist_id): """Get all the artist social profiles for a given artist_id. Args: artist_id (str): the artist id for which to query artist social profiles. Returns: response.Response: containing a list of social profile dictionaries with the given artist_id. """ artist_social_profiles = ( artist_social_profile.get_artist_social_profiles_by_artist_id( artist_id)) if not artist_social_profiles: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS) if artist_social_profiles.status == 400: return response.create_not_found_response() artist_social_profiles = artist_social_profiles.message['items'] ids = [profile['social_profile_id'] for profile in artist_social_profiles] social_profiles = social_profile.get_social_profiles_by_ids(ids) if social_profiles: return social_profiles return response.create_not_found_response() def create_or_update_social_profile(data): """Create or update a social profile. This function would be responsible to create or update a social profile. Its logic would support the following scenarios: 1) Create a social profile and an artist social profile (L91-100). 2) A social profile already exists but we need to create a reference to artist social profile (L202-229). Args: data (dict): the data from which to create the social profile. Returns: response.Response: containing the created social profile dict. """ if not data: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS) platform_id = data['platform_id'] platform = data['platform'] found_social_profile = ( social_profile.get_social_profile_with_platform_id_and_platform( platform_id, platform)) # If an old artist social profile exists, delete it if 'old_social_profile_id' in data: deleted_old_profile = _remove_old_artist_social_profile(data) data.pop('old_social_profile_id', None) if not deleted_old_profile: return response.create_error_response( status.INTERNAL_ERROR, error.ERROR_MESSAGE_CANNOT_UPDATE) if not found_social_profile: return _create_social_profile_and_artist_social_profile(data) else: return _create_artist_social_profile(data, found_social_profile) def delete_social_profile(social_profile_id): """Delete a social profile. This function will delete a social profile based on a given social_profile_id. It will also delete the corresponding artist social profiles. Args: social_profile_id (int): the social profile id. Returns: response.Response: containing the deleted social profile and artist social profiles. """ if not social_profile_id: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS) deleted_social_profile = social_profile.delete_social_profile( social_profile_id) if not deleted_social_profile: return response.create_not_found_response() deleted_artist_social_profiles = ( artist_social_profile .delete_artist_social_profiles_by_social_profile_id(social_profile_id)) if not deleted_artist_social_profiles: return response.Response( { 'items': { 'social_profile': deleted_social_profile.message } }) return response.Response( { 'items': { 'social_profile': deleted_social_profile.message, 'artist_social_profiles': deleted_artist_social_profiles.message['items'] } }) def _remove_old_artist_social_profile(data): """Remove old social profile. Args: data (dict): the data from which to remove the artist social profile. Returns: response.Response: containing the deleted social profile dict. """ old_social_profile_id = data['old_social_profile_id'] artist_id = data['artist_id'] return artist_social_profile.delete_artist_social_profile( old_social_profile_id, artist_id) def _create_social_profile_and_artist_social_profile(data): """Create social profile and artist social profile. Args: data (dict): the data from which to create the social profile. Returns: response.Response: containing the deleted social profile dict. """ label_id = data['label_id'] artist_id = data['artist_id'] data.pop('artist_id', None) data.pop('label_id', None) data['collection_scheduled_time'] = time.strftime('%H:%M:%S') created_social_profile = ( social_profile.create_social_profile(data)) if not created_social_profile: return response.create_error_response( status.BAD_REQUEST, error.ERROR_CODE_MYSQL) created_social_profile = created_social_profile.message new_artist_social_profile = { 'artist_id': artist_id, 'social_profile_id': created_social_profile['social_profile_id'], 'label_id': label_id} result = artist_social_profile.create_artist_social_profile( new_artist_social_profile) if not result: return response.create_error_response( status.BAD_REQUEST, error.ERROR_CODE_MYSQL) result.message.update(created_social_profile) return result def _create_artist_social_profile(data, found_social_profile): """Update social profile. Args: data (dict): the data from which to update the social profile. found_social_profile (response.Response): A response.Response object with the a social profile. Returns: response.Response: containing the created social profile dict. """ found_social_profile = found_social_profile.message new_artist_social_profile = { 'artist_id': data['artist_id'], 'social_profile_id': found_social_profile['social_profile_id'], 'label_id': data['label_id']} result = artist_social_profile.create_artist_social_profile( new_artist_social_profile) if not result: return response.create_error_response( status.BAD_REQUEST, error.ERROR_CODE_MYSQL) result.message.update(data) return result