def test_delete_product(self): """ Ensure we can delete a product. """ category = ProductCategory() category.name = "Toys" category.save() product = Product() product.customer_id = 1 product.name = "Nerf Gun" product.price = 24.99 product.description = "High powered fun" product.quantity = 25 product.category = category product.created_date = datetime.date.today() product.location = "Nashville" product.image_path = "" product.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) product_url = f"/products/{product.id}" response = self.client.delete(product_url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) response = self.client.get(product_url) self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
def products_over_1000(request): if request.method == 'GET': # import pdb; pdb.set_trace() with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" SELECT p.id, p.name, p.price, p.description, p.quantity, p.created_date, p.location, c.name AS category, p.customer_id FROM bangazonapi_product p JOIN bangazonapi_productcategory c ON c.id = p.category_id WHERE p.price > 1000 ORDER By price DESC """) dataset = db_cursor.fetchall() products = [] for row in dataset: category = ProductCategory.objects.get(name=row["category"]) customer = Customer.objects.get(pk=row["customer_id"]) product = Product() product.id = row["id"] product.category = category product.created_date = row["created_date"] product.location = row["location"] product.price = row["price"] product.quantity = row["quantity"] product.customer = customer product.name = row["name"] product.description = row["description"] products.append(product) template = 'products_over_1000.html' context = {"products_over_1000": products} return render(request, template, context)
def create(self, request): """ @api {POST} /products POST new product @apiName CreateProduct @apiGroup Product @apiHeader {String} Authorization Auth token @apiHeaderExample {String} Authorization Token 9ba45f09651c5b0c404f37a2d2572c026c146611 @apiParam {String} name Short form name of product @apiParam {Number} price Cost of product @apiParam {String} description Long form description of product @apiParam {Number} quantity Number of items to sell @apiParam {String} location City where product is located @apiParam {Number} category_id Category of product @apiParamExample {json} Input { "name": "Kite", "price": 14.99, "description": "It flies high", "quantity": 60, "location": "Pittsburgh", "category_id": 4 } @apiSuccess (200) {Object} product Created product @apiSuccess (200) {id} product.id Product Id @apiSuccess (200) {String} product.name Short form name of product @apiSuccess (200) {String} product.description Long form description of product @apiSuccess (200) {Number} product.price Cost of product @apiSuccess (200) {Number} product.quantity Number of items to sell @apiSuccess (200) {Date} product.created_date City where product is located @apiSuccess (200) {String} product.location City where product is located @apiSuccess (200) {String} product.image_path Path to product image @apiSuccess (200) {Number} product.average_rating Average customer rating of product @apiSuccess (200) {Number} product.number_sold How many items have been purchased @apiSuccess (200) {Object} product.category Category of product @apiSuccessExample {json} Success { "id": 101, "url": "http://localhost:8000/products/101", "name": "Kite", "price": 14.99, "number_sold": 0, "description": "It flies high", "quantity": 60, "created_date": "2019-10-23", "location": "Pittsburgh", "image_path": null, "average_rating": 0, "category": { "url": "http://localhost:8000/productcategories/6", "name": "Games/Toys" } } """ new_product = Product() new_product.name = request.data["name"] new_product.price = request.data["price"] new_product.description = request.data["description"] new_product.quantity = request.data["quantity"] new_product.location = request.data["location"] customer = Customer.objects.get(user=request.auth.user) new_product.customer = customer product_category = ProductCategory.objects.get( pk=request.data["category_id"]) new_product.category = product_category if "image_path" in request.data: format, imgstr = request.data["image_path"].split(';base64,') ext = format.split('/')[-1] data = ContentFile( base64.b64decode(imgstr), name=f'{new_product.id}-{request.data["name"]}.{ext}') new_product.image_path = data new_product.save() serializer = ProductSerializer(new_product, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED)