本文整理汇总了Python中models.category.Category.objects方法的典型用法代码示例。如果您正苦于以下问题:Python Category.objects方法的具体用法?Python Category.objects怎么用?Python Category.objects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.category.Category
的用法示例。
在下文中一共展示了Category.objects方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update
# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import objects [as 别名]
def update(category_id):
try:
data = request.form.to_dict()
temp_data = data
ancestorpath = []
data = dict((k, v) for (k, v) in data.iteritems() if len(str(v).strip()) > 0)
category = Category.objects.get(id=str(category_id)) # cha
if category is None:
return abort(400)
update_map = dict([("set__" + key, value) for key, value in data.items()])
if update_map.has_key("set__parent"):
if update_map["set__parent"] == category_id:
return abort(400)
parent = Category.objects(id=str(update_map["set__parent"])).first() # con
if parent is None:
return abort(400)
ancestorpath = parent.ancestors
ancestorpath.append(parent)
"""
Check category in child's ancestors when update a category with its parent is its descendant
If category in child's ancestors return True
else return False
"""
def ancestors(parent, child):
if parent.id == child.id:
return True
if child.parent is None:
return False
return ancestors(parent, child.parent)
if ancestors(category, parent):
return abort(400)
if temp_data.has_key("parent"):
if len(str(temp_data["parent"]).strip()) == 0:
update_map["set__parent"] = None
update_map["set__ancestors"] = ancestorpath
category.update(**update_map)
category.reload
# get all child
children = Category.objects(ancestors__contains=category.id)
def getpath(path, child):
if child.parent is None:
return path
path.insert(0, child.parent)
return getpath(path, child.parent)
# update ancestors in child
for child in children:
child_path = getpath([], child)
child.ancestors = child_path
child.save()
rebuilt_category_product()
return render.template("admin/category/detail.html", category=category.reload()), 200
except Exception as e:
print e
abort(400)
示例2: detail
# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import objects [as 别名]
def detail(category_id):
try:
category = Category.objects(id=str(category_id)).first()
if category is None:
return abort(404)
return render.template("admin/category/detail.html", category=category)
except Exception, e:
abort(404)
示例3: rebuilt_category_product
# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import objects [as 别名]
def rebuilt_category_product():
Category_Product.objects().delete()
categories = Category.objects()
for category in categories:
product_of_categorys = Product.objects(category=category.id)
bulk = []
for product_of_category in product_of_categorys:
bulk.append(Category_Product(product=product_of_category.id, category=category.id))
if len(bulk) > 0:
Category_Product.objects.insert(bulk)
children = Category.objects(ancestors__contains=category.id)
for child in children:
product_of_childs = Product.objects(category=child.id)
bulk = []
for product_of_child in product_of_childs:
bulk.append(Category_Product(product=product_of_child.id, category=category.id))
if len(bulk) > 0:
Category_Product.objects.insert(bulk)
示例4: delete
# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import objects [as 别名]
def delete(category_id):
try:
category = Category.objects.get(id=category_id)
children = Category.objects(ancestors__contains=category.id)
num = category.delete()
def getpath(path, child):
if child.parent is None:
return path
path.insert(0, child.parent)
return getpath(path, child.parent)
# update ancestors in child
for child in children:
child_path = getpath([], child)
child.ancestors = child_path
child.save()
return render.template("admin/category/detail.html", category=category)
except Exception:
abort(404)
示例5: edit
# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import objects [as 别名]
def edit(category_id):
try:
category = Category.objects.get(id=category_id)
categories = Category.objects()
def is_can_be_parent(can_be_parent, can_be_child):
def get_ancestors(list, child):
if child is None:
return list
if child.parent is not None:
list.append(child.parent.id)
return get_ancestors(list, child.parent)
"""
to check can_be_parent is child of can_be_child much get all child of can_be_child
to get all child of can_be_child much check all category with each category as c has ancestors,
which has can_be_child => c is child of can_be_child
if can_be_parent in list child of can_be_child return false
"""
list_child_of_child = []
for c in categories:
list = []
list = get_ancestors(list, c)
if can_be_child.id in list:
list_child_of_child.append(c.id)
if can_be_parent.id in list_child_of_child:
return False
if can_be_child.parent is not None:
if can_be_child.parent.id == can_be_parent.id:
return True
if can_be_child.id == can_be_parent.id:
return False
return True
categories = filter(lambda can_be_parent: is_can_be_parent(can_be_parent, category), categories)
return render.template("admin/category/edit.html", category=category, categories=categories)
except Exception:
abort(404, "404 does not exist")
示例6: test_delete_exist_category_should_be_done
# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import objects [as 别名]
def test_delete_exist_category_should_be_done(self):
category = CategoryFactory.create()
resp = self.post('/admin/category/' + str(category.id) + '/delete',{})
expect(len(Category.objects(id=category.id))).to_equal(0)
示例7: index
# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import objects [as 别名]
def index():
categorys = Category.objects()
return render.template("admin/category/index.html", categorys=categorys)
示例8: add
# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import objects [as 别名]
def add():
categories = Category.objects()
return render.template("admin/category/create.html", categories=categories)