def list_products(error=None, form=None): if form is None: form = ProductForm() brand = BrandModel() product = ProductModel() page = request.args.get("page", 1, type=int) products_pagination, total_page, current_page = product.query_paginate( page) products = [{ 'brand': product.category.brand.name, 'brand_id': product.category.brand.id, 'category': product.category.name, 'category_id': product.category.id, 'name': product.name, 'id': product.id } for product in products_pagination] list_brand = brand.query_all() return render_template('CRUD/product/list.html', products=products, total_page=total_page, current_page=current_page, list_brand=list_brand, product_active="active", form=form, error=error)
def list_category(error=None, form=None): brand = BrandModel() if form is None: form = CategoryForm() category = CategoryModel() page = request.args.get('page', 1, type=int) categories, total_pages = category.query_paginate(page) brands = brand.query_all() return render_template('CRUD/category/list.html', total_pages=total_pages, brands=brands, category_active="active", form=form)
def edit_brand(): form = BrandForm() brand = BrandModel() if form.validate_on_submit(): brand_id = request.form['brand_id'] brand_name = request.form['brand_name'] result, error = brand.edit(brand_id, brand_name) if error: return list_brand(error) return list_brand(form=form)
def list_category_api(): category = CategoryModel() brand = BrandModel() page = request.args.get('page', 1, type=int) categories, total_pages = category.query_paginate(page) res = { "total_pages": total_pages, "data": [{"id": str(category.id), 'brand_name': brand.get_name_by_id(category.brand_id), "name": category.name} for category in categories], } return make_response(jsonify(res), 200)
def list_brand(error=None, form=None): if form is None: form = BrandForm() page = request.args.get('page', 1, type=int) brand = BrandModel() brands, total_pages = brand.query_paginate(page) return render_template('CRUD/brand/list.html', total_pages=total_pages, brand_active="active", form=form, error=error)
def list_brand_api(): page = request.args.get('page', 1, type=int) brand = BrandModel() brands, total_pages = brand.query_paginate(page) res = { "total_pages": total_pages, "data": [{ "id": str(brand.id), "name": brand.name } for brand in brands], } return make_response(jsonify(res), 200)
def create_category(error=None): form = CategoryForm() brand = BrandModel() category = CategoryModel() brands = brand.query_all() if form.validate_on_submit(): if request.method == 'POST': category_name = request.form['category_name'] brand_id = request.form['brand_id'] category_exist = category.find(category_name) if category_exist is None: return category.create(category_name, brand_id) and redirect('/category') else: error = "Your category is error" return render_template('CRUD/category/create.html', brands=brands, error=error, category_active="active", form=form)
def create_product(): form = ProductForm() brand = BrandModel() list_brand = brand.query_all() if form.validate_on_submit(): if request.method == 'POST': category_id = request.form.get('select-category') name = request.form.get('product_name') result, error = ProductModel.create(category_id, name) if error: return render_template('CRUD/product/create.html', list_brand=list_brand, product_active="active", form=form, error=error) return redirect('/product') return render_template('CRUD/product/create.html', list_brand=list_brand, product_active="active", form=form)
def create_brand(error=None): form = BrandForm() if form.validate_on_submit(): if request.method == 'POST': brand_name = request.form['brand_name'] result, error = BrandModel.create(brand_name) if error: return list_brand(error) return redirect('/brand') return render_template('CRUD/brand/create.html', error=error, brand_active="active", form=form)