Esempio n. 1
0
 def post(self, name):
     if ItemModel.find_by_name(name):
         return {'message': MSG.EXISTED.format(name)}, 400
     data = self.parser.parse_args()
     item = ItemModel(**data)
     safe_run(message=MSG.ERROR_INSERT.format(name),
              error_code=500)(item.save_to_db(item))
     return item.jsonify(), 201
Esempio n. 2
0
 def put(self, name):
     data = self.parser.parse_args()
     item = ItemModel.find_by_name(name)
     if item is None:
         item = ItemModel(**data)
     else:
         for key in ['price', 'name']:
             setattr(item, key, data[key])
     safe_run(message=MSG.ERROR_UPDATE.format(name),
              error_code=500)(item.save_to_db(item))
     return item.jsonify(), 201
Esempio n. 3
0
    def post(self):
        data = request.json
        name = data['name']
        if ItemModel.find_by_name(name):
            return {
                'message': 'An item with name {} already exists.'.format(name)
            }

        item = ItemModel(data['name'], data['price'], data['store_id'])
        try:
            item.save_to_db()
        except:
            return 500
Esempio n. 4
0
    def get(self, name):
        item = ItemModel.find_by_name(name)

        if item:
            return item.json(), 200

        return {"message": "No item with name {} found!.".format(name)}, 404
Esempio n. 5
0
 def get(self):
     args = item_ns_parser.parse_args()
     name = args.get('name', None)
     item = ItemModel.find_by_name(name)
     if item:
         return item.json()
     return {'message': 'item not found'}, 404
Esempio n. 6
0
 def delete(self, name):
     claims = get_jwt_claims()
     if not claims['is_admin']:
         return {'message': 'you are not an admin'}
     item = ItemModel.find_by_name(name)
     if item:
         item.delete_from_db()
     return {'message': 'Item deleted'}
Esempio n. 7
0
 def get(self, name):
     try:
         item = ItemModel.find_by_name(name) #  returns an ItemModel object
     except:
         return {'message': 'An error ocurred getting the item'}, 500
     if item:
         return item.json()
     return {'message': "item not found."}, 400
Esempio n. 8
0
 def delete(self, name):
     item = ItemModel.find_by_name(name)
     if item:
         safe_run(message=MSG.ERROR_DELETE.format(name),
                  error_code=500)(item.delete_from_db(item))
     else:
         return {'message': MSG.NOT_FOUND.format(name)}, 400
     return {'message': MSG.DELETED.format(name)}, 201
Esempio n. 9
0
    def delete(self, name):
        item = ItemModel.find_by_name(name)
        if item:
            item.delete_from_db()
            return {"message": "Item deleted successfully"}, 200

        return {
            "message": "An item with name {} does not exist.".format(name)
        }, 400  # Bad Request
 def get(self):
     user_id = get_jwt_identity()
     items = [item.json() for item in ItemModel.find_all()]
     if user_id:
         return {'items': items}, 200
     return {
         'items': [item['name'] for item in items],
         'message': 'more data available if you login!'
     }
 def delete(self, name):
     claims = get_jwt_claims()
     if not claims['is_admin']:
         return {'message': 'Admin privilege required'}, 401
     item = ItemModel.find_by_name(name)
     if item:
         item.delete_from_db()
         return {'message': 'Item deleted.'}
     return {'message': 'Item not found.'}, 404
Esempio n. 12
0
 def delete(self):
     args = item_ns_parser.parse_args()
     name = args.get('name', None)
     item = ItemModel.find_by_name(name)
     if item:
         item.delete_from_db()
         return {'message': 'Item deleted'}
     else:
         return {'message': 'Item not found'}
Esempio n. 13
0
    def get(name: str) -> tuple:
        """
        Returns item by name.

        :param name: String name.
        :return: {'message': "Item not found."}
        """
        item = ItemModel.find_by_name(name)
        if item:
            return item.json(), 200
        return {'message': "Item not found."}, 404
Esempio n. 14
0
    def put(self, name):
        data = Item.parser.parse_args()
        item = ItemModel.find_by_name(name)

        if item is None:
            try:
                item = ItemModel(name, **data)
                item.save_to_db()
            except:
                return {
                    "message": "An error occurred inserting the item."
                }, 500
        else:
            try:
                item.price = data['price']
                item.store_id = data['store_id']
                item.save_to_db()
            except:
                return {"message": "An error occurred updating the item."}, 500

        return item.json(), 201
Esempio n. 15
0
    def delete(self, name):
        item = ItemModel.find_by_name(name)
        if item is None:
            return {
                "message": "No item with name {} found!.".format(name)
            }, 404
        else:
            try:
                item.delete_from_db()
            except:
                return {"message": "An error occurred deleting the item."}, 500

        return {"message": "Item Deleted"}
Esempio n. 16
0
    def put(self):
        data = request.json
        item = ItemModel.find_by_name(data['name'])

        if item is None:
            item = ItemModel(data['name'], data['price'], data['store_id'])
        else:
            item.price = data['price']

        item.save_to_db()

        return item.json()
Esempio n. 17
0
    def put(self, name):
        data = Item.parser.parse_args()

        item = ItemModel.find_by_name(name)

        if item is None:
            item = ItemModel(name, **data)
        else:
            item.price = data['price']

        item.save_to_db()

        return item.json()
