def slug(self): return slugify(self.title)
def slug(self): return slugify(self.name)
def product_add(): categories = Category.query.all() product = Product() form = ProductForm(obj=product) # Set the categories to the form form.category_id.choices = [(c.id, c.name) for c in Category.query.order_by('name')] if request.method == "POST": if form.validate(): # populate Product obj with wtform data form.populate_obj(product) product.user_id = current_user.get_id() product.slug = slugify(product.title) f = form.picture.data # renames the file to prevent duplicated names filename = datetime.now().strftime( '%Y%m%d%H%M%S') + secure_filename(f.filename) filepath = os.path.join( app.config["UPLOAD_FOLDER"], 'uploads', filename ) f.save(filepath) # @TODO Generate an image thumbnail product.picture = filename db_session.add(product) try: db_session.commit() flash('Nice! You just added your product to sell :D', 'success') redirect("/") except exc.SQLAlchemyError: db_session.rollback() flash('Error! There was an error trying to save your product', 'danger') else: for field, errors in form.errors.items(): for error in errors: flash(u"Error in the %s field - %s" % ( getattr(form, field).label.text, error )) return render_template( 'products/add.html', categories=categories, form=form )