def renderHomePage(): page_to_access = request.args.get("page", 1, type=int) queried_all_posts = Post.query.order_by(Post.date_posted.desc()).paginate( page=page_to_access, per_page=5) return render_template("index.html", posts=queried_all_posts, **getCurrentUserJson(current_user))
def renderUserPosts(selected_username): if selected_username == '-': flash("Please log in to view your posts.", "info") return redirect(url_for("users.renderLogin")) page_to_access = request.args.get("page", 1, type=int) selected_user = User.query.filter_by( username=selected_username).first_or_404() queried_user_posts = Post.query.filter_by(author=selected_user).order_by( Post.date_posted.desc()).paginate(page=page_to_access, per_page=5) return render_template("user_posts.html", posts=queried_user_posts, user=selected_user, **getCurrentUserJson(current_user))
def renderResetRequest(): if current_user.is_authenticated: return redirect(url_for("main.renderHomePage")) form = RequestResetForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() sendResetEmail(user) flash( "An email has been sent with instructions to reset your password. If you don't see any mail in your inbox, please check your spams folder.", "info") return redirect(url_for("users.renderLogin")) return render_template("reset_request.html", title="Reset Password", form=form, **getCurrentUserJson(current_user))
def makeNewPost(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user, is_anonymous=current_user.is_anonymous) db.session.add(post) db.session.commit() flash("Your post has been created.", "success") return redirect(url_for("main.renderHomePage")) return render_template("create_post.html", title="New Post", form=form, legend="New post", **getCurrentUserJson(current_user))
def renderAccount(): form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: picture_file = savePicture(form.picture.data) current_user.image_file = picture_file current_user.username = form.username.data current_user.email = form.email.data db.session.commit() flash("Your account has been updated.", "success") return redirect(url_for("users.renderAccount")) elif request.method == "GET": form.username.data = current_user.username form.email.data = current_user.email return render_template("account.html", title="Account", form=form, **getCurrentUserJson(current_user))
def renderResetToken(token): if current_user.is_authenticated: return redirect(url_for("main.renderHomePage")) user = User.verify_reset_token(token) if user is None: flash("That is an invalid or expired token", "warning") return redirect(url_for("users.renderResetRequest")) form = ResetPasswordForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode("utf-8") user.password = hashed_password db.session.commit() flash(f"Your password has been updated.", "success") return redirect(url_for("users.renderLogin")) return render_template("reset_token.html", title="Reset Password", form=form, **getCurrentUserJson(current_user))
def renderRegister(): if current_user.is_authenticated: return redirect(url_for("main.renderHomePage")) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode("utf-8") user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash(f"Account created for {form.username.data}!", "success") return redirect(url_for("users.renderLogin")) return render_template("register.html", title="Register", form=form, **getCurrentUserJson(current_user))
def renderPost(post_id): queried_post = Post.query.get_or_404(post_id) queried_comments = Comment.query.filter_by(post_id=post_id).order_by( Comment.date_posted.asc()) queried_anonymous_table = Anonymous_table.query.filter_by(post_id=post_id) queried_anonymous_table = queried_anonymous_table.all() anonymous_number_table = [] for current_entry in queried_anonymous_table: anonymous_number_table.append({ "user_id": current_entry.user_id, "number": current_entry.number }) form = CommentForm() if form.validate_on_submit(): comment = Comment(post_id=queried_post.id, content=form.content.data, commenter=current_user, is_anonymous=current_user.is_anonymous) db.session.add(comment) if current_user != queried_post.author and not Anonymous_table.query.filter_by( post_id=post_id, user_id=current_user.id).first(): new_commenter_anonymous_number = Anonymous_table.query.filter_by( post_id=post_id).count() anonymous_number = Anonymous_table( post_id=post_id, user_id=current_user.id, number=new_commenter_anonymous_number) db.session.add(anonymous_number) db.session.commit() flash("Your comment has been created.", "success") return redirect(url_for("posts.renderPost", post_id=post_id)) return render_template("post.html", title=queried_post.title, form=form, post=queried_post, comments=queried_comments.all(), anonymous_number_table=anonymous_number_table, **getCurrentUserJson(current_user))
def updateComment(post_id, comment_id): queried_comment = Comment.query.get_or_404(comment_id) if queried_comment.commenter != current_user: abort(403) form = CommentForm() if form.validate_on_submit(): queried_comment.content = form.content.data db.session.commit() flash("Your comment has been updated.", "success") return redirect(url_for("posts.renderPost", post_id=post_id)) elif request.method == "GET": form.content.data = queried_comment.content form.content.data = queried_comment.content return render_template("update_comment.html", title="Update Comment", form=form, legend="Update comment", **getCurrentUserJson(current_user))
def renderLogin(): if current_user.is_authenticated: return redirect(url_for("main.renderHomePage")) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user and bcrypt.check_password_hash(user.password, form.password.data): login_user(user, remember=form.remember.data) next_page = request.args.get("next") return redirect(next_page) if next_page else redirect( url_for("main.renderHomePage")) else: flash("Login unsuccessful. Please check email and/or password.", "danger") return render_template("login.html", title="Login", form=form, **getCurrentUserJson(current_user))
def updatePost(post_id): queried_post = Post.query.get_or_404(post_id) if queried_post.author != current_user: abort(403) form = PostForm() if form.validate_on_submit(): queried_post.title = form.title.data queried_post.content = form.content.data db.session.commit() flash("Your post has been updated.", "success") return redirect(url_for("posts.renderPost", post_id=queried_post.id)) elif request.method == "GET": form.title.data = queried_post.title form.content.data = queried_post.content form.title.data = queried_post.title form.content.data = queried_post.content return render_template("create_post.html", title="New Post", form=form, legend="Update post", **getCurrentUserJson(current_user))
def error_404_handler(error): return render_template("errors/404.html", **getCurrentUserJson(current_user)), 404
def error_500_handler(error): return render_template("errors/500.html", **getCurrentUserJson(current_user)), 500