Exemplo n.º 1
0
Arquivo: rfun.py Projeto: zrong/pyape
def regional_add(r, name, value, kindtype, status):
    """ 增加一个 regional
    """
    if r is None or name is None or value is None:
        return responseto('Param please!', code=401)
    kindtype = parse_int(kindtype, 0)
    try:
        toml.loads(value)
    except toml.TomlDecodeError as e:
        msg = 'value is not a TOML string: %s' % str(e)
        logger.error(msg)
        return responseto(msg, code=401)

    now = int(time.time())
    robj = Regional(r=r,
                    name=name,
                    value=value,
                    status=status,
                    kindtype=kindtype,
                    createtime=now,
                    updatetime=now)
    resp = commit_and_response_error(robj, refresh=True)
    if resp is not None:
        return resp
    return responseto(regional=robj, code=200)
Exemplo n.º 2
0
Arquivo: rfun.py Projeto: zrong/pyape
def regional_edit(r, name, value, kindtype, status):
    """ 修改一个 regional
    """
    status = parse_int(status, 1)
    kindtype = parse_int(kindtype)
    if r is None:
        return responseto('Param please!', code=401)
    robj = Regional.query.get(r)
    if robj is None:
        return responseto('找不到 regional %s!' % r, code=404)
    if name is not None:
        robj.name = name
    if value is not None:
        try:
            toml.loads(value)
            robj.value = value
        except toml.TomlDecodeError as e:
            msg = 'value is not a TOML string: %s' % str(e)
            logger.error(msg)
            return responseto(msg, code=401)
    if kindtype is not None:
        robj.kindtype = kindtype
    if status is not None:
        robj.status = status
    robj.updatetime = int(time.time())
    resp = commit_and_response_error(robj, refresh=True)
    if resp is not None:
        return resp
    return responseto(regional=robj, code=200)
Exemplo n.º 3
0
def valueobject_del(vid, name, return_dict=False):
    """ 删除一个 vo,优先使用 vid,然后考虑 name
    """
    vo = None

    if vid is not None:
        vo = ValueObject.query.get(vid)
    elif name is not None:
        vo = ValueObject.query.filter_by(name=name).first()

    if vo is None:
        return responseto('no vo like this.',
                          code=404,
                          return_dict=return_dict)

    r = vo.r
    name = vo.name

    resp = commit_and_response_error(vo, delete=True)
    if resp is not None:
        return resp
    gcache.delg(name, r)

    return responseto(return_dict=return_dict)
Exemplo n.º 4
0
def valueobject_edit(r,
                     withcache,
                     vid=None,
                     name=None,
                     value=None,
                     votype=None,
                     status=None,
                     index=None,
                     note=None,
                     valuetype=None,
                     return_dict=False):
    """ 更新一个 VO
    """
    if vid is not None:
        vid = parse_int(vid)
        if vid is None:
            return responseto(message='vid 必须是整数!',
                              code=401,
                              error=True,
                              return_dict=return_dict)

    if status is not None:
        status = parse_int(status)
        if status is None:
            return responseto(message='status 必须是整数!',
                              code=401,
                              error=True,
                              return_dict=return_dict)

    if index is not None:
        index = parse_int(index)

    try:
        value = valueobject_check_value(value, valuetype)
    except ValueError as e:
        logger.error('valueobject_edit %s', str(e))
        return responseto(message='value 无法正常解析!请检查。',
                          code=401,
                          error=True,
                          return_dict=return_dict)

    voitem = None
    if vid is not None:
        # 提供 vid 代表是修改 vo
        voitem = ValueObject.query.get(vid)
    elif name is not None:
        # 没有提供 vid 但提供了 name 也代表是修改 vo
        voitem = ValueObject.query.filter_by(name=name).first()

    if voitem is None:
        return responseto(message='找不到 vo!',
                          code=404,
                          error=True,
                          return_dict=return_dict)

    voitem.updatetime = int(time.time())

    # 只有明确提供了 status 才设置它
    # 不处理 votype/r ,因为已经创建了的 VO 不允许修改这些关键分类的值
    if name is not None:
        voitem.name = name
    if value is not None:
        voitem.value = value
    if status is not None:
        voitem.status = status
    if note is not None:
        voitem.note = note
    if index is not None:
        voitem.index = index
    if votype is not None:
        voitem.votype = votype

    resp = commit_and_response_error(voitem, refresh=True, return_dict=True)
    if resp is not None:
        return resp

    if withcache > 0:
        valueobj = ValueObject.load_value(value)
        gcache.setg(name, valueobj, r)
    return responseto(vo=voitem,
                      error=False,
                      code=200,
                      return_dict=return_dict)
Exemplo n.º 5
0
def valueobject_add(r,
                    withcache,
                    name,
                    value,
                    votype,
                    status=None,
                    index=None,
                    note=None,
                    valuetype=None,
                    return_dict=False):
    """ 增加一个 VO
    """
    if name is None or value is None or votype is None:
        return responseto(message='必须提供 name/value/votype!',
                          code=401,
                          error=True,
                          return_dict=return_dict)
    if status is not None:
        status = parse_int(status)
        if status is None:
            return responseto(message='status 必须是整数!',
                              code=401,
                              error=True,
                              return_dict=return_dict)
    if index is not None:
        index = parse_int(index)
    try:
        value = valueobject_check_value(value, valuetype)
    except ValueError as e:
        logger.error('valueobject_add %s', str(e))
        return responseto(message='value 无法正常解析!请检查。',
                          code=401,
                          error=True,
                          return_dict=return_dict)

    votype = parse_int(votype)
    if votype is None:
        return responseto(message='请提供 votype!',
                          code=401,
                          error=True,
                          return_dict=return_dict)

    now = int(time.time())
    voitem = ValueObject(name=name,
                         value=value,
                         status=status if status is not None else 1,
                         index=index if index is not None else 0,
                         r=r,
                         votype=votype,
                         createtime=now,
                         updatetime=now,
                         note=note)

    resp = commit_and_response_error(voitem, refresh=True, return_dict=True)
    if resp is not None:
        return resp

    if withcache > 0:
        valueobj = ValueObject.load_value(value)
        gcache.setg(name, valueobj, r)
    return responseto(vo=voitem,
                      error=False,
                      code=200,
                      return_dict=return_dict)