def edit_city(): form = CityForm() city = CityModel() if form.validate_on_submit(): city_id = request.form['city_id'] city_name = request.form['city_name'] result, error = city.edit(city_id, city_name) if error: return list_city(error) return list_city(form=form)
def list_city(error=None, form=None): if form is None: form = CityForm() page = request.args.get('page', 1, type=int) city = CityModel() cities, total_pages = city.query_paginate(page) return render_template('CRUD/city/list.html', total_pages=total_pages, city_active="active", form=form, error=error)
def list_city_api(): page = request.args.get('page', 1, type=int) city = CityModel() cities, total_pages = city.query_paginate(page) res = { "total_pages": total_pages, "data": [{ "id": str(city.id), "name": city.name } for city in cities], } return make_response(jsonify(res), 200)
def list_district(error=None, form=None): city = CityModel() if form is None: form = DistrictForm() district = DistrictModel() page = request.args.get('page', 1, type=int) districts, total_pages = district.query_paginate(page) cities = city.query_all() return render_template('CRUD/district/list.html', total_pages=total_pages, cities=cities, district_active="active", form=form)
def list_addresses(error=None, form=None): if form is None: form = AddressForm() city = CityModel() address = AddressModel() page = request.args.get("page", 1, type=int) addresses_pagination, total_page, current_page = address.query_paginate(page) addresses = [{'city': address.district.city.name, 'city_id': address.district.city.id, 'district': address.district.name, 'district_id': address.district.id, 'detail': address.detail, 'id': address.id} for address in addresses_pagination] list_city = city.query_all() return render_template('CRUD/address/list.html', addresses=addresses, total_page=total_page, current_page=current_page, list_city=list_city, address_active="active", form=form, error=error)
def create_address(): form = AddressForm() city = CityModel() list_city = city.query_all() if form.validate_on_submit(): if request.method == 'POST': district_id = request.form.get('select-district') detail = request.form.get('address_name') result, error = AddressModel.create(district_id, detail) if error: return render_template('CRUD/address/create.html', list_city=list_city, address_active="active", form=form, error=error) return redirect('/address') return render_template('CRUD/address/create.html', list_city=list_city, address_active="active", form=form)
def list_district_api(): district = DistrictModel() city = CityModel() page = request.args.get('page', 1, type=int) districts, total_pages = district.query_paginate(page) res = { "total_pages": total_pages, "data": [{ "id": str(district.id), 'city_name': city.get_name_by_id(district.city_id), "name": district.name } for district in districts], } return make_response(jsonify(res), 200)
def create_district(error=None): form = DistrictForm() city = CityModel() district = DistrictModel() cities = city.query_all() if form.validate_on_submit(): if request.method == 'POST': district_name = request.form['district_name'] city_id = request.form['city_id'] district_exist = district.find(district_name) if district_exist is None: return district.create(district_name, city_id) and redirect('/district') else: error = "Your district is error" return render_template('CRUD/district/create.html', cities=cities, error=error, district_active="active", form=form)
def create_city(error=None): form = CityForm() if form.validate_on_submit(): if request.method == 'POST': city_name = request.form['city_name'] result, error = CityModel.create(city_name) if error: return list_city(error) return redirect('/city') return render_template('CRUD/city/create.html', error=error, city_active="active", form=form)