Ejemplo n.º 1
0
def eshop_cart_add(req):
    do_check_mgc(req)
    check_token(req, req.json.get('token'))
    cart = ShoppingCart(req)

    item_id = req.json.getfirst('item_id', fce=nint)
    count = req.json.getfirst('count', 0, int)
    if count < 1:
        req.state = state.HTTP_BAD_REQUEST
        req.content_type = 'application/json'
        return json.dumps({'reason': 'count must bigger then zero'})

    item = Item(item_id)
    if not item.get(req) or item.state != STATE_VISIBLE:
        req.state = state.HTTP_NOT_FOUND
        req.content_type = 'application/json'
        return json.dumps({'reason': 'item not found'})

    # append or incrase item
    cart.merge_items(((item_id, {'name': item.name,
                                 'price': item.price,
                                 'count': count
                                 }),))
    cart.store(req)
    cart.calculate()
    req.content_type = 'application/json'
    return json.dumps({'reason': 'item append to cart', 'cart': cart.dict()})
Ejemplo n.º 2
0
def eshop_orders_detail(req, id):
    do_check_mgc(req)
    item = Item(id)
    if not item.get(req):
        raise SERVER_RETURN(state.HTTP_NOT_FOUND)
    item.attachments = Attachment.list(req, Pager(),
                                       object_type='eshop_item', object_id=id)
    return generate_page(req, "eshop/item_detail.html",
                         token=create_token(req),
                         item=item, cfg_currency=req.cfg.eshop_currency)
Ejemplo n.º 3
0
def eshop_orders_eshop(req):
    do_check_mgc(req)
    pager = Pager()
    pager.bind(req.args)

    items = Item.list(req, pager, state=STATE_VISIBLE)
    return generate_page(req, "eshop/eshop.html",
                         token=create_token(req),
                         cfg_currency=req.cfg.eshop_currency,
                         pager=pager, items=items)
Ejemplo n.º 4
0
def eshop_cart_address(req, cart=None, error=None):
    do_check_mgc(req)
    cart = cart or ShoppingCart(req)

    # get method returns HTML Form
    cfg = Object()
    cfg.addresses_country = req.cfg.addresses_country
    cfg.addresses_region = req.cfg.addresses_region
    cfg.eshop_currency = req.cfg.eshop_currency
    # all defined transportation (for universal use):
    for key, val in req.cfg.__dict__.items():
        if key.startswith('eshop_transportation_'):
            cfg.__dict__[key[6:]] = val
        elif key.startswith('eshop_payment_'):
            cfg.__dict__[key[6:]] = val

    # GET method only view shopping cart - no store was needed
    return generate_page(req, "eshop/shopping_address.html",
                         token=create_token(req),
                         cfg=cfg, cart=cart, error=error)
Ejemplo n.º 5
0
def eshop_cart_pay_and_order(req):
    do_check_mgc(req)
    check_token(req, req.form.get('token'), uri='/eshop/cart/recapitulation')
    cart = ShoppingCart(req)
    # TODO: payment page if could be (paypal, card, transfer)
    order = Order.from_cart(cart)
    if not order:
        redirect(req, '/eshop')
    order.client_id = req.login.id if req.login else None
    retval = order.add(req)
    if retval == order:
        cart.clean(req)
        send_order_status(req, order)
        return generate_page(req, "eshop/shopping_accept.html",
                             order=order)
    if retval[0] == EMPTY_ITEMS:
        redirect(req, '/eshop')
    if retval[0] == NOT_ENOUGH_ITEMS:
        cart.set_not_enought(retval[1])
        cart.store(req)
        redirect(req, '/eshop/cart')
Ejemplo n.º 6
0
def eshop_cart(req):
    do_check_mgc(req)
    cart = ShoppingCart(req)

    if req.method == 'PATCH':
        check_token(req, req.json.get('token'), uri='/eshop/cart')
        cart.merge_items(req.json.get('items', []))
        req.content_type = 'application/json'
        cart.store(req)     # store shopping cart
        cart.calculate()
        return json.dumps({'cart': cart.dict()})

    cart.calculate()
    if req.is_xhr:
        check_origin(req)
        req.content_type = 'application/json'
        return json.dumps({'cart': cart.dict()})

    # GET method only view shopping cart - no store was needed
    return generate_page(req, "eshop/shopping_cart.html",
                         token=create_token(req),
                         cfg_currency=req.cfg.eshop_currency, cart=cart)
Ejemplo n.º 7
0
def eshop_cart_address_post(req):
    do_check_mgc(req)
    check_token(req, req.form.get('token'), uri='/eshop/cart/address')
    cart = ShoppingCart(req)

    way = req.form.getfirst('way', '', str)
    same_as_billing = 'same_as_billing' in req.form
    billing_address = Address.bind(req.form, 'billing_')
    if same_as_billing:
        shipping_address = billing_address.copy()
        shipping_address['same_as_billing'] = True
    else:
        shipping_address = Address.bind(req.form, 'shipping_')
        shipping_address['same_as_billing'] = False
    transportation = req.form.getfirst('transportation', '', str)
    payment = req.form.getfirst('payment', '', str)
    if req.login:
        email = req.login.email
        emailcheck = email
    else:
        email = req.form.getfirst('email', '', str)
        emailcheck = req.form.getfirst('emailcheck', '', str)

    transportation_price = req.cfg.__dict__.get(
        'eshop_transportation_' + transportation, -1)
    payment_price = req.cfg.__dict__.get(
        'eshop_payment_' + payment, -1)

    if transportation and transportation_price < 0:
        raise SERVER_RETURN(state.HTTP_BAD_REQUEST)
    if payment and payment_price < 0:
        raise SERVER_RETURN(state.HTTP_BAD_REQUEST)

    if len(billing_address):
        cart.billing_address = billing_address
    if len(shipping_address):
        cart.shipping_address = shipping_address
    if transportation:
        cart.transportation = (transportation, transportation_price)
    if payment:
        cart.payment = (payment, payment_price)
    if re_email.match(email):
        cart.email = email
    if re_email.match(emailcheck):
        cart.emailcheck = emailcheck

    cart.store(req)     # store shopping cart

    if not billing_address:
        return eshop_cart_address(req, cart, error='no_billing_address')
    if len(shipping_address) == 1:  # only same_as_billing
        return eshop_cart_address(req, cart, error='no_shipping_address')
    if not email or email != emailcheck:
        return eshop_cart_address(req, cart, error='no_email')
    if not transportation:
        return eshop_cart_address(req, cart, error='no_transportation')
    if not payment:
        return eshop_cart_address(req, cart, error='no_payment')
    # end of errors block

    cart.calculate()

    if way == 'next':
        redirect(req, '/eshop/cart/recapitulation')
    elif way == 'prev':
        redirect(req, '/eshop/cart')
    return eshop_cart_address(req, cart)