def post(self): """ Creates a Product This endpoint will create a Product based the data in the body that is posted """ try: check_content_type('application/json') product = Product(1, "", "", "", 0, "", 0, "", 0) # product = Product() # app.logger.info((api.payload)) product.deserialize(api.payload) product.save() message = product.serialize() location_url = api.url_for(ProductCollection, item_id=product.id, _external=True) # return make_response(jsonify(message), status.HTTP_201_CREATED, # { # 'Location': location_url # }) return product.serialize(), status.HTTP_201_CREATED, { 'Location': location_url } except ValidationError: return request_validation_error('Invalid Data')
def test_delete_a_product(self): """ Delete a Product """ product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8) product.save() self.assertEqual(len(Product.all()), 1) # delete the product and make sure it isn't in the database product.delete() self.assertEqual(len(Product.all()), 0)
def test_add_a_product(self): """ Create a product and add it to the database """ products = Product.all() self.assertEqual(products, []) product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8) self.assertTrue(product is not None) self.assertEqual(product.id, 1) product.save() # Assert that it was assigned an id and shows up in the database self.assertEqual(product.id, 1) products = Product.all() self.assertEqual(len(products), 1)
def test_update_a_product_rating(self): """ Update a Product Rating""" product = Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8) product.save() self.assertEqual(product.id, 1) # Change it and save it product.rating = 10 product.update() self.assertEqual(product.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change products = Product.all() self.assertEqual(len(products), 1) self.assertEqual(products[0].rating, 10)
def create_products(): """ Creates a Pet This endpoint will create a Pet based the data in the body that is posted """ check_content_type('application/json') product = Product(1, "", "", "", 0, "", 0, "", 0) product.deserialize(request.get_json()) product.save() message = product.serialize() location_url = url_for('list_products_by_id', item_id=product.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': location_url})
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)
def test_sort_by_date(self): """ Sort Products by Date """ Product(1, "Couch", "White couch", "Furniture", 200, "Boxed", 50, " ", 8).save() table = Product(2, "Table", "Oak table", "Home", 150, "Boxed", 100, " ", 7) table.save() table.price = 200 table.update() product = list(Product.sort_by_date())[0] 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, 200) self.assertEqual(product.condition, "Boxed") self.assertEqual(product.inventory, 100) self.assertEqual(product.rating, 7)
def product_add(): """ represents add product processing url """ name = request.form.get("name") price = request.form.get("price") picture = request.files.get("picture") category = request.form.get("category") try: new_product = Product(name, price, picture) new_product.set_object_category(category) new_product.save() except Exception as Error: flash("error", Error.__str__()) else: flash("success", "sukses tambah produk") return redirect(url_for("dashboard.product"))