Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
def valueobject_get_all(r, votype, status, merge, return_dict=False):
    """ 获取指定 votype 下所有 ValueObject 信息
    """
    vos = get_vo_query(r, votype, status).all()
    if merge > 0:
        return responseto(vos=[vo.merge() for vo in vos],
                          return_dict=return_dict)
    return responseto(vos=vos, return_dict=return_dict)
Beispiel #4
0
def regional_get(r, merge):
    """ 获得一个 regional 项目
    :param merge: 若 merge 为 0,则直接返回原始数据;
                    若 merge 为 1,执行 Regional 中的 merge 方法
    """
    if r is None:
        return responseto('Param please!', code=401)
    robj = Regional.query.get(r)
    if robj is None:
        return responseto('No regional %s!' % r, code=404)

    if merge > 0:
        return responseto(regional=robj.merge(), code=200)
    return responseto(regional=robj, code=200)
Beispiel #5
0
def regional_get_all_trim(kindtype, rtype, rformat):
    """ 获得多个 regional 项目,仅包含 r 和 name

    :param kindtype:
    :param rtype: 1000/2000/5000
    :param rformat: 0 代表返回 json 格式的 list,1 代表仅仅返回 r 的 list
    """
    if rtype is not None and not rtype in Regional.REGIONAL_TYPES:
        return responseto('The rtype of regional %s is unavailable!' % rtype,
                          code=401)
    qry = Regional.get_qry(kindtype=kindtype, rtype=rtype, status=1)
    regionals = qry.with_entities(Regional.r, Regional.name,
                                  Regional.createtime).all()
    if rformat == 1:
        regionals = [ritem.r for ritem in regionals]
    return responseto(regionals=regionals, code=200)
Beispiel #6
0
def regional_get_all(kindtype, rtype, merge):
    """ 获得多个 regional 项目
    """
    rtype = parse_int(rtype)
    kindtype = parse_int(kindtype)

    if rtype is not None:
        if not rtype in Regional.REGIONAL_TYPES:
            return responseto('The rtype of regional %s is unavailable!' %
                              rtype,
                              code=401)

    regionals = Regional.get_qry(kindtype=kindtype, rtype=rtype).all()
    if merge > 0:
        regionals = [regional.merge() for regional in regionals]
    return responseto(regionals=regionals, code=200)
Beispiel #7
0
def regional_del(r):
    """  删除一个 regional
    """
    if r is None:
        return responseto('Param please!', code=401)
    # 必须先删除所有的与这个 Regional 相关的关系
    if ValueObject.query.filter_by(r=r).first() is not None:
        return responseto('Please delete valueobjet of %s first!' % r,
                          code=403)
    try:
        Regional.query.filter_by(r=r).delete()
        gdb.session.commit()
    except SQLAlchemyError as e:
        msg = 'regional_del error: ' + str(e)
        logger.error(msg)
        return responseto(msg, code=500)
    return responseto(code=200)
Beispiel #8
0
def regional_get_more(page, per_page, kindtype, status, merge):
    """ 分页获取指定 votype 下的 ValueObject 信息
    """
    if merge > 0:
        return_method = lambda vos: [vo.merge() for vo in vos]
    else:
        return_method = 'model'
    qry = Regional.get_qry(kindtype=kindtype, status=status)
    rdata = get_page_response(qry, page, per_page, 'regionals', return_method)
    return responseto(data=rdata)
Beispiel #9
0
def valueobject_get(r, vid, name, merge, withcache, return_dict=False):
    """ 获取单个 ValueObject 信息,支持通过  vid 和 name
    """
    if withcache > 0:
        # 如果使用 withcache,必须提供 name
        if name is None:
            return responseto('请提供 name!', code=401, return_dict=return_dict)
        value_in_cache = _get_vo_by_cache(r, name)
        if value_in_cache is None:
            return responseto('no vo like this.',
                              code=404,
                              return_dict=return_dict)
        return responseto(vo=value_in_cache, return_dict=return_dict)

    # 从数据库中查询
    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()
    else:
        return responseto('vid or name please!',
                          code=401,
                          return_dict=return_dict)
    if vo is None:
        return responseto('no vo like this.',
                          code=404,
                          return_dict=return_dict)
    if merge > 0:
        vo = vo.merge()
    return responseto(vo=vo, return_dict=return_dict)
Beispiel #10
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)
Beispiel #11
0
def valueobject_get_more(r,
                         page,
                         per_page,
                         votype,
                         status,
                         merge,
                         return_dict=False):
    """ 分页获取指定 votype 下的 ValueObject 信息
    """
    if merge > 0:
        return_method = lambda vos: [vo.merge() for vo in vos]
    else:
        return_method = 'model'
    qry = get_vo_query(r, votype, status)
    rdata = get_page_response(qry, page, per_page, 'vos', return_method)
    return responseto(data=rdata, return_dict=return_dict)
Beispiel #12
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)
Beispiel #13
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)