Ejemplo n.º 1
0
def categorys_tag2bit(categorys_tag):
    '''
    Convert categorys from tag to bit.
    '''
    categorys_bit = 0
    for category_tag in categorys_tag:
        if category_tag["category_checked"] == True:
            status = yield from Category.findNumber('count(id)', 'name="%s" and id="%s"' % (category_tag["category_name"], category_tag["category_id"]))
            # verify the status of category record.
            if 1 == status:
                categorys_bit += (1 << int(category_tag["category_id"]))
    return categorys_bit
Ejemplo n.º 2
0
def api_update_categroy(request, *, id, name, weight):
    check_admin(request)
    if not name or not name.strip():
        raise APIValueError("name", "错误:类别名不应为空,请填写类别名。")
    if not weight:
        raise APIValueError("weight", "错误:类别权重区间为1~100,请设置正确的值。")
    if int(weight) < 1 or int(weight) > 100:
        raise APIValueError("weight", "错误:类别权重区间为1~100,请设置正确的值。")
    sameName = yield from Category.findNumber('count(id)', 'name="%s" and id!="%s"' % (name, id))
    if not 0 == sameName:
        raise APIValueError("name", "错误:已存在同名类别,请使用其他类别名。")
    category = Category(id=id, name=name, weight=weight)
    yield from category.update()
    return category
Ejemplo n.º 3
0
def api_add_category(request, *, nameAdded, weightAdded):
    check_admin(request)
    if not nameAdded or not nameAdded.strip():
        raise APIValueError("name", "错误:类别名不应为空,请填写类别名。")
    if not weightAdded:
        raise APIValueError("weight", "错误:类别权重区间为1~100,请设置正确的值。")
    if int(weightAdded) < 1 or int(weightAdded) > 100:
        raise APIValueError("weight", "错误:类别权重区间为1~100,请设置正确的值。")
    sameName = yield from Category.findNumber('count(id)', 'name="%s"' % nameAdded)
    if not 0 == sameName:
        raise APIValueError("name", "错误:已存在同名类别,请使用其他类别名。")
    idAdded = 64
    for i in range(64):
        matchItem = yield from Category.find(i)
        if None == matchItem:
            idAdded = i
            break
    if idAdded == 64:
        raise APIValueError("name", "错误:最多支持64个分类,资源不足。")
    category = Category(id=idAdded, name=nameAdded, weight=weightAdded)
    rows = yield from category.save()
    if not rows == 1:
        raise APIValueError("name", "错误:数据库操作失败。")
    return category