コード例 #1
0
ファイル: plant.py プロジェクト: seanw7/Plant-API-2
    def put(self, name):
        data = Plant.parser.parse_args()
        plant = PlantModel.find_by_name(name)

        if plant is None:
            plant = PlantModel(name, data['quantity'], data['price'],
                               data['genus_name'])
        else:
            plant.quantity = data['quantity']
            plant.price = data['price']
        plant.save_to_db()

        return plant.json()
コード例 #2
0
ファイル: plant.py プロジェクト: seanw7/Plant-API-2
    def post(self, name):
        if PlantModel.find_by_name(name):
            return {
                'message':
                'An plant with name "{}" already exists'.format(name)
            }, 400  # 400 means bad request

        # Makes sure the item json the user sends to us has a price field.
        data = Plant.parser.parse_args()
        plant = PlantModel(name, data['quantity'], data['price'],
                           data['genus_name'])
        # Try and insert the POST data into the database.
        try:
            plant.save_to_db()
        except:
            return {
                "message": "An error occurred inserting the plant."
            }, 500  # Internal Server Error

        return plant.json(), 201