def categories_manipulation(id, **kwargs): category = Category.query.filter_by(id=id).first() if not category: # Raise an HTTPException with a 404 not found status code abort(404) # DELETE method if request.method == 'DELETE': Category.delete(category) return { 'message': "Category {} deleted successfully.".format(category.id) } # PUT method elif request.method == 'PUT': name = request.values.get('name') url = request.values.get('url') if name is not None: category.name = name if url is not None: category.url = url category.save() Response = jsonify({ 'id': category.id, 'name': category.name, 'url': category.url }) Response.status_code = 200 return Response
def test_delete_a_category(self): """ Delete a Category """ category = Category(name="Dog") category.save() self.assertEqual(len(Category.all()), 1) # delete the category and make sure it isn't in the database category.delete() self.assertEqual(len(Category.all()), 0)
def test_05_delete_cate(self): """测试删除分类""" cate1 = Category.query.filter_by(_name='cate1').first() cate2 = Category.query.filter_by(_name='cate2').first() cate3 = Category.query.filter_by(_name='cate3').first() p = Post() Post.publish(post=p, title='post1', content='post1', category=cate3) Category.delete(cate2) self.assertTrue(Category.query.count() == 2 and p.category == cate3 and cate3.parent == cate1 and cate1.posts_count == 1)
def post(self, menuid): try: form = request.get_json(force=True) name = form.get('name') if name is None: return {'message': 'Dish name is required'}, 400 rank = form.get('rank') if rank is None: return {'message': 'Dish rank is required'}, 400 try: rank = int(rank) except Exception as e: print(e) return {'message': 'Dish rank must be integer'}, 400 category = Category() category.name = name category.rank = rank category.menuId = menuid category.delete = False db.session.add(category) db.session.commit() return { 'message': 'Add a new category successfully', 'id': category.id }, 200 except Exception as e: print(e) return {'message': 'Internal Server Error'}, 500
def delete(self, id): category = Category.get_by_id(id) try: if category is None: raise Exception(_('CATEGORY_NOT_FOUND')) if not category.can_edit(): abort(401) if not Category.transfer_posts(category): raise Exception(_('CATEGORY_TRANSFER_POSTS_FAILED')) name = category.name Category.delete(category.id) flash(_('CATEGORY_REMOVE_SUCCESS', name=name)) except Exception as e: flash(e.message, 'error') return render_view(url_for('CategoriesView:index'), redirect=True)
def productadd(request): name = request.POST.get("product-category-name") order = request.POST.get("parentid") cat = request.POST.get("jibie") ztt = request.POST.get("chack") photo = request.FILES.get("upload") if ztt == "增加": path = os.path.join(os.getcwd(), 'static\\upload') pathname = os.path.join(path, photo.name) path1 = default_storage.save(pathname, ContentFile(photo.read())) pathname2 = os.path.join('static/upload', photo.name).replace('\\', '/') c = Category(oname=name,cate=cat,path=pathname2,parentid=order) c.save() if ztt == "禁用": c = Category.objects.get(oname=name) c.zt = 0 c.save() if ztt == "删除": c = Category.objects.get(oname=name) c.delete() return render(request,'product-category-add.html')
def delete(self, id): category = Category.get_by_id(id) if category is None: flash(gettext("The category was not found"), "error") return redirect(url_for("CategoriesView:index")) if not category.can_edit(): abort(401) try: if not Category.transfer_posts(category): return util.redirect_json_or_html( url_for("CategoriesView:index"), "category", gettext("Sorry, the last category can not be removed") ) name = category.name Category.delete(category.id) flash(gettext('The category "%(name)s" was removed', name=name)) except: return util.redirect_json_or_html( url_for("CategoriesView:index"), "category", gettext("Error while removing the category") ) return util.redirect_json_or_html(url_for("CategoriesView:index"), "category")
def test_06_delete_default_cate(self): """测试删除默认分类""" cate1 = Category.query.get(1) self.assertRaises(AttributeError, lambda: Category.delete(cate1))