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, 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 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 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 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 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()
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',