def add(title, desc, father, icon): assert isinstance(title, basestring), 'title is not string' assert isinstance(desc, basestring), 'title is not string' assert len(title) > 0, 'title name too short!' assert len(desc) > 5, 'desc name too short!' assert father is None or isinstance(father, ObjectId) if father: assert CategoryHelper.get(father) new_category = Category() new_category.title = title new_category.desc = desc if father: new_category.father = father if icon: encoded = b64encode(icon.read()) new_category.icon.new_file() new_category.icon.write(encoded) new_category.icon.close() new_category.save() if father: CategoryHelper.add_son(father, new_category.id) return new_category