"""ReleaseArtist Model.""" from oto import response from sqlalchemy import BigInteger from sqlalchemy import Column from sqlalchemy import Integer from sqlalchemy import String from product.connectors import mysql from product.constants import error class ReleaseArtist(mysql.BaseModel): """ReleaseArtist Model class.""" __tablename__ = 'release_artist' release_artist_id = Column( Integer, primary_key=True, autoincrement=True, nullable=False) upc = Column(BigInteger, nullable=False) role = Column(String(25), nullable=False, default='performer') release_id = Column(Integer, default=0) artist_name = Column(String(255), nullable=False) url = Column(String(156)) artist_info_id = Column(Integer) def get_formatted_role(self): """Convert release artist roles to product artist types. Currently there is only 1 role mapping but we can have more here. """ if self.role == 'performer': return 'primary_artist' return self.role def to_dict(self): """To dict representation.""" return { 'id': self.release_artist_id, 'upc': self.upc, 'release_id': self.release_id, 'role': self.role, 'artist_name': self.artist_name, 'url': self.url, 'artist_info_id': self.artist_info_id, } def validate_artist_ids(release_artist_ids): """Validate release artist_ids with DB. Args: release_artist_ids (list): unique identifiers for ReleaseArtist. Returns: response.Response: containing success or error message. """ with mysql.db_session() as session: result = ( session.query(ReleaseArtist) .filter(ReleaseArtist.release_artist_id.in_(release_artist_ids)) .all() ) final = [str(s.release_artist_id) for s in result] invalid_ids = set(release_artist_ids) - set(final) if len(invalid_ids) > 0: return response.create_error_response( error.ERROR_CODE_INVALID_DATA, 'Not all artist_ids are valid. Invalid ids: {}.'.format( invalid_ids)) return response.Response(message='All ids are valid.') def get_artist_ids_for_product(product_id): """Get release artist_ids for this product_id. Args: product_id (list): unique identifiers for release. Returns: Response: containing list of artist ids or [] """ with mysql.db_session() as session: release_artists = session.query(ReleaseArtist.release_artist_id) \ .filter_by(release_id=product_id) \ .all() release_artist_ids = [item for sublist in release_artists for item in sublist] return response.Response(message=release_artist_ids) def create(values): """Create a release artist entry. Args: values (dict): dictionary of values to insert """ with mysql.db_session() as session: release_artist = ReleaseArtist(**values) session.add(release_artist) session.flush() return response.Response(message=release_artist.to_dict()) def update(values, product_id): """Update a release artist entry. Args: values (dict): dictionary of values to update product_id (int): id of product to update the release artist for """ with mysql.db_session() as session: session.query(ReleaseArtist) \ .filter(ReleaseArtist.release_id == product_id) \ .update(values) return response.Response(message=values) def get(product_id): """Get a release artist entry. Args: product_id (int): id of product to update the release artist for """ with mysql.db_session() as session: release_artist = session.query(ReleaseArtist) \ .filter(ReleaseArtist.release_id == product_id) \ .first() if not release_artist: return response.Response(status=404) return response.Response(message=release_artist.to_dict()) def delete(product_id): """Delete a release artist entry. Args: product_id (int): id of product to delete the release artist for """ with mysql.db_session() as session: session.query(ReleaseArtist) \ .filter(ReleaseArtist.release_id == product_id) \ .delete() return response.Response()