def make_post(chat_name): form = PostForm(chat_name=chat_name) if form.validate_on_submit(): chat_name = form.chat_name.data new_post = Post(title=form.title.data, body=form.body.data, author=current_user) new_post.chat = Chat.query.filter_by(name=chat_name).first() if 'image' in request.files and request.files['image']: filename = images.save(form.image.data) url = images.url(filename) image = Image(filename=filename, url=url) new_post.attachment = image db.session.add(new_post) db.session.commit() flash('Your post is now live!') return redirect(url_for('main.show_chat', name=chat_name)) return render_template('make_post.html', title="New Post", form=form)
def new_post(): form = NewPostForm() if form.validate_on_submit(): userid = current_user.get_id() post = Post(body=form.postbody.data, timestamp=datetime.utcnow(), user_id=userid) if form.attachment.data: f = form.attachment.data name_append = ''.join([str(randrange(9)) for n in range(9)]) filename = str( current_user.id) + '_' + name_append + '_' + secure_filename( f.filename) f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) post.attachment = filename flash(f'Uploaded {filename}.') db.session.add(post) db.session.commit() flash('New post successful.') return redirect(url_for('index')) return render_template('new_post.html', title='New Post', form=form)