def create(self, request): """Handle POST operations Returns: Response -- JSON serialized Product instance """ 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.created_date = request.data["created_date"] 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["product_category_id"]) new_product.product_category = product_category new_product.save() serializer = ProductSerializer(new_product, context={'request': request}) return Response(serializer.data)
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, format=None): newproduct = Product() # product type refers to a foreign key product_type = ProductType.objects.get(pk=request.data["product_type"]) # customer refers to the user customer = Customer.objects.get(user=request.auth.user) # request all other data newproduct.title = request.data["title"] newproduct.price = request.data["price"] newproduct.description = request.data["description"] newproduct.quantity = request.data["quantity"] newproduct.location = request.data["location"] newproduct.image_path = request.data["image_path"] newproduct.customer = customer newproduct.product_type = product_type newproduct.created_at = datetime.now() newproduct.save() serializer = SellSerializer(newproduct, context={'request': request}) return Response(serializer.data)
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)