def edit(listing_id): if request.method == 'GET': token = request.args.get('token') listing = Listing.query.filter(Listing.id==listing_id).first() category = Category.query.filter(Category.id==listing.cat_id).first() if token != listing.token: flash('Please use the link sent to your email') return render_template('home.html') return render_template('edit_listing.html', listing=listing, cat_name=cat_name) elif request.method == 'POST': listing = Listing.query.filter(Listing.id==listing_id).first() title = request.form.get('title') body = request.form.get('body') email = request.form.get('email') price = request.form.get('price') img_file = request.form.get('file') filename = 'None' if img_file: if img_file.filename != listing.img_filename: filename = secure_filename(img_file.filename) img_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) image = Image.create(filename=filename, listing_id=listing.id) if filename: key = bucket.new_key(filename) key.set_contents_from_string(img_file.read()) key.set_canned_acl('public-read') image = Image.create(filename=filename, listing_id=listing.id) listing.update(title=title, body=body, email=email, price=price, filename=filename) flash('update successful') return redirect('/listings')
def create(): if request.method == 'GET': return render_template('create_listing.html', categories=Category.query.all()) elif request.method == 'POST': cat_id = request.form.get('cat_id') title = request.form.get('title') body = request.form.get('body') email = request.form.get('email') price = request.form.get('price') filename = 'None' token = bcrypt.gensalt() if title and body and email and price: listing = Listing.create(cat_id=cat_id, title=title, body=body, email=email, price=price, token=token) for img_file in request.files.getlist('file[]'): if img_file: filename = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(26)) + ".jpeg" if filename: #img_file.save(os.path.join(app.config['uploads'], filename)) key = bucket.new_key(filename) key.set_contents_from_string(img_file.read()) key.set_canned_acl('public-read') image = Image.create(filename=filename, listing_id=listing.id) link = 'http://*****:*****@gmail.com', recipients=[email]) msg.body = 'Click here to edit your listing: ' + link mail.send(msg) flash('Listing created. Please check your email for a link to edit it.') return redirect('/') else: flash('Oops, something went wrong. Please create a valid listing.') return render_template('create_listing.html')
def create(): if request.method == 'GET': # return render_template('create.html', categories=Category.query.order_by(Category.name).all()) return render_template('create.html', categories=Category.query.all()) elif request.method == 'POST': title = request.form.get('title') body = request.form.get('body') category_id = request.form.get('category_id') email = request.form.get('email') price = request.form.get('price') token = bcrypt.gensalt() if title and body and email and price: post = Post.create(title=title, body=body, category_id=category_id, email=email, price=price, token=token) print request.files.getlist('file[]') for img_file in request.files.getlist('file[]'): if img_file: filename = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(26)) + ".jpeg" if filename: key = bucket.new_key(filename) key.set_contents_from_string(img_file.read()) key.set_canned_acl('public-read') image = Image.create(filename=filename, post_id=post.id) link = 'http://{2}/edit/{1}?token={0}'.format(post.token, post.id, app.config['URL']) msg = Message("Tyler'sList Edit Post - DO NOT DELETE", sender=app.config['MAIL_DEFAULT_SENDER'], recipients=[post.email]) msg.html = "Thank you for posting with Tyler'sList, we hope your experience with our platform was enjoyable and painless.<br>" + " Use this link to edit your post: " + link mail.send(msg) flash('Post was successfully created, please check your email for an editing link.') return redirect('/') else: flash('please fill out the required fields') return render_template('create.html', title=title, body=body, email=email, price=price)
def create(): if request.method == 'GET': return render_template('create_listing.html', categories=Category.query.all()) elif request.method == 'POST': cat_id = request.form.get('cat_id') title = request.form.get('title') body = request.form.get('body') email = request.form.get('email') price = request.form.get('price') filename = 'None' token = bcrypt.gensalt() if title and body and email and price: listing = Listing.create(cat_id=cat_id, title=title, body=body, email=email, price=price, token=token) for img_file in request.files.getlist('file[]'): if img_file: filename = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(26)) + ".jpeg" if filename: #img_file.save(os.path.join(app.config['uploads'], filename)) key = bucket.new_key(filename) key.set_contents_from_string(img_file.read()) key.set_canned_acl('public-read') image = Image.create(filename=filename, listing_id=listing.id) link = 'http://*****:*****@gmail.com', recipients=[email]) msg.body = 'Click here to edit your listing: ' + link mail.send(msg) flash( 'Listing created. Please check your email for a link to edit it.' ) return redirect('/') else: flash('Oops, something went wrong. Please create a valid listing.') return render_template('create_listing.html')
def edit(listing_id): if request.method == 'GET': token = request.args.get('token') listing = Listing.query.filter(Listing.id == listing_id).first() category = Category.query.filter(Category.id == listing.cat_id).first() if token != listing.token: flash('Please use the link sent to your email') return render_template('home.html') return render_template('edit_listing.html', listing=listing, cat_name=cat_name) elif request.method == 'POST': listing = Listing.query.filter(Listing.id == listing_id).first() title = request.form.get('title') body = request.form.get('body') email = request.form.get('email') price = request.form.get('price') img_file = request.form.get('file') filename = 'None' if img_file: if img_file.filename != listing.img_filename: filename = secure_filename(img_file.filename) img_file.save( os.path.join(app.config['UPLOAD_FOLDER'], filename)) image = Image.create(filename=filename, listing_id=listing.id) if filename: key = bucket.new_key(filename) key.set_contents_from_string(img_file.read()) key.set_canned_acl('public-read') image = Image.create(filename=filename, listing_id=listing.id) listing.update(title=title, body=body, email=email, price=price, filename=filename) flash('update successful') return redirect('/listings')