Example #1
0
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__})