예제 #1
0
    def test_cart(self):
        """We can add items to the shopping cart
        and retrieve them"""

        # first need to force the creation of a session and
        # add the cookie to the request
        sessionid = session.get_or_create_session(self.db)
        self.assertIsNotNone(sessionid)
        request.cookies[session.COOKIE_NAME] = sessionid

        # initial cart should be empty
        cart = session.get_cart_contents(self.db)
        self.assertEqual([], cart)

        # now add something to the cart
        for pname in ['Yellow Wool Jumper', 'Ocean Blue Shirt']:
            product =  self.products[pname]
            session.add_to_cart(self.db, product['id'], 1 )

        cart = session.get_cart_contents(self.db)
        self.assertEqual(2, len(cart))

        # check that all required fields are in the every cart entry
        for entry in cart:
            self.assertIn('id', entry)
            self.assertIn('name', entry)
            self.assertIn('quantity', entry)
            self.assertIn('cost', entry)
예제 #2
0
    def test_add_nonexisting_to_cart(self):
        # We start one session
        cart = []
        request.environ['beaker.session'] = MockBeakerSession({'cart': cart})
        self.assertEqual([], session.get_cart_contents())
        product = self.products['Classic Varsity Top']
        session.add_to_cart(self.db, product['id'], 1)
        cart = session.get_cart_contents()
        self.assertEqual(1, len(cart))

        # check that we cannot add a non-existing object
        session.add_to_cart(self.db, 100, 2)
        cart = session.get_cart_contents()
        self.assertEqual(1, len(cart), "Error when adding non-existing object")

        # check that we cannot add an item that exceeds amount in inventory
        session.add_to_cart(self.db, 10, 100)
        cart = session.get_cart_contents()
        self.assertEqual(1, len(cart), "Quantity exceeded amount in inventory")

        # check that we cannot add an item with non-positive amount
        session.add_to_cart(self.db, 10, -3)
        cart = session.get_cart_contents()
        self.assertEqual(1, len(cart), "Adding item with negative amount")
        session.add_to_cart(self.db, 10, 0)
        cart = session.get_cart_contents()
        self.assertEqual(1, len(cart), "Adding item with zero amount")
예제 #3
0
    def test_cart_update(self):
        """We can update the quantity of an item in the cart"""

        # first need to force the creation of a session and
        # add the cookie to the request
        sessionid = session.get_or_create_session(self.db)
        self.assertIsNotNone(sessionid)
        request.cookies[session.COOKIE_NAME] = sessionid

        # add something to the cart
        product = self.products['Yellow Wool Jumper']
        quantity = 3
        session.add_to_cart(self.db, product['id'], quantity)
        cart = session.get_cart_contents(self.db)

        self.assertEqual(1, len(cart))
        # compare ids as strings to be as flexible as possible
        self.assertEqual(str(product['id']), str(cart[0]['id']))
        self.assertEqual(quantity, cart[0]['quantity'])

        # now add again to the cart, check that we still have one item and the
        # quantity is doubled
        session.add_to_cart(self.db, product['id'], quantity)
        cart = session.get_cart_contents(self.db)

        self.assertEqual(1, len(cart))
        self.assertEqual(product['id'], cart[0]['id'])
        self.assertEqual(quantity * 2, cart[0]['quantity'])
예제 #4
0
def cart_add(db):
    """Render the cart(POST) page"""
    cookieId = session.get_or_create_session(db)
    quantity = int(request.forms.get('quantity'))
    productId = request.forms.get('product')
    session.add_to_cart(db, productId, quantity)
    return redirect("/cart")
예제 #5
0
def post_cart_page(db):
    session.get_or_create_session(db)
    new_product = request.forms.get("product")
    new_quantity = request.forms.get("quantity")
    session.add_to_cart(db, new_product, new_quantity)
    session.get_or_create_session(db)
    redirect('/cart')
예제 #6
0
def index(db):
    session.get_or_create_session(db)
    productid = request.forms.get("product")
    quantity = request.forms.get("quantity")
    session.add_to_cart(db, productid, quantity)
    session.get_or_create_session(db)
    redirect('/cart')
예제 #7
0
def index(db):
    """This is a post method which is triggered when the user selects a quantity on the product page.
    This function further calls the add_to_cart function implemented in the session.py file
    Two values are taken from the page, product ID and quantity selected by the user.
    This function redirects to the same page but with a get method that is implemented ahead.
    To understand how add_to_cart function works, refer to the function add_to_cart in session.py"""
    session.add_to_cart(db, request.forms.get('product'),
                        request.forms.get('quantity'))
    return redirect('cart')
예제 #8
0
파일: main.py 프로젝트: natlec/WT
def addToCart(db):

    # Get or set session cookie
    session.get_or_create_session(db)

    # Add item to cart
    session.add_to_cart(db, request.forms.get('product'),
                        request.forms.get('quantity'))

    # Redirect to cart page
    return redirect('cart')
예제 #9
0
def cart(db):
    """with variables `product` (the product id, an integer) and `quantity` (an integer) adds a new entry to the
shopping cart.  The response to the POST request is a **redirect to the cart page `/cart`**. When
the browser gets this redirect response it will make a new GET request to `/cart` and the
resulting page will contain the updated shopping cart contents."""
    session.get_or_create_session(db)
    product_id = request.forms.get('product')
    product_quantity = request.forms.get('quantity')
    if int(product_quantity) < 0:
        product_quantity = 0
    # add_to_cart(product_id, product_quantity)
    session.add_to_cart(db, product_id, product_quantity)
    # return show_cart(db)
    return redirect("/cart")
