Beispiel #1
0
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)
Beispiel #2
0
def create_cupcake():
    """
    Add a cupcake and returndata about new cupcake
    Returns JSON:
    {
      cupcake: [
        {
          id,
          flavor,
          rating,
          size,
          image
        }
      ]
    }
  """
    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()

    # POST requests should return HTTP status of 201
    return (jsonify(cupcake=cupcake.to_dict()), 201)
Beispiel #3
0
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)
Beispiel #4
0
    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)
Beispiel #5
0
def create_cupcake():
    new_cupcake = Cupcake(**request.get_json())

    db.session.add(new_cupcake)
    db.session.commit()

    return jsonify({'cupcake': new_cupcake.serialize()}), 201
Beispiel #6
0
def cupcakes_get(cupcake_id):
    

    if request.method == "GET":

        response = Cupcake.get(id=cupcake_id)

        return jsonify(response)

    elif request.method == "PATCH":

        data = request.json

        flavor = data.get('flavor', None)
        size = data.get('size', None) 
        rating = data.get('rating', None) 
        image = data.get('image', None) 

        response = Cupcake.patch(cupcake_id, flavor=flavor, size=size, rating=rating, image=image)

        return jsonify(response)

    elif request.method == "DELETE":

        response = Cupcake.delete(cupcake_id)

        return jsonify(response)

    else:

        return jsonify(["invalid HTTP Verbage"])
Beispiel #7
0
def post_cupcake():
    """Post 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_cupcake())
    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)
Beispiel #9
0
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)
Beispiel #10
0
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)
Beispiel #11
0
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)
Beispiel #12
0
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 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)
Beispiel #14
0
def make_cupcake():
    """Add a cupcake and return cupcake information as JSON"""
    cupcake = Cupcake(flavor=request.json["flavor"],
                      rating=request.json["rating"],
                      size=request.json["size"],
                      image=request.json["image"] or None)

    db.session.add(cupcake)
    db.session.commit()
    return (jsonify(cupcake=cupcake.serialized()), 201)
Beispiel #15
0
def post_cupcake():
    '''add new cupcake, and return it'''
    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.to_dictionary()), 201)
Beispiel #16
0
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 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)
Beispiel #18
0
def create_cupcake():
    data = request.json
    flavor = data['flavor']
    size = data['size']
    rating = data['rating']
    image = data['image']
    cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image)
    db.session.add(cupcake)
    db.session.commit()
    return (jsonify(cupcake=cupcake.serializer_cupcakes()), 201)
Beispiel #19
0
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)
Beispiel #20
0
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)
Beispiel #21
0
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)
Beispiel #22
0
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)
Beispiel #23
0
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)
Beispiel #25
0
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)
Beispiel #26
0
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)
Beispiel #27
0
def api_create_cupcake():
    flavor = request.json["flavor"]
    size = request.json["size"]
    rating = float(request.json["rating"])
    image = request.json.get("image", "https://tinyurl.com/demo-cupcake")
    new_cupcake = Cupcake(flavor=flavor, size=size, rating=rating, image=image)

    db.session.add(new_cupcake)
    db.session.commit()
    # return (flavor)
    return (jsonify(cupcake=new_cupcake.json()), 201)
Beispiel #28
0
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)
Beispiel #29
0
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)
Beispiel #30
0
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)