示例#1
0
    def post(self, id):
        if not session.get('logged_in'):
            return redirect(url_for('admin.login'))
        model = request.form['model']
        year = request.form['year']
        manufacturer = request.form['manufacturer']
        photo = request.files['photo']

        if(not model or not manufacturer or not year or (not id and not photo)):
            fields_required = []

            if not model:
                fields_required.append("Modelo")

            if not manufacturer:
                fields_required.append("Fabricante")

            if not year:
                fields_required.append("Ano")

            if not photo and not id:
                fields_required.append("Foto")

            if(len(fields_required) > 1):
                alert = u"Os campos " + ", ".join(fields_required)+ u" são obrigatórios."
            else:
                alert = u"O campo " + fields_required[0] + u" é obrigatório."

            context = {
                "car": Car(model=model, year=year, photo=photo, manufacturer=manufacturer),
                "create": id is None,
                "tab_active" : "admin",
                "alert": alert,
                "status": -1
                }
            return render_template('admin/detail.html',**context)

        if photo:
            photo_name = self.upload_file(photo)
        if id:
            car = Car.objects.get_or_404(id=id)
            car.model = model
            car.year = year
            car.manufacturer = manufacturer
            if photo:
                #remove old photo
                try:
                    os.remove(os.path.join(app.config['UPLOAD_FOLDER'], car.photo))
                except:
                    pass
                #update photo
                car.photo = photo_name
        else:
            car = Car(model=model, year=year, photo=photo_name, manufacturer=manufacturer)
        car.save()
        return redirect(url_for('admin.list'))
示例#2
0
    def post(self):
        model = request.form['model']
        manufacturer = request.form['manufacturer']
        year = request.form['year']

        query = dict()
        if model:
            query['model'] = model
        if manufacturer:
            query['manufacturer'] = manufacturer
        if year:
            query['year'] = year

        cars = Car.objects(**query)

        return render_template('cars/search.html', cars=cars, tab_active= "car")