def default_category(cls): category = DBCategory.get_by_url("") if not category: category = DBCategory.create(url="", name="Home") category.save() _ = category.stats # init stats return cls(category)
def update(self, **settings): url = settings.get("url", None) if url is not None: url = Category.norm_url(url) category = DBCategory.get_by_url(url) if category and category.id != self.id: raise Exception("url '%s' exist" % url) settings["url"] = url self.db_object.update(**settings)
def create_category(cls, **settings): url = cls.norm_url(settings.pop("url")) name = settings.pop("name") if cls.check_exist(url=url): raise Exception("category %s exist" % url) dbcategory = DBCategory.create(url=url, name=name) dbcategory.save() _ = dbcategory.stats # init stats dbcategory.update(**settings) return cls(dbcategory)
def check_exist(cls, **kwargs): return DBCategory.check_exist(**kwargs)
def get_by_url(cls, category_url): if not category_url: return cls.default_category() category_url = cls.norm_url(category_url) dbcategory = DBCategory.get_by_url(category_url) return dbcategory and cls(dbcategory)
def get_by_id(cls, id): dbcategory = DBCategory.get_by_id(id) return dbcategory and cls(dbcategory)