def put(self, album_id): data = parser.parse_args() _album_id = AlbumModel.find_by_id(album_id) _artist_id = ArtistModel.find_by_id(data['artist_id']) _album_name = AlbumModel.find_by_title(data['title']) _album_artist = AlbumModel.find_by_id_and_title( data['artist_id'], data['title']) if not _album_id: return {'message': 'No Such Album Exist'}, 404 if _artist_id is None: return {'message': 'Artist with this ID does not exists'}, 404 if _album_artist: return {'message': 'Album with this artist already exists'}, 403 if not data['title'] or data['title'].isspace(): return {'message': 'Please Enter Album Name'}, 400 album = AlbumModel.find_by_id(album_id) album.title = data['title'] album.artist_id = data['artist_id'] artist = ArtistModel.find_by_id(data['artist_id']) db.session.commit() return { 'message': 'Genre Has been Updated', 'GenreDetails': { 'id': album_id, 'title': data['title'], 'created_at': album.created_at.strftime("%Y-%m-%d %H:%M:%S"), 'artist_name': artist.name } }, 200
def post(self): parser = reqparse.RequestParser( ) # create parameters parser from request # define al input parameters need and its type parser.add_argument('name', type=str, required=True, help="This field cannot be left blank") parser.add_argument('country', type=str) parser.add_argument('genre', type=str) data = parser.parse_args() for artist in ArtistModel.query.all(): if (data['name'] == artist.name): return { 'message': 'Artist {} already exists'.format(artist.name) }, 404 new_artist = ArtistModel(data['name'], data['country'], data['genre']) new_artist.save_to_db() return new_artist.json(), 200
def post(self): data = parser.parse_args() if ArtistModel.find_by_name(data['name']): return {'message': 'Artist with this Name Already Existed'}, 403 _created_at = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) new_artist = ArtistModel( name=data['name'], created_at=_created_at ) if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Artist Name'}, 400 try: new_artist.save_to_db() new_id = new_artist.id return { 'message': 'New Artist Has been Added', 'ArtistDetails': { 'id': new_id, 'name': data['name'], 'created_at': _created_at } }, 201 except: return { 'message': 'Something went Wrong' }, 500
def post(self, id_event): event = EventModel.query.filter_by(id=id_event).first() if not event: return {'message': 'Event not found'}, 404 parser = reqparse.RequestParser( ) # create parameters parser from request # define al input parameters need and its type parser.add_argument('name', type=str, required=True, help="This field cannot be left blank") parser.add_argument('country', type=str) parser.add_argument('genre', type=str) data = parser.parse_args() # Si existeix, l'afegim a la llista d'artistes de l'event for artist in ArtistModel.query.all(): if (data['name'] == artist.name): event.artists.append(artist) db.session.commit() return {'artist': artist.json()}, 200 # Si no, el creem i l'afegim a la llista d'artistes de l'event new_artist = ArtistModel(data['name'], data['country'], data['genre']) event.artists.append(new_artist) new_artist.save_to_db() return new_artist.json(), 200
def get(self): if ArtistModel.is_data_present() is None: return {'message': 'No Data Available.'} current_user = get_jwt_identity() if not current_user: return ArtistModel.return_two_records() try: return ArtistModel.return_all() except: return { 'message': 'Something went Wrong' }, 500
def setUp(self): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['DEBUG'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/data_test2.db' self.app_context = app.app_context() self.app_context.push() db.init_app(app) db.create_all() self.app = app.test_client() self.artist1 = ArtistModel(name="Luciano") self.artist2 = ArtistModel(name="Fabiana") db.session.add(self.artist1) db.session.add(self.artist2) db.session.commit()
def post(self, id_event): event = EventModel.find_by_id(id_event) if event: parser = reqparse.RequestParser( ) # create parameters parser from request # define al input parameters need and its type parser.add_argument(n.NM, type=str, required=True, help="Artistic name of the artist") parser.add_argument(n.CTRY, type=str, required=True, help="Country nationality of the artist") parser.add_argument(n.GNR, type=str, required=True, help="Main genre of the artist") data = parser.parse_args() with lock.lock: artist = ArtistModel.find_by_name(data[n.NM]) if artist and artist.country == data[ n.CTRY] and artist.genre == data[n.GNR]: event.artists.append(artist) event.save_to_db() return event.json(), 200 else: return {n.MSG: "Artist not found"}, 409 else: return {n.MSG: "Event not found"}, 404
def post(self): data = parser.parse_args() _album_artist = AlbumModel.find_by_id_and_title( data['artist_id'], data['title']) if _album_artist: return {'message': 'Album with this artist already exists'}, 403 artist = ArtistModel.find_by_id(data['artist_id']) if not artist: return {'message': 'No Such Artist Exists'}, 404 if not data['title'] or data['title'].isspace(): return {'message': 'Please Enter Album Name'}, 400 _created_at = str( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) new_album = AlbumModel(title=data['title'], artist_id=data['artist_id'], created_at=_created_at) try: new_album.save_to_db() new_id = new_album.id return { 'message': 'New Album Has been Added', 'AlbumDetails': { 'id': new_id, 'title': data['title'], 'created_at': _created_at, 'artist_name': artist.name } }, 201 except: return {'message': 'Something Went Wrong'}, 500
def delete(self, id): with lock.lock: artist = ArtistModel.find_by_id(id) if artist: artist.delete_from_db() return {n.MSG: "Artist deleted successfully"}, 200 else: return {n.MSG: "Artist not found"}, 404
def put(self, artist_id): id = ArtistModel.find_by_id(artist_id) if not id: return {'message': 'No Such Artist Exist'}, 404 data = parser.parse_args() _date = id.created_at if ArtistModel.find_by_name(data['name']): return {'message': 'Artist with this name already exists'}, 403 updated_artist = ArtistModel( name=data['name'], created_at=_date.strftime("%Y-%m-%d %H:%M:%S") ) if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Artist Name'}, 400 try: artist = ArtistModel.find_by_id(artist_id) artist.name = data['name'] updated_artist.commit_db() return { 'message': 'Artist Has been Updated', 'ArtistDetails': { 'id': artist_id, 'name': data['name'], 'created_at': _date.strftime("%Y-%m-%d %H:%M:%S") } }, 200 except: return { 'message': 'Something went Wrong' }, 500
def to_json(x): return { 'data': { 'album_id': x.id, 'title': x.title, 'artist_name': ArtistModel.find_by_id(x.artist_id).name, 'created_at': x.created_at.strftime("%Y-%m-%d %H:%M:%S") } }
def json(self, album_id): return { 'data': { 'album_id': album_id, 'title': self.title, 'artist_name': ArtistModel.find_by_id(self.artist_id).name, 'created_at': self.created_at.strftime("%Y-%m-%d %H:%M:%S") } }
def get(self, artist_id: int): artist = ArtistModel.find_by_id(artist_id) if not artist: return {'message': 'Artist Not Found'}, 404 try: return artist.json(), 200 except: return { 'message': 'Something went Wrong' }, 500
def put(self, id): artist = ArtistModel.find_by_id(id) if artist: parser = reqparse.RequestParser( ) # create parameters parser from request # define al input parameters need and its type parser.add_argument(n.NM, type=str, required=True, help="Artistic name of the artist") parser.add_argument(n.CTRY, type=str, required=True, help="Country nationality of the artist") parser.add_argument(n.GNR, type=str, required=True, help="Main genre of the artist") data = parser.parse_args() with lock.lock: if data[n.GNR] not in genres: return {n.MSG: "Genre not allowed"}, 400 aux_artist = ArtistModel.find_by_name(data[n.NM]) if aux_artist and aux_artist.id != id: return { n.MSG: "Artist already in the data base with another ID" }, 409 try: artist.modify_from_db(data[n.NM], data[n.CTRY], data[n.GNR]) return artist.json(), 200 except: return {n.MSG: "Error Description"}, 500 else: return {n.MSG: "Artist not found"}, 404
def get(self, id): artist = ArtistModel.find_by_id(id) if artist: return { n.EVNTS: [ event.json()[n.EVNT] for event in EventModel.query.filter( EventModel.artists.any(id=id)).all() ] }, 200 else: return {n.MSG: "Artist not found"}, 404
def post(self): parser = reqparse.RequestParser( ) # create parameters parser from request # define al input parameters need and its type parser.add_argument(n.NM, type=str, required=True, help="Artistic name of the artist") parser.add_argument(n.CTRY, type=str, required=True, help="Country nationality of the artist") parser.add_argument(n.GNR, type=str, required=True, help="Main genre of the artist") data = parser.parse_args() with lock.lock: if ArtistModel.find_by_name(data[n.NM]): return {n.MSG: "Artist already in the data base"}, 409 if data[n.GNR] not in genres: return {n.MSG: "Genre not allowed"}, 400 try: new_artist = ArtistModel(data[n.NM], data[n.CTRY], data[n.GNR]) new_artist.save_to_db() return new_artist.json(), 201 except: return {n.MSG: "Error Description"}, 500
def post(self): data = parser.parse_args() if not AlbumModel.find_by_id(data['album_id']): return {'message': 'No Such Album Exists'}, 404 if not GenreModel.find_by_id(data['genre_id']): return {'message': 'No Such Genre Exists'}, 404 if not MediaTypeModel.find_by_id(data['mediatype_id']): return {'message': 'No Such Media Type Exists'}, 404 if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Track Name'}, 400 if data['composer'].isspace(): return {'message': 'Please Enter Composer Name'}, 400 if TracksModel.find_song_already_created(data['name'], data['album_id'], data['genre_id'], data['mediatype_id']): return{'message': 'Track with all this information is already Exists'}, 403 _created_at = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) try: new_track = TracksModel( name=data['name'], albumid=data['album_id'], mediatypeid=data['mediatype_id'], genreid=data['genre_id'], composer=data['composer'], created_at=_created_at ) new_track.save_to_db() new_id = new_track.trackid artist_id = AlbumModel.find_by_id(data['album_id']).artist_id artist_name = ArtistModel.find_by_id(artist_id).name return { 'message': 'New Track has been Added', 'data': { 'track_id': new_id, 'name': data['name'], 'composer': data['composer'], 'created_at': _created_at, 'track_info': { 'artist_name': artist_name, 'album': AlbumModel.find_by_id(data['album_id']).title, 'media_type': MediaTypeModel.find_by_id(data['mediatype_id']).name, 'genre': GenreModel.find_by_id(data['genre_id']).name } } },201 except: return { 'message': 'Something went wrong' }, 500
def put(self, track_id: int): data = parser.parse_args() if not AlbumModel.find_by_id(data['album_id']): return {'message': 'No Such Album Exists'}, 404 if not GenreModel.find_by_id(data['genre_id']): return {'message': 'No Such Genre Exists'}, 404 if not MediaTypeModel.find_by_id(data['mediatype_id']): return {'message': 'No Such Media Type Exists'}, 404 if not data['name'] or data['name'].isspace(): return {'message': 'Please Enter Track Name'}, 400 if data['composer'].isspace(): return {'message': 'Please Enter Composer Name'}, 400 track = TracksModel.find_by_id(track_id) if not track: return {'message': 'No Such Track found.'}, 404 if (TracksModel.find_song_already_created(data['name'], data['album_id'], data['genre_id'], data['mediatype_id']) and (data['composer'] is track.composer)): return {'message': 'Track with all this information is already Exists'}, 403 try: track.name = data['name'] track.albumid = data['album_id'] track.mediatypeid = data['mediatype_id'] track.genreid = data['genre_id'] track.composer = data['composer'] db.session.commit() artist_id = AlbumModel.find_by_id(data['album_id']).artist_id artist_name = ArtistModel.find_by_id(artist_id).name return { 'message': 'Track has been Updated', 'Track_Details': { 'track_id': track_id, 'name': data['name'], 'composer': data['composer'], 'created_at': track.created_at.strftime("%Y-%m-%d %H:%M:%S"), 'track_info': { 'artist_name': artist_name, 'album': AlbumModel.find_by_id(data['album_id']).title, 'media_type': MediaTypeModel.find_by_id(data['mediatype_id']).name, 'genre': GenreModel.find_by_id(data['genre_id']).name } } }, 200 except: return { 'message': 'Something went wrong' }, 500
def delete(self, artist_id: int): claims = get_jwt_claims() if not claims['is_admin']: return { 'message': 'Admin Privileges required' }, 401 artist_id = ArtistModel.find_by_id(artist_id) if not artist_id: return {'message': 'No Such Artist Exist'}, 404 try: artist_id.delete_from_db(artist_id) return { 'message': 'Artist has been deleted' } except IntegrityError as e: db.session.rollback() return dict(message=e._message())
from models.event import EventModel from models.artist import ArtistModel from models.accounts import AccountsModel from flask_sqlalchemy import SQLAlchemy from flask import Flask app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) db.init_app(app) new_artist1 = ArtistModel('Oques Grasses', 'Spain', 'REGGAE') db.session.add(new_artist1) new_artist2 = ArtistModel('Txarango', 'Spain', 'POP') db.session.add(new_artist2) new_artist3 = ArtistModel('Bad Gyal', 'Spain', 'TRAP') db.session.add(new_artist3) new_artist4 = ArtistModel('Billie Eilish', 'USA', 'INDIE') db.session.add(new_artist4) new_event1 = EventModel('Festival Cruilla 2020', 'Parc del Forum', 'Barcelona', '2020-07-03', 60, 15000) new_event1.artists.append(new_artist1) new_event1.artists.append(new_artist2) db.session.add(new_event1) new_event2 = EventModel('Canet Rock 2020', 'Canet de Mar Beach', 'Barcelona',
def get(self, id): artist = ArtistModel.find_by_id(id) if artist: return artist.json(), 200 else: return {n.MSG: "Artist not found"}, 404
def get(self): return ArtistModel.return_all(), 200