예제 #1
0
 def put(self):
     """Updates product review with review provided as newrev"""
     app.logger.info("Fetching the product")
     item = request.args.get("id")
     # item = int((product_arguments3.parse_args())['id'])
     check_content_type("application/json")
     if not item:
         return request_validation_error("Missing Parameter product ID")
     product = Product.find_by_id(item)
     newreview = request.args.get('newrev')
     # newreview =  str ((product_arguments2.parse_args())['rev'])
     print(newreview)
     if not product:
         # api.abort(status.HTTP_404_NotFound,'Product with id: %s was not found' % str(item))
         return not_found("Product with id {} not found".format(item))
     if newreview == '' or newreview is None:
         return request_validation_error(
             "Review should be an empty string atleast")
     elif not product.review:
         print(newreview)
         product.review = str(newreview)
     else:
         product.review = str(product.review) + "|" + str(newreview)
         print(product.review)
     product.update()
     return product.serialize(), status.HTTP_200_OK
예제 #2
0
파일: service.py 프로젝트: varsh27/products
def deleteproduct(item_id):
    app.logger.info("Deleting the product for the id provided")
    product = Product.find_by_id(item_id)
    if not product:
        return make_response("Product does not exist",
                             status.HTTP_204_NO_CONTENT)
    product.delete()
    return make_response(" ", status.HTTP_204_NO_CONTENT)
예제 #3
0
 def delete(self, item_id):
     """ Deletes a product by ID"""
     app.logger.info("Deleting the product for the id provided")
     product = Product.find_by_id(item_id)
     # if not product:
     #     return make_response("Product does not exist", status.HTTP_204_NO_CONTENT)
     if product:
         product.delete()
     # return make_response(" ", status.HTTP_204_NO_CONTENT)
     return " ", status.HTTP_204_NO_CONTENT
예제 #4
0
 def get(self, item_id):
     """ Finds a product by ID"""
     app.logger.info('Finding a Product with id [{}]'.format(item_id))
     if not isinstance(item_id, int):
         return request_validation_error("Invalid Product ID")
     product = Product.find_by_id(item_id)
     if product:
         # app.logger.info(product)
         return product.serialize(), status.HTTP_200_OK
     else:
         return not_found('Product ID was not found')
예제 #5
0
파일: service.py 프로젝트: varsh27/products
def list_products_by_id(item_id):
    app.logger.info('Finding a Product with id [{}]'.format(item_id))
    product = Product.find_by_id(item_id)
    if product:
        message = product.serialize()
        return_code = status.HTTP_200_OK
    else:
        message = {'error': 'Product with id: %s was not found' % str(item_id)}
        raise NotFound(message)
        # return_code = status.HTTP_404_NOT_FOUND

    return jsonify(message), return_code
예제 #6
0
파일: service.py 프로젝트: varsh27/products
def update_product_review():
    app.logger.info("Fetching the product")
    item_id = request.args.get("id")
    check_content_type("application/json")
    product = Product.find_by_id(item_id)
    newreview = request.args.get('newrev')
    print(newreview)
    if not product:
        raise NotFound("Product with id {} not found".format(item_id))
    if not product.review:
        product.review = str(newreview)
    else:
        product.review = str(product.review) + "|" + str(newreview)
    product.update()
    return make_response(jsonify(product.serialize()), status.HTTP_200_OK)
예제 #7
0
파일: service.py 프로젝트: varsh27/products
def update_product(item_id):
    app.logger.info("Fetching the product")
    check_content_type("application/json")
    product = Product.find_by_id(item_id)
    # app.logger.info(product.rating)
    # prevrating = product.rating
    if not product:
        raise NotFound("Product with id {} not found".format(item_id))
    # app.logger.info(product.deserialize(request.get_json()))
    hitcount = product.updateCount
    product.deserialize(request.get_json())
    product.id = item_id
    # app.logger.info(product.rating)
    product.rating = product.totalrating / (hitcount + 1)
    product.update()
    return make_response(jsonify(product.serialize()), status.HTTP_200_OK)
