text: from datetime import datetime
  from flask import Flask, flash, g, redirect, render_template, request, session, url_for
  from flask_wtf.csrf import CSRFProtect
  from models import User, Post
  from sqlalchemy import create_engine
  from sqlalchemy.orm import Session
  from werkzeug.security import check_password_hash, generate_password_hash
  
  
  # App setup
  app = Flask(__name__)
  app.config.from_object('config.Config')
  CSRFProtect(app)
  
  # Connect to database
  db = Session(create_engine(app.config['DATABASE_URL'],
                             echo=app.config['ECHO']))
  
  
  @app.before_request
  def load_user():
      """Load logged in user, if one exists."""
      user_id = session.get('user_id')
  
      if user_id is None:
          g.user = None
      else:
          g.user = db.query(User).get(user_id)
  
  
  @app.route('/')
  def index():
      posts = db.query(Post).order_by(Post.time_posted.desc()).all()
      return render_template('index.html', posts=posts)
  
  
  @app.route('/login', methods=['GET', 'POST'])
  def login():
      if g.user:
          return redirect(url_for('index'))
  
      elif request.method == 'GET':
          return render_template('login.html')
  
      elif request.method == 'POST':
          username = request.form['username']
          password = request.form['password']
  
          user = db.query(User).filter(User.username == username).first()
          if user is None or not check_password_hash(user.pw_hash, password):
              error = 'Invalid username/password.'
              return render_template('login.html', error=error)
  
          session['user_id'] = user.id
          session.permanent = True
  
          flash('You have been logged in.')
          return redirect(url_for('index'))
  
  
  @app.route('/logout', methods=['POST'])
  def logout():
      session.clear()
      flash('You have been logged out.')
      return redirect(url_for('index'))
  
  
  @app.route('/register', methods=['GET', 'POST'])
  def register():
      if g.user:
          return redirect(url_for('index'))
  
      elif request.method == 'GET':
          return render_template('register.html')
  
      elif request.method == 'POST':
          username = request.form['username']
          password = request.form['password']
  
          if db.query(User).filter(User.username == username).first():
              error = 'That username is already taken. Please choose a different one.'
              return render_template('register.html', error=error)
  
          pw_hash = generate_password_hash(password)
          user = User(username=username, pw_hash=pw_hash)
          db.add(user)
          db.commit()
  
          session['user_id'] = user.id
          session.permanent = True
  
          flash('Your account has been created. You have been logged in.')
          return redirect(url_for('index'))
  
  
  @app.route('/post', methods=['POST'])
  def post():
      if not g.user:
          flash('You must be logged in to post notes.')
          return redirect(url_for('/login'))
  
      content = request.form['content']
  
      g.user.posts.append(
          Post(content=content, time_posted=str(datetime.utcnow())))
      db.add(g.user)
      db.commit()
  
      flash('Your note has been posted.')
      return redirect(url_for('index'))
  