Esempio n. 18
0
    def put(self, name):
        data = Item.parser.parse_args()
        try:
            item = ItemModel.find_by_name(name)

            if item is None:
                item = ItemModel(name, **data)
            else:
                item.price = data['price']

            item.save_to_db()
        except:
            return {'message': 'An error ocurred updating the item'}, 500
        return item.json()
Esempio n. 19
0
    def put(self, name):
        data = Item.parser.parse_args()
        item = ItemModel.find_by_name(name)
        if item is None:
            return {'message': 'Item not found'}, 404
        elif StoreModel.find_by_id(data['store_id']) is None:
            return {
                'message': 'There is no Store. You should be add store first.'
            }, 400
        else:
            item.price = data['price']
            item.store_id = data['store_id']

        item.save_to_db()

        return item.json()
Esempio n. 20
0
    def get() -> tuple:
        """
        Returns a list of all items.
        More detailed return when logged in.

        :return: {'items': Int}
        """
        user_id = get_jwt_identity()

        items = [item.json() for item in ItemModel.find_all()]
        if user_id:
            return {'items': items}, 200
        return {
            'items': [item['name'] for item in items],
            'message': 'More data available when logged in.'
        }, 200
Esempio n. 21
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {'message': "An item with name {} is already exists".format(name)}, 400  # returns HTTP status bad request
        data = Item.parser.parse_args()
        item = ItemModel(name, **data)
        try:
            item.save_to_db()
        except:
            return {'message': 'An error ocurred inserting the item'}, 500

        return item.json(), 201
Esempio n. 22
0
 def put(self, name):
     data = Item.parser.parse_args()
     item = ItemModel.find_by_name(name)
     if item is None:
         try:
             item = ItemModel(name, data['price'], data['store_id'])
         except Exception as e:
             return {"message": "an error occurred inserting the item"}, 500
     else:
         try:
             item.price = data['price']
         except Exception as e:
             return {"message": "an error occurred updating the item"}, 500
     item.save_to_db()
     return item.json()
Esempio n. 23
0
    def put(self, name):
        data = Item.parser.parse_args()

        item = ItemModel.find_by_name(name)

        try:
            if item is None:
                item = ItemModel(name, data["price"], data["store_id"])
            else:
                item.price = data["price"]
                item.store_id = data["store_id"]

            item.save_to_db()
        except:
            return {
                "message": "An error occurred inserting/updating the item."
            }, 500

        return item.json()
Esempio n. 24
0
    def delete(name: str) -> tuple:
        """
        Deletes given object if existing.
        Requires a fresh JWT.

        :param name: String name.
        :return: {'message': String}
        """
        claims = get_jwt()

        if not claims['is_admin']:
            return {'message': 'Admin privilege required!'}, 401

        item = ItemModel.find_by_name(name)

        if item:
            item.delete_from_db()
            return {'message': 'Item deleted!'}, 200
        return {'message': 'Item not found!'}, 404
Esempio n. 25
0
 def post(self, name):
     if ItemModel.find_by_name(name):
         return {
             'message': 'an item with {} already exists'.format(name)
         }, 400
     request_data = Item.parser.parse_args()
     item = ItemModel(name, request_data['price'], request_data['store_id'])
     try:
         item.save_to_db()
     except Exception as e:
         return {
             "message": "an error occurred inserting the item."
         }, 500  # internal server error
     return item.json(), 201
Esempio n. 26
0
    def put(cls, name: str) -> tuple:
        """
        Create new or update existing item.

        :param name: String name.
        :return: {'item': Int}
        """
        data = cls.parser.parse_args()

        item = ItemModel.find_by_name(name)

        if item is None:
            # Insert new object
            item = ItemModel(name, **data)  # data['price'], data['store_id']
        else:
            item.price = data['price']  # Update object in db
        item.save_to_db()

        return item.json(), 200
Esempio n. 27
0
    def post(self, name):
        item = ItemModel.find_by_name(name)
        if item is not None:
            message = f'An item with name {name} already exists.'
            return {'message': message}, 400

        data = Item.parser.parse_args()

        item = ItemModel(name, **data)

        try:
            item.save_to_db()
        except:
            return {"message": "An error occured inserting the item."}, 500

        return item.json(), 201
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {
                'message':
                "An item with name '{}' already exists.".format(name)
            }, 400

        data = Item.parser.parse_args()

        item = ItemModel(name, **data)

        try:
            item.save_to_db()
        except:
            return {"message": "An error occurred inserting the item."}, 500

        return item.json(), 201
Esempio n. 29
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            # return a message with Bad Request code 400
            return {
                "message": "An item with name {} already exists".format(name)
            }, 400

        # use the get_json() method to retrieve the json data from the page
        #request_data = request.get_json()
        # instead we use the request parser here
        data = Item.parser.parse_args()
        item = ItemModel(
            name=name, price=data["price"],
            store_id=data["store_id"])  # or ItemModel(name, **data)
        try:
            item.save_to_db()
        except:
            return {"message": "An error occurred inserting the item."}, 500

        return item.json(), 201  # code for created ok
Esempio n. 30
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {
                'message': "An item with '{}' already exists".format(name)
            }, 400

        jsonPayload = Item.parser.parse_args()

        if StoreModel.find_by_id(jsonPayload['store_id']) is None:
            return {
                'message': 'There is no Store. You should be add store first.'
            }, 400

        item = ItemModel(name, **jsonPayload)

        try:
            item.save_to_db()  # attributing object self
        except:
            return {"message", "An error occurred inserting the item"}, 400

        return item.json(), 201  # create success