View raw

1 """ 2 routes.py module 3 ---------------- 4 5 This Python module contains the logic supporting: 6 1. Navigating between website pages 7 2. Interpreting user requests to the server 8 3. Dispatching requested content back to the user 9 10 Python dependencies: 11 - flask: provides web application features 12 - forms: provides secure user form submission 13 - sqlalchemy: provides communication with database on server. 14 15 Personal imports: 16 These are used to avoid cluttering this file with 17 placeholder data for posts' content. 18 """ 19 20 from datetime import datetime 21 from flask import Flask, render_template, flash, redirect, url_for 22 from forms import RegistrationForm, LoginForm 23 from flask_sqlalchemy import SQLAlchemy 24 25 from placeholder_posts import posts 26 from placeholder_news import news 27 from placeholder_messages import conversations 28 29 app = Flask(__name__) 30 app.config['SECRET_KEY'] = 'Scooby_Lu,_where_are_you?' 31 app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' 32 db = SQLAlchemy(app) 33 34 35 class Alias(db.Model): 36 id = db.Column(db.Integer, primary_key=True) 37 username = db.Column(db.String(20), unique=True, nullable=False) 38 email = db.Column(db.String(120), unique=True, nullable=False) 39 image_file = db.Column(db.String(20), 40 nullable=False, 41 default='default.png') 42 password = db.Column(db.String(60), nullable=False) 43 posts = db.relationship('Post', backref='author', lazy=True) 44 45 def __repr__(self): 46 return f"Alias('{self.username}','{self.email}','{self.image_file}' )" 47 48 49 class Post(db.Model): 50 id = db.Column(db.Integer, primary_key=True) 51 title = db.Column(db.String(100), nullable=False) 52 date_posted = db.Column(db.DateTime, 53 nullable=False, 54 default=datetime.utcnow) 55 content = db.Column(db.Text, nullable=False) 56 user_id = db.Column(db.Integer, db.ForeignKey('alias.id'), nullable=False) 57 58 def __repr__(self): 59 return f"Post('{self.title}', '{self.date_posted}')" 60 61 62 @app.route("/") 63 @app.route("/home") 64 def home(): 65 """This is our homepage.""" 66 home_posts = posts + news + conversations 67 return render_template('home.html', posts=home_posts) 68 69 70 @app.route("/yes") 71 def yes(): 72 """Another page, presenting the user with content marked as 'yes'.""" 73 return render_template("yes.html", conversations=conversations, news=news) 74 75 76 @app.route("/no") 77 def no(): 78 """This page contains an archive of 'no's, for forgiving user mistakes.""" 79 archives = posts + news + messages 80 return render_template("no.html", archive=archives) 81 82 83 @app.route("/register", methods=['GET', 'POST']) 84 def register(): 85 """Account registration page leveraging the form Python package.""" 86 form = RegistrationForm() 87 if form.validate_on_submit(): 88 flash(f"Alias created for {form.alias.data}.", 'success') 89 return redirect(url_for('home')) 90 return render_template('register.html', title="Register", form=form) 91 92 93 @app.route("/login") 94 def login(): 95 form = LoginForm() 96 return render_template('login.html', title="Login", form=form) 97 98 99 # If this file is executed as a script (i.e. double-clicked), 100 # the Python interpreter will run the Flask process and begin serving 101 # the web pages on the standard localhost address (127.0.0.1). 102 # But if this file is called as a module by another Python script, it will not 103 # serve content to the web pages, but the function definitions contained in 104 # this file will be available to the calling script. 105 # E.g. calling script will know what the yes() function is. 106 if __name__ == '__main__': 107 app.run(debug=True) 108