def create_cupcake(): """ Create and return a new Cupcake. """ form = AddCupcake() if request.is_json: new_cupcake = Cupcake( flavor=request.json["flavor"], size=request.json["size"], rating=request.json["rating"], image=request.json.get("image", None)) db.session.add(new_cupcake) db.session.commit() return (jsonify(cupcake=new_cupcake.serialize()), 201) if form.validate_on_submit(): new_cupcake = Cupcake( flavor=form.flavor.data, size=form.size.data, rating=form.rating.data, image=form.image.data or None ) db.session.add(new_cupcake) db.session.commit() flash(f"Cupcake {new_cupcake.id} added.") return redirect("/") return render_template("index.html", form=form)
def test_create_cupcake(self): with app.test_client() as client: url = "/api/cupcakes" cupcake1 = Cupcake(flavor='TestFlavor2', size='TestSize2', rating=10, image="http://test.com/cupcake2.jpg") resp = client.post(url, json=cupcake1.serialize()) self.assertEqual(resp.status_code, 201) data = resp.json # don't know what ID we'll get, make sure it's an int & normalize self.assertIsInstance(data['cupcake']['id'], int) del data['cupcake']['id'] self.assertEqual( data, { "cupcake": { "flavor": "TestFlavor2", "size": "TestSize2", "rating": 10, "image": "http://test.com/cupcake2.jpg" } }) self.assertEqual(Cupcake.query.count(), 2)
def add_cupcake(): """Add a cupcake to the database""" new_cupcake = Cupcake(flavor=request.json["flavor"], size=request.json["size"], rating=request.json["rating"], image=(request.json["image"] or None)) db.session.add(new_cupcake) db.session.commit() return (jsonify(cupcake=new_cupcake.serialize()), 201)
def create_cupcake(): new_cupcake = Cupcake(**request.get_json()) db.session.add(new_cupcake) db.session.commit() return jsonify({'cupcake': new_cupcake.serialize()}), 201
def create_cupcake(): new_cupcake = Cupcake(flavor=request.json["flavor"], size=request.json["size"], rating=request.json["rating"], image=request.json["image"] or None) db.session.add(new_cupcake) db.session.commit() return (jsonify(cupcake=new_cupcake.serialize()), 201)
def create_cupcake(): new_cupcake = Cupcake(flavor=request.json["flavor"], rating=request.json["rating"], size=request.json["size"]) db.session.add(new_cupcake) db.session.commit() response_json = jsonify(cupcake=new_cupcake.serialize()) return (response_json, 201)
def create_cupcake(): """ Create a new cupcake """ new_cupcake = Cupcake(flavor = request.json['flavor'], size = request.json['size'], rating = request.json['rating'], image = request.json['image']) db.session.add(new_cupcake) db.session.commit() response_json = jsonify(cupcake = new_cupcake.serialize()) return (response_json, 201)
class AppTestCase(unittest.TestCase): def setUp(self): """Set up test client and make new cupcake.""" #create tables db.create_all() self.client = app.test_client() self.new_cupcake = Cupcake( flavor='testing', size='small', rating=10) self.new_cupcake2 = Cupcake( flavor='testing2', size='medium', rating=10) db.session.add(self.new_cupcake) db.session.add(self.new_cupcake2) db.session.commit() def tearDown(self): print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') db.drop_all(bind=None) def test_root(self): response = self.client.get('/') self.assertEqual(response.status_code, 302) def test_all_cupcakes(self): response = self.client.get('/cupcakes') self.assertEqual(response.status_code, 200) print(response.json['cupcakes']) self.assertEqual(response.json['cupcakes'], [self.new_cupcake.serialize(), self.new_cupcake2.serialize()]) def test_add_cupcake(self): # self.assertEqual(Cupcake.query.count(), 1) response = self.client.post('/cupcakes', json={ 'flavor': 'new_cupcake', 'size': 'small', 'rating': 10 }) self.assertEqual(response.status_code, 200) self.assertEqual(response.json['cupcake'], {'id':3, 'flavor': 'new_cupcake', 'size': 'small', 'rating': 10, 'image': 'https://tinyurl.com/truffle-cupcake'})
def show_cupcakes(): """ Show all cupcakes """ # Get all cupcakes from database cupcakes = Cupcake.query.all() serialized_cupcakes = [Cupcake.serialize(cupcake) for cupcake in cupcakes] return jsonify(cupcakes=serialized_cupcakes)
def create_cupcake(): """Creates a new cupcake and returns JSON of that created cupcake""" new_cupcake = Cupcake(flavor=request.json["flavor"],size=request.json["size"],rating=request.json["rating"], image=request.json["image"] or None) db.session.add(new_cupcake) db.session.commit() response_json = jsonify(cupcake=new_cupcake.serialize()) return (response_json, 201)
def add_cupcake(): flavor = request.json.get('flavor') size = request.json.get('size') rating = request.json.get('rating') image = request.json.get('image') new_cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image) db.session.add(new_cupcake) db.session.commit() return (jsonify(cupcake=new_cupcake.serialize()), 201)
def add_cupcake(): cupcake = Cupcake(flavor=request.json.get("flavor"), size=request.json.get("size"), rating=request.json.get("rating"), image=request.json.get("image")) db.session.add(cupcake) db.session.commit() return (jsonify(cupcake=cupcake.serialize()), 201)
def add_cupcake(): """Add a new cupcake to the database""" flavor = request.json.get("flavor") size = request.json.get('size') rating = request.json.get('rating') image = request.json.get("image") cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image) Cupcake.add_cupcake(cupcake) return (jsonify(cupcake=cupcake.serialize()), 201)
def create_cupcake(): data = request.json new_cupcake = Cupcake(flavor=data['flavor'], size=data['size'], rating=data['rating'], image=data['image'] or None) db.session.add(new_cupcake) db.session.commit() response_json = jsonify(cupcake=new_cupcake.serialize()) return (response_json, 201)
def add_cupcake(): """creates a new cupcake""" flavor = request.json['flavor'] size = request.json['size'] rating = request.json['rating'] image = request.json['image'] new_cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image) db.session.add(new_cupcake) db.session.commit() return (jsonify(cupcake=new_cupcake.serialize()), 201)
def create_cupcakes(): """Create new cupcake""" #Collect Cupcake form data new_cupcake = Cupcake(flavor=request.json["flavor"], size=request.json["size"], rating=request.json["rating"], image=request.json["image"]) #Commit new to DB db.session.add(new_cupcake) db.session.commit() response_json = jsonify(cupcake=new_cupcake.serialize()) return (response_json, 201)
def create_cupcake(): flavor = request.json['flavor'] size = request.json['size'] rating = request.json['rating'] image = request.json['image'] image = image if len(image) > 0 else None new_cupcake = Cupcake(flavor=flavor, size=size, image=image, rating=rating) db.session.add(new_cupcake) db.session.commit() return (jsonify(cupcake=new_cupcake.serialize()), 201)
def create_cupcake(): """Creates a new cupcake and returns JSON of that cupcake""" data = request.json new_cupcake = Cupcake(flavor=data["flavor"], size=data["size"], rating=data["rating"], image=data["image"]) db.session.add(new_cupcake) db.session.commit() response = jsonify(cupcake=new_cupcake.serialize()) return (response, 201)
def create_cupcake(): """Creates a new cupcake and returns JSON of that created todo""" flavor = request.json["flavor"] size = request.json["size"] rating = request.json["rating"] image = request.json["image"] new_cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image) db.session.add(new_cupcake) db.session.commit() response_json = jsonify(cake=new_cupcake.serialize()) return (response_json, 201)
def create_cupcake(): new_cupcake = Cupcake( flavor = request.json['flavor'], size = request.json['size'], rating = request.json['rating'], image = request.json.get('image' or img_default) ) db.session.add(new_cupcake) db.session.commit() response_json = jsonify(cupcake = new_cupcake.serialize()) return (response_json, 201)
def post_cupcake(): """post cupcake and return JSON""" flavor = request.json['flavor'] size = request.json['size'] rating = request.json.get('rating',0.0) image = request.json.get('image', default_img) new_cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image) db.session.add(new_cupcake) db.session.commit() response = jsonify(cupcake=new_cupcake.serialize()) return (response,201)
def create_cupcake(): """Creates a new todo and returns JSON of that created todo""" new_cupcake = Cupcake(flavor=request.json["flavor"], size=request.json["size"], rating=request.json["rating"], image=request.json.get( "image", "https://tinyurl.com/demo-cupcake")) db.session.add(new_cupcake) db.session.commit() response_json = jsonify(cupcake=new_cupcake.serialize()) return (response_json, 201)
def create_cupcake(): json = request.json flavor = json['flavor'] size = json['size'] rating = json['rating'] image = json['image'] if json[ 'image'] else "https://tinyurl.com/demo-cupcake" cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image) db.session.add(cupcake) db.session.commit() return (jsonify(cupcake=cupcake.serialize()), 201)
def create_cupcake(): ''' should create new cupcake , return JSON describing cupcake ''' new_cupcake = Cupcake(flavor=request.json.get('flavor'), size=request.json.get('size'), rating=request.json.get('rating'), image=request.json.get('image')) db.session.add(new_cupcake) db.session.commit() return (jsonify(cupcake=new_cupcake.serialize()), 201)
def add_cupcake(): flavor = request.json['flavor'] size = request.json['size'] rating = request.json['rating'] image = request.json['image'] new_cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image) db.session.add(new_cupcake) db.session.commit() serialized_new_cupcake = new_cupcake.serialize() respond = jsonify(cupcake=serialized_new_cupcake) return (respond, 201)
def create_new_cupcake(): """ POST: adds a cupcake. returns {cupcake: {id, flavor, size, rating, image}}""" new_cupcake = Cupcake(flavor=request.json['flavor'], size=request.json['size'], rating=request.json['rating'], image=request.json['image']) db.session.add(new_cupcake) db.session.commit() return (jsonify(cupcake=new_cupcake.serialize()), 201)
def create_cupcake(): flavor = request.json.get('flavor', None) size = request.json.get('size', None) rating = request.json.get('rating', None) image = request.json.get('image', None) if (flavor and size and rating): cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image) db.session.add(cupcake) db.session.commit() return (jsonify({'cupcake': cupcake.serialize()}), 201) else: return jsonify({'cupcake': 'empty required fields'})
def createCupcake(): data = request.json cupcake = Cupcake(flavor=data['flavor'], rating=data['rating'], size=data['size'], image=data['image'] or None) db.session.add(cupcake) db.session.commit() return (jsonify(cupcake=cupcake.serialize()), 201)
def add_cupcake(): try: new_cupcake = Cupcake(flavor=request.json['flavor'], size=request.json['size'], rating=request.json['rating'], image=request.json['image'] or None) db.session.add(new_cupcake) db.session.commit() return (jsonify(cupcake=new_cupcake.serialize()), 201) except: return jsonify( message='Sorry, please make sure all values are filled in.')
def create_cupcake(): """ Create a new cupcake and return data about it an the response status of 201 Create""" new_cupcake = Cupcake(flavor=request.json["flavor"], size=request.json["size"], rating=request.json["rating"], image=request.json["image"] or None) db.session.add(new_cupcake) db.session.commit() response_json = jsonify(cupcake=new_cupcake.serialize()) return (response_json, 201)