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