33/Flask-Web-Developer

/app/migrations/versions/02a8140731e4_.py
from app import db

class User(db.Model):
__tablename__ = "user"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), nullable=False)
password = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(50), nullable=False)
posts = db.relationship("Post", backref="user", lazy="dynamic", cascade="all, delete, delete-orphan")
comments = db.relationship("Comment", backref="user", lazy="dynamic", cascade="all, delete, delete-orphan")

class Post(db.Model):
__tablename__ = "post"
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
comments = db.relationship("Comment", backref="post", lazy="dynamic", cascade="all, delete, delete-orphan")

class Comment(db.Model):
__tablename__ = "comment"
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
post_id = db.Column(db.Integer, db.ForeignKey("post.id"), nullable=False)
replies = db.relationship("Reply", backref="comment", lazy="dynamic", cascade="all, delete, delete-orphan")
posts = db.relationship("Post", backref="comments", lazy="dynamic", cascade="all, delete, delete-orphan")

class Reply(db.Model):
__tablename__ = "reply"
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text, nullable=False)
comment_id = db.Column(db.Integer, db.ForeignKey("comment.id"), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)

def __repr__(self):
return "<Reply {}>".format(self.content)

/app/commands.py
from datetime import datetime
from app import db, login
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from app import login

@login.user_loader
def load_user(id):
return User.query.get(int(id))

class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
posts = db.relationship('Post', backref='author', lazy=True)

def __repr__(self):
return '<User {}>'.format(self.username)

def set_password(self, password):
self.password_hash = generate_password_hash(password)

def check_password(self, password):
return check_password_hash(self.password_hash, password)

class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(140), nullable=False)
body = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))

def __repr__(self):
return '<Post {}>'.format(self.body)

class Comment(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
parent_id = db.Column(db.Integer, db.ForeignKey('comment.id'))

def __repr__(self):
return '<Comment {}>'.format(self.body)

class Reply(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'))

def __repr__(self):
return '<Reply {}>'.format(self.body)

/app/models.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_migrate import Migrate

app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'

@app.route('/')
def hello():
return 'Hello World!'

@login.login_required
@app.route('/secret')
def secret():
return 'You are logged in.'

@app.errorhandler(404)
def page_not_found(e):
return '<h1>Page Not Found</h1>', 404

if __name__ == '__main__':
app.run()

/app/main.py
from flask import Blueprint, render_template
from flask_login import login_required, current_user

main = Blueprint('main', __name__)

@main.route('/')
def index():
return render_template('index.html')

@main.route('/profile')
@login_required
def profile():
return render_template('profile.html', name=current_user.username)

/app/templates/index.html
from app import app

if