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()})
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)
def admin_store(req): check_login(req) check_right(req, module_right) pager = Pager(sort='desc') pager.bind(req.args) show = req.args.getfirst('show', '', uni) if show == 'visible': kwargs = {'state': STATE_VISIBLE} pager.set_params(show=show) elif show == 'hidden': kwargs = {'state': STATE_HIDDEN} pager.set_params(show=show) elif show == 'disabled': kwargs = {'state': STATE_DISABLED} pager.set_params(show=show) else: kwargs = {} items = Item.list(req, pager, **kwargs) return generate_page(req, "admin/eshop/store.html", token=create_token(req), pager=pager, items=items, show=show)
def admin_item_mod(req, id): check_login(req) check_right(req, module_right) item = Item(id) if req.method == 'POST': check_token(req, req.form.get('token')) item.bind(req.form) error = item.mod(req) if error != item: return generate_page(req, "admin/eshop/item_mod.html", item=item, error=error) if not item.get(req): # still fresh data raise SERVER_RETURN(state.HTTP_NOT_FOUND) return generate_page(req, "admin/eshop/item_mod.html", token=create_token(req), item=item)
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)
def admin_item_add(req): check_login(req) check_right(req, module_right) item = Item() if req.method == 'POST': check_token(req, req.form.get('token'), uri='/admin/eshop/store/add') item.bind(req.form) error = item.add(req) if error != item: return generate_page(req, "admin/eshop/item_mod.html", item=item, error=error) redirect(req, '/admin/eshop/store/%d' % item.id) # endif return generate_page(req, "admin/eshop/item_mod.html", token=create_token(req), item=item)
def admin_item_incdec(req, id): check_login(req, '/log_in?referer=/admin/eshop/store/%s' % id) check_right(req, module_right) check_token(req, req.form.get('token'), uri='/admin/eshop/store/%s' % id) if req.uri.endswith('/inc'): action_type = ACTION_INC elif req.uri.endswith('/dec'): action_type = ACTION_DEC elif req.uri.endswith('/pri'): action_type = ACTION_PRI else: raise RuntimeError('Unknow action') action = Action.bind(req.form, action_type) item = Item(id) if not item.action(req, action) or not item.get(req): req.status = state.HTTP_NOT_FOUND req.content_type = 'application/json' return json.dumps({'reason': 'item not found'}) req.content_type = 'application/json' return json.dumps({'item': item.__dict__})
def admin_item_state(req, id): check_login(req, '/log_in?referer=/admin/eshop/store') check_right(req, module_right) check_token(req, req.form.get('token'), uri='/admin/eshop/store') item = Item(id) if not item.get(req): raise SERVER_RETURN(state.HTTP_NOT_FOUND) if req.uri.endswith('/visible'): item.set_state(req, STATE_VISIBLE) elif req.uri.endswith('/hidden'): item.set_state(req, STATE_HIDDEN) else: item.set_state(req, STATE_DISABLED) redirect(req, req.referer)