예제 #10
0
파일: main.py 프로젝트: hventer/comp249_a2
def postcart(db):
    """The POST cart page. Will create/get cookie
    When submit is pressed on a product page, this
    will run. It will get the product id and quantity
    selected from the form. It will then add the id
    and quantity to the cart.
    Finishes by redirecting to the GET cart page
    """
    session.get_or_create_session(db)

    product = request.forms.get('product')
    quantity = request.forms.get('quantity')

    session.add_to_cart(db, product, quantity)

    redirect("/cart")
예제 #11
0
def cart_add(db):
    """Render the cart(POST) page"""

    key = session.get_or_create_session(db)

    quantity = int(request.forms.get('quantity'))
    product = request.forms.get('product')

    session.add_to_cart(db, product, quantity)
    current = session.get_cart_contents(db)
    cost = 0
    for each in current:
        cost += each['cost'] * each['quantity']
    cart = {
        'content': current,
        'total': cost,
    }
    return redirect("/cart")
예제 #12
0
    def test_add_to_cart(self):
        """We can add items to the shopping cart
        and retrieve them"""

        # We start one session
        cart = []
        request.environ['beaker.session'] = MockBeakerSession({'cart': cart})
        self.assertEqual([], session.get_cart_contents())

        # now add something to the cart
        for pname in ['Yellow Wool Jumper', 'Classic Varsity Top']:
            product = self.products[pname]
            session.add_to_cart(self.db, product['id'], 1)

        cart = session.get_cart_contents()
        self.assertEqual(2, len(cart))

        # check that all required fields are in every cart entry
        for entry in cart:
            self.assertIn('id', entry)
            self.assertIn('name', entry)
            self.assertIn('quantity', entry)
            self.assertIn('cost', entry)

        # We start a session again and check that the cart is empty
        cart = []
        request.environ['beaker.session'] = MockBeakerSession({'cart': cart})
        self.assertEqual([], session.get_cart_contents())

        # now add something to the cart in the new session
        for pname in [
                'Yellow Wool Jumper', 'Silk Summer Top', 'Zipped Jacket'
        ]:
            product = self.products[pname]
            session.add_to_cart(self.db, product['id'], 1)

        cart = session.get_cart_contents()
        self.assertEqual(3, len(cart))
예제 #13
0
    def test_cart_update(self):  # Not passed.....see below
        """We can update the quantity of an item in the cart"""

        # first need to force the creation of a session and
        # add the cookie to the request
        sessionid = session.get_or_create_session(self.db)
        self.assertIsNotNone(sessionid)
        request.cookies[session.COOKIE_NAME] = sessionid

        # add something to the cart
        product = self.products['Yellow Wool Jumper']
        quantity = 3
        session.add_to_cart(self.db, product['id'], quantity)
        cart = session.get_cart_contents(self.db)

        self.assertEqual(1, len(cart))
        self.assertEqual(product['id'], cart[0]['id'])
        self.assertEqual(quantity, cart[0]['quantity'])

        # now add again to the cart, check that we still have one item and the
        # quantity is doubled
        session.add_to_cart(self.db, product['id'],
                            quantity)  # -----> extra parameter: update=False
        # session.add_to_cart(self.db, product['id'], quantity, update=False)
        cart = session.get_cart_contents(self.db)

        self.assertEqual(1, len(cart))
        self.assertEqual(product['id'], cart[0]['id'])
        # self.assertEqual(quantity * 2, cart[0]['quantity'])
        self.assertEqual(
            str(quantity * 2),
            cart[0]['quantity'])  # ------>  quantity needs to be string

        # now add again but with the update flag set, this should
        # set the quantity rather than adding to it
        session.add_to_cart(self.db, product['id'],
                            quantity)  # -----> extra parameter: update=True
        # session.add_to_cart(self.db, product['id'], quantity, update=True)
        cart = session.get_cart_contents(self.db)

        self.assertEqual(1, len(cart))
        self.assertEqual(product['id'], cart[0]['id'])
        self.assertEqual(
            quantity, cart[0]['quantity']
        )  # 9!=3 means what? We are not told about the update flag in the readme file
예제 #14
0
    def test_cart_update(self):
        """We can update the quantity of an item in the cart"""

        # first need to force the creation of a session with an empty cart
        cart = []
        request.environ['beaker.session'] = MockBeakerSession({'cart': cart})

        # add something to the cart
        product = self.products['Yellow Wool Jumper']
        quantity = 3
        session.add_to_cart(self.db, product['id'], quantity)
        cart = session.get_cart_contents()

        self.assertEqual(1, len(cart))
        # compare ids as strings to be as flexible as possible
        self.assertEqual(str(product['id']), str(cart[0]['id']))
        self.assertEqual(quantity, cart[0]['quantity'])

        # now add again to the cart, check that we still have one item and the
        # quantity is doubled
        session.add_to_cart(self.db, product['id'], quantity)
        cart = session.get_cart_contents()

        self.assertEqual(1, len(cart))
        self.assertEqual(product['id'], cart[0]['id'])
        self.assertEqual(quantity * 2, cart[0]['quantity'])

        # now add a third time, this time exceeding the amount in inventory, check that
        # we still have one item and the quantity has not changed
        session.add_to_cart(self.db, product['id'], 100)
        cart = session.get_cart_contents()

        self.assertEqual(1, len(cart),
                         "Test adding excessive quantity of products")
        self.assertEqual(product['id'], cart[0]['id'],
                         "Test adding excessive quantity of products")
        self.assertEqual(quantity * 2, cart[0]['quantity'],
                         "Test adding excessive quantity of products")
예제 #15
0
def form_handler(db):
    session.get_or_create_session(db)
    quantity = request.forms.get('quantity')
    item_id = request.forms.get('product')
    session.add_to_cart(db, itemid=item_id, quantity=quantity)
    redirect('/cart')