Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask REST API Resourceful Blueprints Ingredient Fields

ds

Why is my post is not correct?

resources/ingredients.py
from flask import Blueprint

from flask.ext.restful import (
    Resource, Api, reqparse, inputs,
    marshal, marshal_with, fields
)

import models

ingredient_fields = {
    'name': fields.String,
    'description': fields.String,
    'measurement_type': fields.String,
    'quantity': fields.Float,
    'recipe': fields.String    
}

class IngredientList(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument(
            'name',
            required=True,
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'description',
            required=True,
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'measurement_type',
            required=True,
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'quantity',
            type=float,
            required=True,
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'recipe',
            type=inputs.positive,
            required=True,
            location=['form', 'json']
        )
        super().__init__()

    def get(self):
        ingredients = [marshal(ingredient,ingredient_fields) for ingredient in models.Ingredient.select()]
        return {'ingredients':ingredients}

    def post(self):
        args = self.reqparse.parse_args()
        ingredients = [marshal(ingredient,ingredient_fields) for ingredient in ingredientmodels.Ingredient.create(**args)]
        return jsonify({'ingredients':ingredients})


class Ingredient(Resource):
    def __init__(self):
          self.reqparse = reqparse.RequestParser()
          self.reqparse.add_argument(
              'name',
              required=True,
              location=['form', 'json']
          )
          self.reqparse.add_argument(
              'description',
              required=True,
              location=['form', 'json']
          )
          self.reqparse.add_argument(
              'measurement_type',
              required=True,
              location=['form', 'json']
          )
          self.reqparse.add_argument(
              'quantity',
              type=float,
              required=True,
              location=['form', 'json']
          )
          self.reqparse.add_argument(
              'recipe',
              type=inputs.positive,
              required=True,
              location=['form', 'json']
          )
          super().__init__()

    def get(self, id):
        ingredient = marshal(models.Ingredient.get(models.Ingredient.id==id),ingredient_fields)
        return ingredient


ingredients_api = Blueprint('resources.ingredients', __name__)
api = Api(ingredients_api)
api.add_resource(IngredientList, '/api/v1/ingredients')
api.add_resource(Ingredient, '/api/v1/ingredients/<int:id>')

1 Answer

Hint: post creates one ingredient, not a list of them. So all you have to do is marshal(ingredient, ingredient_fields) in the return. You're just telling it, "Hey, serialize and return this ingredient that I just created in the format I described in ingredient_fields."