コード例 #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
コード例 #3
0
ファイル: plant.py プロジェクト: seanw7/Plant-API-2
    def delete(self, name):
        plant = PlantModel.find_by_name(name)
        if plant:
            plant.delete_from_db()

        return {"message": "Plant deleted"}
コード例 #4
0
ファイル: plant.py プロジェクト: seanw7/Plant-API-2
 def get(self, name):
     plant = PlantModel.find_by_name(name)
     if plant:
         return plant.json()
     return {"message": "Plant not found"}, 404