"""Social Profile Model. This model represents a profile for a Social Platform, e.g. 'Facebook with id 123455'. """ from oto import response from oto import status import sqlalchemy from sqlalchemy.sql import func from social_analytics.connectors import mysql from social_analytics.constants import error class SocialProfile(mysql.BaseModel): """Social Profile model.""" __tablename__ = 'social_profile' social_profile_id = sqlalchemy.Column( sqlalchemy.INT, primary_key=True, autoincrement=True) platform = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) platform_id = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) platform_name = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) collection_scheduled_time = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) last_collected_date = sqlalchemy.Column(sqlalchemy.DateTime) created_date = sqlalchemy.Column(sqlalchemy.DateTime, default=func.now()) updated_date = sqlalchemy.Column(sqlalchemy.DateTime, onupdate=func.now()) created_by = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) updated_by = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) def to_dict(self): """Return the object as dictionary.""" return dict( social_profile_id=self.social_profile_id, platform=self.platform, platform_id=self.platform_id, platform_name=self.platform_name, collection_scheduled_time=self.collection_scheduled_time, last_collected_date=self.last_collected_date, created_by=self.created_by, updated_by=self.updated_by) @mysql.autosession() def get_social_profiles(session): """ Return all the social profiles. Args: session (Session): the mysql session. Returns: response.Response: containing all the social profiles. """ rows = session.query(SocialProfile).all() results = [row.to_dict() for row in rows] if results: return response.Response({'items': results}) return response.create_not_found_response() @mysql.autosession() def create_social_profile(data, session): """ Create a new Social Profile. Args: data (dict): the data from which to create the social profile. session (Session): the mysql session. Returns: response.Response: containing the created social profile dict. """ if not data: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_EMPTY_BODY) social_profile = SocialProfile(**data) session.add(social_profile) session.commit() return response.Response(social_profile.to_dict()) @mysql.autosession() def update_social_profile(data, session): """Update a Social Profile. Args: data (dict): the data with which to update the social profile. session (Session): the mysql session. Returns: response.Response: containing the updated social profile dict. """ if not data: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_EMPTY_BODY) profile = SocialProfile(**data) session.query( SocialProfile).filter( SocialProfile.social_profile_id == profile.social_profile_id).update( data) session.commit() return response.Response(profile.to_dict()) @mysql.autosession() def update_social_profiles_last_collected_date(profile_ids, session): """Update multiple social profiles. Args: profile_ids (list): A list containing the social profile ids to be updated. session (Session): The MySQL session. Returns: response.Response: Containing a list with the updated social profiles. """ if not profile_ids: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_EMPTY_BODY) items = [] for profile_id in profile_ids: updated_count = ( session.query( SocialProfile).filter( SocialProfile.social_profile_id == profile_id) .update({'last_collected_date': func.now()}, synchronize_session=False)) if updated_count > 0: items.append(profile_id) session.commit() return response.Response({'items': items}) @mysql.autosession() def get_social_profiles_by_ids(social_profile_ids, session): """ Query Social Profiles by social_profile_id. Args: social_profile_ids (list): the ids with which to query the Social Profiles. session (Session): the mysql session. Returns: response.Response: containing the queried Social Profiles. """ if not social_profile_ids: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_EMPTY_BODY) rows = session.query(SocialProfile).filter( SocialProfile.social_profile_id.in_(social_profile_ids)) results = [row.to_dict() for row in rows] if len(results) > 0: return response.Response({'items': results}) return response.create_not_found_response() @mysql.autosession() def get_social_profile_by_id(social_profile_id, session): """Get a social profile by social_profile_id. Args: social_profile_id (int): social_profile_id eg. 123 session (Session): the mysql session. Returns: response.Response: containing the queried Social Profiles. """ if not social_profile_id: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS) social_profile = session.query(SocialProfile).filter_by( social_profile_id=social_profile_id).first() return response.Response(social_profile.to_dict()) @mysql.autosession() def get_social_profile_with_platform_id_and_platform( platform_id, platform, session): """Get a social profile with platform_id and platform. Args: platform_id (int): The platform id e.g. 123444 platform (string) E.g. Facebook session (Session): the mysql session. Returns: response.Response: containing the social profile. """ if not platform_id and not platform: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS) social_profile = session.query(SocialProfile).filter_by( platform_id=platform_id, platform=platform).first() if social_profile is not None: return response.Response(social_profile.to_dict()) return response.create_not_found_response() @mysql.autosession() def delete_social_profile(social_profile_id, session): """Delete a social profile. Args: social_profile_id (int): the social profile id. session (Session): the mysql session. Returns: response.Response: containing the deleted social profile dict. """ if not social_profile_id: return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS) social_profile = session.query( SocialProfile).filter_by(social_profile_id=social_profile_id).first() if not social_profile: return response.create_not_found_response() session.delete(social_profile) session.commit() return response.Response(social_profile.to_dict())