def get_images(product_id=''):
    product_model = Product()
    product = product_model.find(product_id)
    images = [product['imgUrl']]
    colors = product['fullSaleInfo']['colors']
    for color in colors:
        images += [image for key, image in color.items()]

    return jsonify({'imageUrls': images})
def get_image(product_id=''):
    try:
        product_model = Product()
        product = product_model.find(product_id)

        image = product['imgUrl']
        return jsonify({'image': image})
    except:
        return jsonify({'image': None})
Ejemplo n.º 3
0
    def test_find_product(self):
        """ Find a Product by ID """
        Product(0, 'Asus2500', 'Laptop', '234', 'laptop', 'blue', 4).save()
        Product(0, 'GE4509', 'Microwave', '34324', 'microwave', 'black',
                4).save()
        products = Product.find(2)

        self.assertIsNot(products, None)
        self.assertEqual(products.id, 2)
        self.assertEqual(products.name, "GE4509")
def update(payload):
    p_id = payload['id']
    data = payload['payload']
    app.logger.info('Payload = %s', data)
    product = Product.find(p_id)
    if not product:
        raise NotFound('Product with id [{}] was not found.'.format(p_id))
    app.logger.info('Product with id [%s] updated', p_id)
    product.deserialize(data)
    product.id = p_id
    product.save()
 def delete(self, products_id):
     """
     Delete a Product
     This endpoint will delete a Product based the id specified in the path
     """
     app.logger.info('Request to Delete a product with id [%s]',
                     products_id)
     product = Product.find(products_id)
     if product:
         product.delete()
     return '', HTTP_204_NO_CONTENT
Ejemplo n.º 6
0
def add_product_unit(id):
    """ Updates a Product in the database fom the posted database """
    product = Product.find(id)
    if product:
        product.count = int(product.count) + 1
        message = product.serialize()
        return_code = HTTP_200_OK
    else:
        message = {'error' : 'Product with id: %s was not found' % str(id)}
        return_code = HTTP_404_NOT_FOUND

    return jsonify(message), return_code
def add_product_unit(id):
    product = Product.find(id)
    if product:
        product.count = int(product.count) + 1
        product.save()
        message = product.serialize()
        return_code = HTTP_200_OK
    else:
        message = {'error': 'Product with id: %s was not found' % str(id)}
        return_code = HTTP_404_NOT_FOUND

    return make_response(jsonify(message), return_code)
 def get(self, products_id):
     """
     Retrieve a single Product
     This endpoint will return a Product based on it's id
     """
     app.logger.info("Request to Retrieve a product with id [%s]",
                     products_id)
     product = Product.find(products_id)
     if product:
         return product.serialize(), status.HTTP_200_OK
     else:
         raise NotFound(
             "Product with id '{}' was not found.".format(products_id))
Ejemplo n.º 9
0
def get_products(id):
    """ Retrieves a Product with a specific id """
    product = Product.find(id)
    if product:
        message = product.serialize()
        return_code = HTTP_200_OK
        if product.count == 0:
            message['Understocked'] = 'Product out of Stock'
    else:
        message = {'error' : 'Product with id: %s was not found' % str(id)}
        return_code = HTTP_404_NOT_FOUND

    return jsonify(message), return_code
Ejemplo n.º 10
0
def update_products(id):
    """ Updates a Products in the database fom the posted database """
    product = Product.find(id)
    if product:
        payload = request.get_json()
        product.deserialize(payload)
        product.save()
        message = product.serialize()
        return_code = HTTP_200_OK
    else:
        message = {'error' : 'Product with id: %s was not found' % str(id)}
        return_code = HTTP_404_NOT_FOUND

    return jsonify(message), return_code
Ejemplo n.º 11
0
def sell_products(id):
    """ Updates a Product in the database fom the posted database """
    product = Product.find(id)
    if product:
        if product.count == 0:
            message = {'error' : 'Product with id: %s is out of Stock' % str(id)}
        else:
            product.count = int(product.count) - 1
            message = product.serialize()

        return_code = HTTP_200_OK
    else:
        message = {'error' : 'Product with id: %s was not found' % str(id)}
        return_code = HTTP_404_NOT_FOUND

    return jsonify(message), return_code
Ejemplo n.º 12
0
    def get(self, products_id):
        """
        Retrieve a single Product
        This endpoint will return a Product based on it's id
        """
        app.logger.info("Request to Retrieve a product with id [%s]",
                        products_id)
        product = Product.find(products_id)
        if product:
            message = product.serialize()
            return_code = HTTP_200_OK
            return product.serialize(), status.HTTP_200_OK
            if product.count == 0:
                raise NotFound("Product is Understocked.")

        else:
            raise NotFound(
                "Product with id '{}' was not found.".format(products_id))
def sell_products(id):
    product = Product.find(id)
    if product:
        if product.count == 0:
            message = {
                'error': 'Product with id: %s is out of Stock' % str(id)
            }
        else:
            product.count = int(product.count) - 1
            product.save()
            message = product.serialize()

        return_code = HTTP_200_OK
    else:
        message = {'error': 'Product with id: %s was not found' % str(id)}
        return_code = HTTP_404_NOT_FOUND

    return make_response(jsonify(message), return_code)
Ejemplo n.º 14
0
 def put(self, products_id):
     """
     Update a Product
     This endpoint will update a Product based the body that is posted
     """
     app.logger.info('Request to Update a product with id [%s]',
                     products_id)
     check_content_type('application/json')
     product = Product.find(products_id)
     if not product:
         #api.abort(404, "Pet with id '{}' was not found.".format(pet_id))
         raise NotFound(
             'Product with id [{}] was not found.'.format(products_id))
     #data = request.get_json()
     data = api.payload
     app.logger.info(data)
     product.deserialize(data)
     product.id = products_id
     product.save()
     return product.serialize(), status.HTTP_200_OK
def get_description(product_id=''):
    product_model = Product()
    product = product_model.find(product_id)
    return jsonify({'description': _generate_description(product)})
def get_comments(product_id=''):
    product_model = Product()
    product = product_model.find(product_id)
    comments = product['fullSaleInfo']['listCmts'] if product is not None else []
    comments = [{'cmt': c['cmt'], 'date': c['date'], 'name': c['name']} for c in comments]
    return jsonify({'comments': comments})
def get_contact(product_id=''):
    product_model = Product()
    product = product_model.find(product_id)
    contact = product['fullSaleInfo']['call'] if product is not None else []
    return jsonify({'contact': contact})
Ejemplo n.º 18
0
def delete_product(id):
    """ Removes a Product from the database that matches the id """
    product = Product.find(id)
    if product:
        product.delete()
    return make_response('', HTTP_204_NO_CONTENT)
Ejemplo n.º 19
0
 def test_find_with_no_product(self):
     """ Find a Product with no products """
     products = Product.find(1)
     self.assertIs(products, None)
def get_product(product_id):
    product_model = Product()
    product = product_model.find(product_id)
    product = _create_response_product(product)
    return jsonify({'product': product})
def get_product_raw(product_id):
    product_model = Product()
    product = product_model.find(product_id)
    return jsonify({'product': product})
Ejemplo n.º 22
0
 def test_product_not_found(self):
     """ Test for a Product that doesn't exist """
     Product(0, 'Asus2500', 'Laptop', '234', 'laptop', 'blue', 4).save()
     #Product(0, 'GE4509', 'Microwave','34324', 'wewef', 'fwfwsxdws' ).save()
     products = Product.find(2)
     self.assertIs(products, None)