예제 #8
0
 def test_find_by_id(self):
     """ Find a Product by ID """
     Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ",
             8).save()
     table = Product(2, "Table", "Oak table", "Home", 150, "Boxed", 100,
                     " ", 7)
     table.save()
     product = Product.find_by_id(table.id)
     self.assertIsNot(product, None)
     self.assertEqual(product.id, table.id)
     self.assertEqual(product.name, "Table")
     self.assertEqual(product.category, "Home")
     self.assertEqual(product.description, "Oak table")
     self.assertEqual(product.price, 150)
     self.assertEqual(product.condition, "Boxed")
     self.assertEqual(product.inventory, 100)
     self.assertEqual(product.rating, 7)
예제 #9
0
파일: service.py 프로젝트: varsh27/products
def list_products():
    """ Return all the products"""
    products = []
    name = request.args.get('name')
    app.logger.info(name)
    category = request.args.get('category')
    id = request.args.get("id")
    if name:
        products = Product.find_by_name(name)
    elif category:
        products = Product.find_by_category(category)
    elif id:
        products = Product.find_by_id(id)
    else:
        products = Product.all()

    results = [product.serialize() for product in products]
    return make_response(jsonify(results), status.HTTP_200_OK)
예제 #10
0
파일: service.py 프로젝트: varsh27/products
def update_product_rating():
    app.logger.info("Fetching the product")
    item = request.args.get("id")
    # check_content_type("application/json")
    product = Product.find_by_id(item)
    newrating = request.args.get('stars')
    print(newrating)
    if not product:
        raise NotFound("Product with id {} not found".format(item))
    # app.logger.info(product.deserialize(request.get_json()))
    # product.deserialize(request.get_json())
    # product.id = item_id
    # app.logger.info(product.rating)
    product.totalrating += int(newrating)
    # product.updateCount += 1
    product.rating = (int(product.totalrating) / (product.updateCount + 1))
    product.update()
    return make_response(jsonify(product.serialize()), status.HTTP_200_OK)
예제 #11
0
 def put(self):
     """Updates product rating with rating provided as stars"""
     try:
         app.logger.info("Fetching the product")
         item = request.args.get("id")
         # check_content_type("application/json")
         # item = (product_arguments2.parse_args())['id']
         app.logger.info(item)
         # newrating = int ((product_arguments2.parse_args())['rating'])
         if not item:
             return request_validation_error("Missing Parameter product ID")
         product = Product.find_by_id(item)
         newrating = request.args.get('stars')
         print(product)
         print(newrating)
         if not product:
             # api.abort(status.HTTP_404_NotFound,'Product with id: %s was not found' % str(item))
             return not_found("Product with id {} not found".format(item))
         elif newrating == '' or newrating is None:
             return request_validation_error("Rating cannot be empty")
         elif not isinstance(int(newrating), int):
             return request_validation_error('Rating is a number')
         elif int(newrating) > 10 or int(newrating) < 1:
             # app.logger.info("WOOHOO")
             # app.logger.info(newrating)
             return request_validation_error(
                 "Rating should be between 1-10")
         # app.logger.info(product.deserialize(request.get_json()))
         # product.deserialize(request.get_json())
         # product.id = item_id
         # app.logger.info(product.rating)
         product.totalrating += int(newrating)
         # product.updateCount += 1
         product.rating = (int(product.totalrating) /
                           (product.updateCount + 1))
         product.update()
         # return make_response(jsonify(product.serialize()),status.HTTP_200_OK)
         return product.serialize(), status.HTTP_200_OK
     except:
         return request_validation_error('Invalid request')
예제 #12
0
 def put(self, item_id):
     """ Updates a product by ID"""
     try:
         app.logger.info("Fetching the product")
         check_content_type("application/json")
         product = Product.find_by_id(item_id)
         # app.logger.info(product.rating)
         # prevrating = product.rating
         if not product:
             # api.abort(status.HTTP_404_NotFound,'Product with id: %s was not found' % str(item_id))
             return not_found(
                 "Product with id {} not found".format(item_id))
         # app.logger.info(product.deserialize(request.get_json()))
         hitcount = product.updateCount
         product.deserialize(api.payload)
         product.id = item_id
         # app.logger.info(product.rating)
         product.rating = product.totalrating / (hitcount + 1)
         product.update()
         # return make_response(jsonify(product.serialize()),status.HTTP_200_OK)
         return product.serialize(), status.HTTP_200_OK
     except ValidationError:
         return request_validation_error('Invalid data provided')