def edit_post(slug): post = Post.query.filter(Post.slug == slug).first() if post.author.username == current_user.username: if request.method == 'POST': form = PostForm(formdata=request.form, obj=post) form.populate_obj(post) post.updated = datetime.utcnow() db.session.commit() return redirect(url_for('posts.news_detail.html, post=post')) form = PostForm(obj=post) return render_template('posts/edit_post.html', edit_post_form=form, post=post) return redirect(url_for('posts.news'))
def dispatch_request(self, post_id: int): username = session['active_user']['username'] post = db_posts(id=post_id) if username != post.author.username: abort(403) form = PostForm() if request.method == 'POST': if form.validate_on_submit(): try: post.title = form.title.data post.content = form.content.data post.tag = form.tag.data db.session.commit() flash('Your post is successfully updated.', 'success') return redirect(url_for(request.endpoint, post_id=post_id)) except Exception as e: flash('Invalid update', 'error') return redirect(url_for(request.endpoint, post_id=post_id)) form.title.data = post.title form.content.data = post.content form.tag.data = post.tag return render_template('editpost.html', form=form)
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user and not current_user.is_admin: logger.debug( "User %r tried unsuccessfully to edit post %d", current_user.username, post.id, ) abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() logger.debug("User %r updated post %d", current_user.username, post.id) flash("Your post has been updated!", "success") return redirect(url_for("posts.get_post", post_id=post.id)) if request.method == "GET": form.title.data = post.title form.content.data = post.content return render_template( "create_post.html", title="Update Post", form=form, legend="Update Post" )
def new_post(): for key, value in request.form.items(): print("key: {0}, value: {1}".format(key, value)) form = PostForm() if form.validate_on_submit(): print(form.data) post = Post(title=form.title.data, content=form.content.data, user=current_user, category=form.category.data) db.session.add(post) db.session.commit() for tag in form.tags: print(tag) tag_entry = Tag(tag=tag.data.strip(), post=post, user=current_user) db.session.add(tag_entry) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def list_posts(): """ shows all posts and lets signed-in users post a picture, text or both """ form_post = PostForm() all_posts = PostsModel.query.all() authors = UserModel if request.method == 'POST' and check_auth(): if form_post.text.data or form_post.media.data: text = form_post.text.data media = form_post.media.data time = datetime.now() if media: media = save_file( current_user.username, media, 'post_uploads' ) # saves file to directory, returns filename PostsModel.add(time, text, media, current_user.id) return redirect('/posts') else: return render_template('posts.html', pages=generate_pages(), form=form_post, all_posts=all_posts, authors=authors)
def managepost(): category_list = [(g.id, g.title) for g in Category.query.all()] form = PostForm() form.category.choices = category_list if form.validate_on_submit(): language = guess_language(form.post.data) if language == 'UNKNOWN' or len(language) > 5: language = '' post = Post(title=form.title.data, body_html=form.post.data, author=current_user, language=language, category_id=form.category.data) db.session.add(post) db.session.commit() flash(_('Your post is now live!')) return redirect(url_for('posts.managepost')) page = request.args.get('page', 1, type=int) posts = current_user.posts.order_by(Post.timestamp.desc()).paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('posts.managepost', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('posts.managepost', page=posts.prev_num) \ if posts.has_prev else None return render_template('manage_post.html', title=_('Posts'), form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
def dispatch_request(self): form = PostForm() username = session['active_user']['username'] author = db_user(username=username) posts = db_posts(author=author) if request.method == 'POST': if form.validate_on_submit(): title = form.title.data content = form.content.data tag = form.tag.data try: new_post = Post(author=author, title=title, content=content, tag=tag) db.session.add(new_post) db.session.commit() flash('Your post is successfully submitted', 'success') return redirect(url_for(request.endpoint)) except Exception as e: flash('Invalid inputs.', 'error') return redirect(url_for(request.endpoint)) return render_template('admin.html', username=username, form=form, posts=posts)
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('posts.post', post_id = post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('new_post.html', title = 'Update Post', legend = 'Update Post', form = form )
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): if form.picture.data: print('Hello World') picture_file = save_picture(form.picture.data) post.image_file = picture_file post.title = form.title.data post.content = form.content.data post.category = form.category.data post.summary = form.summary.data db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('posts.post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content form.category.data = post.category form.summary.data = post.summary return render_template('create_post.html', title='Update Post', form=form, legend='Update Post')
def new_post(): form = PostForm() if form.validate_on_submit(): flash('Your post has been created!', 'success') hashtag = '' hashtags = form.content.data print(hashtags) newstr = hashtags.split() for char in newstr: if char.startswith("#"): hashtag = char.strip("#") print(hashtag) # if form.picture.data: # picture_file = save_picture(form.picture.data) picture_file = save_picture(form.picture.data) # video_file = save_video(form.video.data ) image = url_for('static', filename='images/' + picture_file) # video = url_for('static',filename='videos/'+video_file) post = Pitch(title=form.title.data, content=form.content.data, author=current_user, hashtags=hashtag, image=image) db.session.add(post) db.session.commit() return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def new_post(): form = PostForm() if form.validate_on_submit(): pic = None if form.image.data: picture_file = save_picture(form.image.data) final_pic = picture_file pic = final_pic post = Post(title=form.title.data, content=form.content.data, author=current_user, category=form.category.data, image=pic) db.session.add(post) db.session.commit() flash('Your post has been published!', 'success') return redirect(url_for('main.home')) myposts = Post.query.order_by(Post.posted_date.desc()) return render_template('new-post.html', title='New Post', form=form, legend='New Post', myposts=myposts, quotes=quotes)
def update_post(post_id): post = Post.query.get_or_404(post_id) final_pic = " " if post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): if form.image.data: picture_file = save_picture(form.image.data) final_pic = picture_file post.title = form.title.data post.content = form.content.data post.category = form.category.data post.image = final_pic db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('posts.post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content form.category.data = post.category myposts = Post.query.order_by(Post.posted_date.desc()) return render_template('new-post.html', title='Update Post', form=form, legend='Update Post', quotes=quotes, myposts=myposts)
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() tagForm = TagForm() tags = Tag.query.all() form.tags.choices = [(tag.id, tag.value) for tag in tags] if form.validate_on_submit(): post.title = form.title.data post.content = repr(form.content.data) post.tags = Tag.query.filter(Tag.id.in_(form.tags.data)).all() db.session.commit() flash(f'Your Post has been updated!!', category='success') return redirect(url_for('posts.post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content selectedTags = post.tags return render_template('create_post.html', title='Update Post', legend='Update Post', form=form, tags=tags, tagForm=tagForm, selectedTags=selectedTags) return render_template('create_post.html', title='Update Post', legend='Update Post', form=form, tags=tags, tagForm=tagForm)
def post(): form_post = PostForm() if form_post.validate_on_submit(): if form_post.picture_for_posts.data: file = request.files['picture_for_posts'] pic = saving_pictures_post(file) form_post.picture_for_posts = pic post = Post(title=form_post.title.data, content=form_post.content.data, image=form_post.picture_for_posts, location=form_post.location.data, space_size=form_post.space_size.data, author=current_user) db.session.add(post) db.session.commit() flash('you have successfully posted your space!!', 'success') return redirect(url_for('main.home_page')) image = url_for('static', filename='post_pictures/' + str(form_post.picture_for_posts)) return render_template('post.html', title='Post', content='content', image=image, form=form_post)
def update_post(post_id): post = Post.query.get_or_404(post_id) form = PostForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture(form.picture.data) post.image_file = picture_file post.city = form.city.data post.category = form.category.data post.title = form.title.data post.story = form.story.data post.summary = form.summary.data db.session.commit() flash(' post has been updated!', 'success') return redirect(url_for('posts.postn', post_id=post.id)) elif request.method == 'GET': form.city.data = post.city form.category.data = post.category form.title.data = post.title form.story.data = post.story form.summary.data = post.summary return render_template('posts/create_post.html', title='Update Post', form=form, legend='Update Post')
def update_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() # Create an instance of the PostForm() if form.validate_on_submit(): # If the input is valid title_check = predict([form.title.data]) content_check = predict([form.content.data]) if (title_check[0] == 1 or content_check[0] == 1) and (current_user.profanity == False): post.title = censor.censor( form.title.data) # The form title overwrites the post title post.content = censor.censor(form.content.data) current_user.reputation -= 1 # reputation score decremented by 1 current_user.profanity = True db.session.commit() flash( 'Warning! Profanity detected! Since this is your first offense, Your reputation score has been reduced by 1. Any subsequent offense will reduce your reputation score by 5!', 'warning') return redirect(url_for('posts.post', post_id=post.id)) if (title_check[0] == 1 or content_check[0] == 1) and (current_user.profanity == True): post.title = censor.censor( form.title.data) # The form title overwrites the post title post.content = censor.censor( form.content.data ) # The form content overwrites the post content current_user.reputation -= 5 # reputation score decremented by 5 db.session.commit() flash( 'Warning! Profanity detected! Your reputation score has been reduced by 5. You were warned!', 'danger') return redirect(url_for('posts.post', post_id=post.id)) # When there is no profanity: post.title = form.title.data # The form title overwrites the post title post.content = form.content.data # The form content overwrites the post content db.session.commit( ) # Commit changes to db! # We do not need to add anything into the db, these objects are already in the db and being overwritten! flash( 'Your post has been successfully updated!', 'success' ) # Display success message. For anyone wondering 'success' is a bootstrap class it gives a green-ish hue. return redirect(url_for( 'posts.post', post_id=post.id)) # Redirect to the post/id page if request.method == 'GET': form.title.data = post.title # This ensures that the fields are populated with the previous text! But only if it's a 'GET' request. form.content.data = post.content # This ensures that the fields are populated with the previous text! But only if it's a 'GET' request. return render_template( 'create_post.html', title='Update Post', form=form, legend='Update Post' ) # Redirect to Create_Post.html, but with some pre-filled text, and a newer legend!
def new_post(): form = PostForm() # Create instance of the PostForm() if form.validate_on_submit( ): # If user input is valid (i.e) no null characters, etc title_check = predict([form.title.data]) content_check = predict([form.content.data]) if (title_check[0] == 1 or content_check[0] == 1) and (current_user.profanity == False): title = censor.censor( form.title.data) # Feature # 11: Profanity Filter ... content = censor.censor(form.content.data) post = Post( title=title, content=content, author=current_user ) # Create instance of Post() and pass in user input given to the form current_user.reputation -= 1 current_user.profanity = True db.session.commit() flash( 'Warning! Profanity detected! Since this is your first offense, Your reputation score has been reduced by 1. Any subsequent offense will reduce your reputation score by 5!', 'warning') return redirect(url_for('main.home')) # Redirect to Home page if (title_check[0] == 1 or content_check[0] == 1) and (current_user.profanity == True): title = censor.censor( form.title.data) # Feature # 11: Profanity Filter ... content = censor.censor(form.content.data) post = Post( title=title, content=content, author=current_user ) # Create instance of Post() and pass in user input given to the form current_user.reputation -= 5 db.session.commit() flash( 'Warning! Profanity detected! Your reputation score has been reduced by 5. You were warned!', 'danger') return redirect(url_for('main.home')) # Redirect to Home page title = censor.censor( form.title.data) # Feature # 11: Profanity Filter ... content = censor.censor(form.content.data) post = Post( title=title, content=content, author=current_user ) # Create instance of Post() and pass in user input given to the form db.session.add(post) # Mark this 'post' to be added to the database db.session.commit() # Commit changes to db flash( 'Your post has been created!', 'success' ) # Display success message! For anyone wondering 'success' is just a bootstrap class, it gives a green-ish hue. return redirect(url_for('main.home')) # Redirect to Home page return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def delete_post(post_id): post = Post.query.get_or_404(post_id) if post.author != current_user: abort(403) form = PostForm() db.session.delete(post) db.session.commit() flash('Your post has been deleted!', 'success') return redirect(url_for('main.home'))
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash(f"New post is live!", category="success") return redirect(url_for("main.home")) return render_template("create_post.html", title="New Post", form=form, legend="New Post")
def create_post(): form = PostForm() if form.validate_on_submit() and request.method == 'POST': post = Post(title=form.title.data, body=form.body.data, author=current_user) db.session.add(post) db.session.commit() post.set_tags(tags=form.tags.data) return redirect(url_for('posts.posts')) return render_template('posts/create_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Post created successfully!', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title='New Post', form=form, legend='New Post')
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title = form.title.data, content = form.content.data, user = current_user) db.session.add(post) db.session.commit() flash('Your story has been posted, thank you', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', title = 'New Post', form = form, legend = 'New Story')
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('post has been created') return redirect(url_for('users.user', username=current_user.username)) return render_template('create_post.html', form=form, title='New Post')
def new_post(user): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() return redirect(url_for("main.logged_in", user=current_user.username)) return render_template('create_post.html', user=current_user.username, form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, body=form.post.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post is now live!') return redirect(url_for('main.index')) return render_template("new_post.html", title='Home Page', form=form)
def create_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, pre_body=form.pre_body.data, body=form.body.data) post.author = current_user db.session.add(post) db.session.commit() return redirect(url_for('posts.news')) return render_template('posts/create_post.html', create_post_form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('main.home')) return render_template("post.html", title="New post", form=form)
def editpost(): if g.auth is not None and g.auth.role is not USER.ADMIN: return redirect(url_for('not_found')) if request.method == 'POST': try: form = request.args title = form.get('title') pid = form.get('drafts') dopost = form.get('dopost') post = Post.query.filter_by(title=title).first() if post is None: post = Post.query.filter_by(id=pid).first() if post is None: post = Post() post.user_id = g.user.id post.title = title post.textbody = form.get('textbody').encode('u8') post.htmlbody = form.get('htmlbody').encode('u8') if dopost == True or dopost == "true": post.draft = False post.posted = datetime.datetime.now() db.session.add(post) else: post.title = title post.textbody = form.get('textbody').encode('u8') post.htmlbody = form.get('htmlbody').encode('u8') if dopost == True or dopost == "true": post.draft = False post.posted = datetime.datetime.now() db.session.commit() return make_response("Post Saved") except: print >> sys.stderr, sys.exc_info() return make_response("Exceptional Error") pid = request.args.get('pid') form = PostForm() form.drafts.choices = [ (p.id, p.title) for p in Post.query.filter_by(draft=True).order_by(Post.title).all() ] form.drafts.choices.insert(0, (-1, "New Post")) return render_template("posts/editpost.html", form=form, user=g.user, admin=g.admin, pid=pid)
def new_blog(): if current_user.is_authenticated: return render_template('blog.html',title='New Blog') form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user, post=post) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('main.home')) return render_template('layout.html', title='New Post', form=form,legend='New Post')
def index(): form = PostForm() if form.validate_on_submit(): post = Post(author=current_user, body=form.body.data) post.commit() # Reload the page to avoid resubmit. return redirect(url_for('users.index')) posts = Post.query\ .filter_by(author=current_user, active=True)\ .order_by(Post.created.desc())\ .all() return render_template('users/index.jinja', form=form, posts=posts)