Ejemplo n.º 1
0
def index():
    form = search_vehicles_form_builder(request.form)
    type_fields = [getattr(form, vt) for vt in Vehicle.VEHICLE_TYPES]

    if form.validate_on_submit():
        types_search = [f.label.text for f in type_fields if f.data]
        manufacturer_search = form.manufacturer.data
        model_search = form.model.data
        color_search = form.color.data
        min_engine_search = form.min_engine.data
        max_engine_search = form.max_engine.data
        min_mileage_search = form.min_mileage.data
        max_mileage_search = form.max_mileage.data

        filtered_vehicles = Vehicle.perform_search(
            types_search, manufacturer_search, model_search, color_search,
            min_engine_search, max_engine_search, min_mileage_search,
            max_mileage_search)
        return render_template('index.html',
                               form=form,
                               vehicles=filtered_vehicles,
                               types=type_fields)

    all_vehicles = Vehicle.query.all()
    return render_template('index.html',
                           form=form,
                           vehicles=all_vehicles,
                           types=type_fields)
    def test_search_type_1(self):
        v1 = Vehicle(u'Car', u'Fiat', u'Uno', u'Red', 1000, 0)
        v2 = Vehicle(u'Car', u'Volkswagen', u'Fox', u'Silver', 1600, 10000)

        self.db.session.add(v1)
        self.db.session.add(v2)
        self.db.session.commit()

        vv = Vehicle.perform_search([u'Motorcycle'], '', '', '', 1, 10000, 0, 200000)
        self.assertEqual(len(vv), 0)
    def test_search_type_2(self):
        v1 = Vehicle(u'Car', u'Fiat', u'Uno', u'Red', 1000, 0)
        v2 = Vehicle(u'Motorcycle', u'Honda', u'CG 125', u'Silver', 125, 10000)

        self.db.session.add(v1)
        self.db.session.add(v2)
        self.db.session.commit()

        vv = Vehicle.perform_search([u'Motorcycle'], '', '', '', 1, 10000, 0, 200000)
        self.assertEqual(len(vv), 1)
        self.assertEqual(vv[0].id, v2.id)
    def test_search_manufacturer(self):
        v1 = Vehicle(u'Car', u'Fiat', u'Uno', u'Red', 1000, 0)
        v2 = Vehicle(u'Motorcycle', u'Honda', u'CG 125', u'Silver', 125, 10000)
        v3 = Vehicle(u'Car', u'Honda', u'Civic', u'Silver', 1600, 10000)

        self.db.session.add(v1)
        self.db.session.add(v2)
        self.db.session.add(v3)

        self.db.session.commit()

        vv = Vehicle.perform_search([u'Motorcycle', u'Car'], u'Honda', '', '', 1, 10000, 0, 200000)
        self.assertEqual(set(vv), {v2, v3})