Exemplo n.º 1
0
 def post(self):
     result_dict, err_code = handle_request_validation_and_serialisation(
         item_creation_schema)
     if err_code:
         return result_dict, err_code
     with db.session.no_autoflush:
         item = ItemModel(**result_dict)
         if not item.categories:
             return {
                 "message": f'Item must have at lest one category.'
             }, 400
         for cat in item.categories:
             c_id = cat.id
             if not ItemCategoryModel.find_by_id(c_id):
                 return {
                     "message":
                     f"Category with id: {c_id} does not exists. "
                     f"Item's category id's must exist before the item can be created. "
                     f"Create this category and try again."
                 }, 404
     try:
         item.add_to_db()
     except IntegrityError:
         return {
             "message":
             "An integrity error has occurred while adding to the database."
         }, 400
     return {
         "message": "Entry successfully created.",
         "entry": item_schema.dump(item)
     }, 201
Exemplo n.º 2
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {
                'message': "an item with name '{}' already exist.".format(name)
            }, 400

        data = Item.parser.parse_args()

        item = ItemModel(
            name, **data)  # **data simplifies data['price'], data['store_id']

        try:
            item.add_to_db()
        except:
            return {'message': 'Error occured while inserting item'}, 500

        return item.json(), 201