コード例 #1
0
    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)
コード例 #2
0
    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)
コード例 #4
0
    def test_delete_product(self):
        product = Product()
        product.name = "Kite"
        product.price = 24.99
        product.quantity = 40
        product.description = "It flies very high"
        product.category_id = 1
        product.created_date = datetime.date.today()
        product.location = "Pittsburgh"
        product.customer_id = 1
        product.save()

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.delete(f"/products/{product.id}")
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        response = self.client.get(f"/products/{product.id}")
        self.assertEqual(response.status_code,
                         status.HTTP_500_INTERNAL_SERVER_ERROR)