Beispiel #1
0
def editProduct(product_id):
    currentUser = getCurrentUser()
    product = session.query(Product).filter(Product.id == product_id).one()
    # Only allow product edit if the seller is the current user
    if matchesLoginUser(product.seller):
        form = ProductForm(request.form, product)
        categories = session.query(Category).all()
        form.category_id.choices = [(category.id, category.name)
                                    for category in categories]
        if form.validate_on_submit():
            form.populate_obj(product)
            if 'image' in request.files and \
              request.files['image'].filename != '':
                image = request.files['image']
                product.imageName = secure_filename(image.filename)
                appDir = flask.config['APP_DIR']
                path = os.path.join(
                    flask.config['APP_DIR'],
                    'server/static/product_images/%s' % product.imageName)
                image.save(path)
            try:
                session.add(product)
                session.commit()
            except:
                session.rollback()
                return 'Failed to edit record. Please make sure to pick a unique name.'
            return redirect(url_for('userProfile', user_id=product.seller.id))
        else:
            return render_template('edit-product.html',
                                   currentUser=currentUser,
                                   form=form,
                                   product=product)
    else:
        return redirect(url_for('home'))
Beispiel #2
0
def editProduct(product_id):
	currentUser = getCurrentUser()
	product = session.query(Product).filter(Product.id == product_id).one()
	# Only allow product edit if the seller is the current user
	if matchesLoginUser(product.seller):
		form = ProductForm(request.form, product)
		categories = session.query(Category).all()
		form.category_id.choices = [(category.id, category.name) for category in categories]
		if form.validate_on_submit():
			form.populate_obj(product)
			if 'image' in request.files and \
					request.files['image'].filename != '':
				image = request.files['image']
				product.imageName = secure_filename(image.filename)
				appDir = flask.config['APP_DIR']
				path = os.path.join(flask.config['APP_DIR'], 'server/static/product_images/%s' % product.imageName)
				image.save(path)
			try:
				session.add(product)
				session.commit()
			except:
				session.rollback()
				return 'Failed to edit record. Please make sure to pick a unique name.'
			return redirect(url_for('userProfile', 
				user_id = product.seller.id))
		else:
			return render_template('edit-product.html', 
				currentUser = currentUser, form = form, product = product)
	else:
		return redirect(url_for('home'))
Beispiel #3
0
def newProduct():
    user = getCurrentUser()
    # Only logged in users can add a product
    if user is not None:
        categories = session.query(Category).order_by(Category.name).all()
        form = ProductForm(request.form)
        form.category_id.choices = \
         [(category.id, category.name) for category in categories]
        if form.validate_on_submit():
            product = Product()
            form.populate_obj(product)
            if 'image' in request.files and \
              request.files['image'].filename != '':
                image = request.files['image']
                product.imageName = secure_filename(image.filename)
                appDir = flask.config['APP_DIR']
                path = os.path.join(
                    flask.config['APP_DIR'],
                    'server/static/product_images/%s' % product.imageName)
                image.save(path)
            product.seller = user
            try:
                session.add(product)
                session.commit()
            except:
                session.rollback()
                return 'Failed to add record. Please make sure to pick a unique name.'
            return redirect(url_for('userProfile', user_id=user.id))
        else:
            return render_template('new-product.html',
                                   currentUser=user,
                                   form=form)
    else:
        return redirect(url_for('home'))
Beispiel #4
0
def newProduct():
	user = getCurrentUser()
	# Only logged in users can add a product
	if user is not None:
		categories = session.query(Category).order_by(Category.name).all()
		form = ProductForm(request.form)
		form.category_id.choices = \
			[(category.id, category.name) for category in categories]
		if form.validate_on_submit():
			product = Product()
			form.populate_obj(product)
			if 'image' in request.files and \
					request.files['image'].filename != '':
				image = request.files['image']
				product.imageName = secure_filename(image.filename)
				appDir = flask.config['APP_DIR']
				path = os.path.join(flask.config['APP_DIR'], 'server/static/product_images/%s' % product.imageName)
				image.save(path)
			product.seller = user
			try:
				session.add(product)
				session.commit()
			except:
				session.rollback()
				return 'Failed to add record. Please make sure to pick a unique name.'
			return redirect(url_for('userProfile', user_id = user.id))
		else:
			return render_template('new-product.html', currentUser = user, 
				form = form)
	else:
		return redirect(url_for('home'))