def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: g.user = get_session().query(User).filter(User.id == user_id).first()
def delete(id): get_post(id) db = get_session() post = db.query(Post).filter(Post.id == id).first() db.delete(post) db.commit() db.close() return redirect(url_for('blog.index'))
def get_post(id, check_author=True): post = get_session().query(Post).join(User).filter(Post.id == id).first() if post is None: abort(404, "Post id {0} doesn't exist.".format(id)) if check_author and post.author_id != g.user.id: abort(403) return post
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: orm_session = get_session() user = orm_session.query( User.UserId).filter(User.UserId == user_id).first() g.user = user[0]
def flag_comment(id): user_id = g.user.id db = get_session() if db.query(FlaggedComment).filter(FlaggedComment.user_id == user_id)\ .filter(FlaggedComment.comment_id == id).first() is None : flagged_comment = FlaggedComment(user_id, id) db.add(flagged_comment) db.commit() db.close() return redirect(url_for('blog.index'))
def index(): db = get_session() posts = db.query(Post, User)\ .join(User)\ .all() comments = db.query(Comment, User)\ .join(User)\ .all() flagged_comments = db.query(FlaggedComment.comment_id, func.count(FlaggedComment.comment_id))\ .group_by(FlaggedComment.comment_id).all() return render_template('blog/index.html', posts=posts, comments=comments, flagged_comments=flagged_comments)
def comment(id): if request.method == 'POST': body = request.form['body'] error = None if not body: error = 'Body is required.' if error is not None: flash(error) else: db = get_session() comment = Comment(body, g.user.id, id) db.add(comment) db.commit() db.close() return redirect(url_for('blog.index')) return render_template('blog/comment.html')
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_session() error = None user = db.query(User).filter(User.username == username).first() if user is None: error = 'Incorrect username.' elif not check_password_hash(user.password, password): error = 'Incorrect password.' if error is None: session.clear() session['user_id'] = user.id return redirect(url_for('index')) flash(error) return render_template('auth/login.html')
def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] orm_session = get_session() error = None user = orm_session.query(User).filter(User.UserId == username).first() if user is None: error = 'Incorrect username.' elif not user.HashedPassword == password: error = 'Incorrect password.' if error is None: session.clear() session['user_id'] = user.UserId return redirect(url_for('index')) flash(error) return render_template('auth/login.html')
def update(id): post = get_post(id) if request.method == 'POST': title = request.form['title'] body = request.form['body'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) else: db = get_session() post = db.query(Post).filter(Post.id == id).first() post.title = title post.body = body db.commit() db.close() return redirect(url_for('blog.index')) return render_template('blog/update.html', post=post)
def create(): if request.method == 'POST': title = request.form['title'] body = request.form['body'] error = None if not title: error = 'Title is required.' if not body: error = 'Body is required.' if error is not None: flash(error) else: db = get_session() post = Post(title, body, g.user.id) db.add(post) db.commit() db.close() return redirect(url_for('blog.index')) return render_template('blog/create.html')
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db = get_session() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' elif len(db.query(User).filter(User.username == username).all()) != 0: error = 'User {} is already registered.'.format(username) if error is None: user = User(username, generate_password_hash(password)) db.add(user) db.commit() db.close() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] orm_session = get_session() error = None if not username: error = 'Username is required.' elif not password: error = 'Password is required.' else: if len( orm_session.query(User.UserId).filter( User.UserId == username).all()) > 0: error = 'User {} is already registered.'.format(username) if error is None: new_user = User(UserId=username, HashedPassword=password) orm_session.add(new_user) orm_session.commit() return redirect(url_for('auth.login')) return render_template('auth/register.html')