示例#1
0
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
0
 def put(self, id):
     '''This method receives an ID from an category and updates the category'''
     if Commons.isValidId(id):
         category = Category.objects(id=id)
         if Commons.checkIfNotExists(category):
             return Commons.notFound('category')
         if auth.isAuthorized(category[0].userId):
             params = Commons.filterQueryParams(self.reqparse.parse_args())
             Category.objects(id=id).update_one(upsert=False,
                                                write_concern=None,
                                                **params)
             return make_response(jsonify({'data': 'Category updated'}),
                                  201)
     return auth.unauthorized()
示例#3
0
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)
示例#4
0
文件: cache.py 项目: phuclc/dienthoai
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)
示例#5
0
 def get(self, id):
     '''This method receives an ID from an category and returns the category'''
     if Commons.isValidId(id):
         category = Category.objects(id=id)
         if Commons.checkIfNotExists(category):
             return Commons.notFound('category')
         if auth.isAuthorized(category[0].userId):
             return make_response(jsonify({'data': category}), 201)
     return auth.unauthorized()
示例#6
0
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)
示例#7
0
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")
 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)
示例#9
0
 def get(self, cat_id):
     cat = Category.objects(id=cat_id)[0]
     return cat.to_json()
示例#10
0
 def get(self):
     root = Category.objects(is_root=True)
     return root[0].to_json()
示例#11
0
 def get(self):
     cats = Category.objects()
     return cats.to_json()
示例#12
0
def index():
    categorys = Category.objects()
    return render.template("admin/category/index.html", categorys=categorys)
示例#13
0
def add():
    categories = Category.objects()
    return render.template("admin/category/create.html", categories=categories)
示例#14
0
 def get(self):
     '''This method returns all categories from an user'''
     categories = Category.objects.all() if auth.isAdmin(
     ) else Category.objects(userId=auth.user['id'])
     return Commons.notFound('category') if Commons.checkIfNotExists(
         categories) else make_response(jsonify({'data': categories}), 201)