def post(self, request, id=None, format=None): form = CarForm(request.DATA, user=request.user) if form.is_valid(): obj = form.save(commit=False) obj.created_by = request.user # p is the flag for publish if request.DATA.get("status") == "P": obj.status = obj.STATUS_PENDING obj.save() if obj.status == obj.STATUS_PENDING: # when post is published obj.primary_image = Image.objects.get(id=request.DATA["primary_image"]) obj.save() self._update_images(request, obj) self._send_notification(obj) messages.info(request, _("Your car has been successfully submitted and is currently pending approval.")) return Response({"redirect": reverse("vehicle-index")}) response = {"id": obj.id} return Response(response, mimetype="application/json", status=status.HTTP_201_CREATED) else: return Response({"errors": form.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
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 addCar(request, eventid): event = EventModel.getEvent(eventid) user = request.user if memberCheck(request.user, event) == False: return render(request, 'invite/notMember.html') if request.method == 'GET': carform = CarForm() context = {'menu' : getMenuInfo(request), 'title' : "Add Car", 'carform' : carform, 'event' : event, 'user' : request.user} return render(request, 'ride_share/add_car_form.html', context) if request.method == 'POST': #instance = Car.objects.filter(event = event, user = user) #if instance.count() == 0: # carform = CarForm(request.POST) #else: # carform = CarForm(request.POST, instance = instance[0]) carform=CarForm(request.POST) if carform.is_valid(): #form = carform.save(commit=False) #form.user = request.user #form.event = event #form.save() person=Person(personid=user,status='DR',event=event) person.save() car=Car(event=event, driver=person, seats=request.POST['seats'], open_seats=request.POST['seats']) car.event=event car.driver =person car.seats = request.POST['seats'] car.open_seats=car.seats car.save() return HttpResponseRedirect(reverse('events:tools:rideshare:carDetails', kwargs={'eventid':eventid,'carid':car.carid}))
def new(request): form = None try: if request.method == 'POST': if request.POST.get('id'): #edit operations pass form = CarForm(request.POST, user=request.user) if form.is_valid(): obj = form.save(commit=False) obj.created_by = request.user obj.save() # save record and redirect return redirect('/') else: # edit mode for draft form = CarForm(instance=Car.objects.get(status=Car.STATUS_DRAFT, created_by=request.user), user=request.user) model_lookup_form = ModelLookUpForm() model_lookup_i18n_form = ModelLookUpI18nForm() return render_to_response( 'vehicle/new.html', { 'model_lookup_form': model_lookup_form, 'model_lookup_i18n_form': model_lookup_i18n_form, 'form': form, }, RequestContext(request) ) except (Car.DoesNotExist, Car.MultipleObjectsReturned) as e: # new form (no drafts found) model_lookup_form = ModelLookUpForm() model_lookup_i18n_form = ModelLookUpI18nForm() form = CarForm(user=request.user) return render_to_response( 'vehicle/new.html', { 'model_lookup_form': model_lookup_form, 'model_lookup_i18n_form': model_lookup_i18n_form, 'form': form, }, RequestContext(request) ) except Exception as e: logging.error(str(e)) raise Http404 finally: pass
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 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 car(request, car_id=None, template="car/car.html", context={}): car_object = None car_form = None if car_id: car_object = get_object_or_404(Car, pk=car_id) car_form = CarForm(request.POST or None, instance=car_object) if request.method == 'POST': car_object = car_form.save() return redirect('/car') context['car_id'] = car_id context['car_object'] = car_object context['car_form'] = car_form return render(request, template, context)
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 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)