def create(): # Create a new instance of our CarForm class using the request.form # object which will get the request method (GET or POST) as the first # parameter form = CarForm(request.form, csrf_enabled=False) # To check if our require fields have been filled out and then create a new # instance of our Car class and its contructor method will take the form # object if form.validate_on_submit(): car = Car(form) # Since, our object has been created; use an INSERT INTO statement to # add the object's properties to our database query = "INSERT INTO `cars` (`name`, `image`, `topspeed`) VALUES (%s, %s, %s)" # query = "INSERT INTO `cars` (`name`, `image`, `topspeed`) VALUES (%s, %s, %d)" value = (car.name, car.image, car.topspeed) mycursor.execute(query, value) mydb.commit() # If the required fields have been filled out add the car to the # database and redirect to the View Cars page with GET data to # display a success message, else go back to the Add Car page with # GET data to display an error message return redirect('/?add=success') else: return redirect('/add-car?add=error')
def home(): car_form = CarForm() if car_form.validate_on_submit(): car_age = int(car_form.car_age.data) car_fuel = car_form.car_fuel.data car_doors = int(car_form.car_doors.data) car_cc = int(car_form.car_cc.data) car_horsepower = int(car_form.car_horsepower.data) car_transmission = car_form.car_transmission.data car_odometer = int(car_form.car_odometer.data) car_weight = car_form.car_weight.data car_color = car_form.car_color.data return render_template("car.html", form=car_form)
def add(): if "username" in session: usrname = session["username"] form = CarForm(request.form) if request.method == 'POST' and form.validate(): car = Car() save_changes(car, form, new=True) flash('Car created successfully!') return render_template('adminBase.html', form=form) return render_template('addCar.html', form=form) else: flash("You must login first!", "info") return redirect(url_for("login"))
def add(): # Using get() method of the args property of the request object to collect # GET data from the URL get = {"add": request.args.get("add")} # Creating a new instance of our CarForm class with csrf_enabled set to # False form = CarForm(csrf_enabled=False) # Passing our get and form variables to the Jinja template return render_template("add-car.html.j2", site_title=site_title, page_title=site_title + " - Add Car", get=get, form=form)
def edit(id): """ edit a Car in the database """ if "username" in session: usrname = session["username"] car = Car.query.filter(Car.car_id == id).first() if car: form = CarForm(formdata=request.form, obj=car) if request.method == 'POST' and form.validate(): save_changes(car, form) flash('Car updated successfully!') return render_template('adminBase.html', form=form) return render_template('edit_car.html', form=form) else: return 'Error loading #{id}'.format(id=id)
def index(request): form_class = CarForm form = form_class(request.POST or None) if request.method == "POST": if form.is_valid(): car = form.cleaned_data['car'] url = "https://api.restb.ai/classify" querystring = {"client_key":"630baac25e972c9f607047fdc7498322b7adca6469de70684eb9b99a6dae9f4c", "model_id":"car_make_model", "image_url":car} response = requests.request("GET", url, params=querystring) print response.text else: form = CarForm() return render(request, "home/index.html", {"form":form, 'form_class':form_class})
def homepage(): form = CarForm() form.fuel.choices = parse_input_select_data('Fuel') form.euro.choices = parse_input_select_data('Standard Euro Emissions') form.transmission.choices = parse_input_select_data('Transmission') if request.method == 'POST': mpg = request.form['mpg'] fuel = request.form['fuel'] mileage = request.form['mileage'] euro = request.form['euro'] transmission = request.form['transmission'] if fuel == '' or euro == '' or transmission == '': return render_template('pages/landing.html', form=form, result=False, field_error=True) df_input = pd.DataFrame({ 'EC Combined (mpg)': [mpg], 'Fuel': [fuel], 'Mileage': [mileage], 'Standard Euro Emissions': [euro], 'Transmission': [transmission] }) predict_model_facade = PredictModelFacade('data/encode_model.model', 'data/predict_model.model') prediction = predict_model_facade.predict(df_input) return render_template('pages/landing.html', form=form, result=True, price=prediction) else: return render_template('pages/landing.html', form=form)