def POST(self, id): i = web.input() name = i.name.strip() slug = None if i.slug.strip(): # should validate slug slug = i.slug.strip().replace(" ", "-") desc = i.desc.strip() if not name: # name can't be empty web.ctx.msg = "Category name can not be empty" raise web.seeother("/category/edit/%s" % id) cate = web.ctx.orm.query(Term).filter(Term.type == "category").filter(Term.id == int(id)).first() # if the category get a new name if cate.name != name: count = web.ctx.orm.query(Term).filter(Term.type == "category").filter(Term.name == name).count() if count > 0: web.ctx.msg = "Category '%s' already exist, change a name!" % name raise web.seeother("/category/edit/%s" % id) # validates slug if slug: if not term_slug_validates(slug, int(id)): web.ctx.msg = "Category not modified! slug is the same as another category." raise web.seeother("/categories") cate.name = name cate.slug = slug cate.description = desc web.ctx.orm.commit() # update it # web.ctx.msg = "Category '%s' has been modified!" % name raise web.seeother("/categories")
def POST(self): i = web.input() name = i.name.strip() slug = None if i.slug.strip(): slug = i.slug.strip().replace(" ", "-") desc = i.desc.strip() # name should not be empty if not name: web.ctx.msg = "Category name can not be empty." raise web.seeother("/categories") count = web.ctx.orm.query(Term).filter(Term.type == "category").filter(Term.name == name).count() if count > 0: web.ctx.msg = "Category '%s' already exist!" % name raise web.seeother("/categories") # validates slug if slug: if not term_slug_validates(slug): web.ctx.msg = "Category not saved! slug is the same as another category." raise web.seeother("/categories") # add a new category term = Term(name=name, slug=slug, description=desc, type="category") web.ctx.orm.add(term) web.ctx.msg = "Category '%s' has been saved!" % name web.ctx.orm.commit() raise web.seeother("/categories")