def post_template(post_id): post = session.query(Post).filter(Post.id == post_id).first() calc_rating = calculate_rating(session, post_id) if calc_rating: rating = calc_rating else: rating = 'No votes yet.' comments = session.query(Comment).filter(Comment.post_id == post_id).order_by(Comment.date_published.desc()).all() return render_template('post_template.j2', post=post, rating=rating, comments=comments)
def comment_post(post_id): if request.method == "POST": if current_user: content = request.form["content"] rating = request.form["rating"] if int(rating) == 1: rating = session.query(Rating).filter(Rating.id == 1).first() else: rating = session.query(Rating).filter(Rating.id == 2).first() comment(session=session, post_id=post_id, user_id=current_user.id, content=content, rating=rating.id) return redirect(url_for('sort_rating')) else: return render_template('comment-template.j2')
def account(user_id): user_id = current_user.id user = session.query(User).filter(User.id == user_id).first() if request.method == "POST": delete_user(session, user.id) return redirect(url_for('index')) else: return render_template('account.j2', user=user)
def write(): if request.method == "POST": if current_user: title = request.form["title"] content = request.form["editordata"] if title and content: post(session, current_user.id, title, content) new_post = session.query(Post).filter(Post.title == title).first() post_id = new_post.id return redirect(url_for('post_template', post_id=post_id)) return render_template('write.j2')
def index(): if request.method == "POST": try: email = request.form["email"] password = request.form["password"] except (TypeError, Flask.werkzeug.exceptions.BadRequestKeyError) as error: return redirect(url_for('incorrect')) else: if email and password: instance = session.query(User).filter(User.email == email).first() if instance: if bcrypt.checkpw(password.encode('utf8'), instance.password): login_user(instance) return redirect(url_for('sort_rating')) return redirect(url_for('incorrect')) else: return render_template('index.j2')
def load_user(user_id): return session.query(User).filter(User.id == user_id).first()
def fetch_rating(): posts = [i.as_dict() for i in session.query(Post).order_by(Post.likes.desc()).all()] return jsonify(posts)
def fetch_date(): posts = [i.as_dict() for i in session.query(Post).order_by(Post.date_published.desc()).all()] return jsonify(posts)