def cars(): cars_query = (Cars.select(Cars.car_plate, Users.first_name, Users.last_name, Users.phone_number, CarColors.color_name).join(Users).join( CarColors, on=(Cars.color_id == CarColors.color_id))) carsList = list(cars_query.dicts()) print(carsList) return render_template('cars.html', carsList=carsList)
def add_car(): print('kan1', request.method) if request.method == 'GET': cars_query = CarColors.select() car_colors = list(cars_query.dicts()) users_query = Users.select() users = list(users_query.dicts()) return render_template('add_car.html', carColors=car_colors, users=users) elif request.method == 'POST': print('kan') try: car_to_add = get_car_to_update() Cars.insert(car_to_add).execute() return redirect(url_for('cars')) except: return redirect(url_for('cars'))
def store_car(): cars = Store_Cars() if cars.validate_on_submit(): car = Cars(location=cars.location.data, price=cars.price.data, numcars=cars.numcars.data, numavail=cars.numavail.data, ) db.session.add(car) db.session.commit() flash('Congratulations!') return render_template('end.html', title='旅游预定系统') return render_template('store_car.html', title='旅游预定系统-出租车入库', form=cars)
def delete_car(car_plate): print(car_plate) try: car_to_delete = Cars.get_by_id(car_plate) print('cartodelete', car_to_delete) car_to_delete.delete_instance() flash('Delete successfully.', 'danger') except: flash('Error delete car.', 'danger') return redirect(url_for('cars'))
def get_car(id_car): if request.method == 'GET': try: car = Cars.get(Cars.id == id_car) return jsonify(model_to_dict(car)) except: return jsonify({'error': 'Not found object'}) if request.method == 'PUT': try: car = Cars.get(Cars.id == id_car) if not request.json: abort(400) print(car.brand) data = { 'brand': request.json.get('brand', ""), 'car_model': request.json.get('car_model', ""), 'year_release': request.json.get('year_release'), 'price': request.json.get('price'), 'dealer': request.json.get('dealer'), } car.brand = data['brand'] car.car_model = data['car_model'] car.year_release = data['year_release'] car.price = data['price'] car.dealer = data['dealer'] car.save() return jsonify({'result': 'done'}) except Exception as ex: return jsonify({'error': str(ex)}) if request.method == 'DELETE': try: car = Cars.get(Cars.id == id_car) car.delete_instance() return jsonify({'result': 'done'}) except Exception as ex: return {'error': str(ex)}
def edit_car(car_plate): if request.method == 'GET': cars_query = CarColors.select() car_colors = list(cars_query.dicts()) users_query = Users.select() users = list(users_query.dicts()) cars_query = (Cars.select( Cars.car_plate, Cars.user_id, Cars.color_id).where(Cars.car_plate == car_plate)) car = list(cars_query.dicts())[0] return render_template('edit_car.html', car=car, users=users, carColors=car_colors) elif request.method == 'POST': try: car_to_update = get_car_to_update() Cars.update(car_to_update).where( Cars.car_plate == car_plate).execute() flash('Updated car successfully.', 'success') return redirect(url_for('cars')) except: flash('Couldnt update car.', 'danger') return redirect(url_for('cars'))
def post(): response = request.get_json(force=True) try: data = car_schema.load(response) except ValidationError as err: return {"Error": f"{err}"}, 400 car = Cars(**data) try: db.session.add(car) db.session.commit() except exc.IntegrityError as err: return { "Error": "Database specific error occured. The car is already presented in system" }, 409 else: result = car_schema.dump(Cars.query.get(car.car_id)) return {"message": "Created new car.", "car": result}, 200
def get_data_cars(): if request.method == 'GET': cars = Cars.select() result = [] for car in cars: result.append(model_to_dict(car)) return jsonify(result) if request.method == 'POST': if not request.json or not 'dealer' in request.json: abort(400) data = { 'brand': request.json.get('brand', ""), 'car_model': request.json.get('car_model', ""), 'year_release': request.json.get('year_release'), 'price': request.json.get('price'), 'dealer': request.json.get('dealer'), } new_car = dict_to_model(Cars, data) new_car.save() return jsonify({'result': data}), 201