mirror of
https://github.com/Xevion/runnerspace.git
synced 2025-12-07 05:16:22 -06:00
Refactor and merge Zach's work into main
This commit is contained in:
0
__init__.py
Normal file
0
__init__.py
Normal file
61
auth.py
Normal file
61
auth.py
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
from flask import Blueprint, flash, redirect, request, url_for
|
||||||
|
from flask_login import login_required, login_user, logout_user
|
||||||
|
from werkzeug.security import check_password_hash, generate_password_hash
|
||||||
|
|
||||||
|
from .models import User
|
||||||
|
from .create_app import db
|
||||||
|
|
||||||
|
blueprint = Blueprint('auth', __name__)
|
||||||
|
|
||||||
|
'''
|
||||||
|
FIXME this will have to be revisited later with added funcitonality,
|
||||||
|
as right now `login`, `signup`, and `logout` only return text
|
||||||
|
|
||||||
|
There will also be routes for handling POST requests from login and signup
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
@blueprint.route('/login', methods=['POST'])
|
||||||
|
def login_post():
|
||||||
|
username = request.form.get('username')
|
||||||
|
password = request.form.get('password')
|
||||||
|
remember = bool(request.form.get('remember'))
|
||||||
|
|
||||||
|
user = User.query.filter_by(username=username).first()
|
||||||
|
|
||||||
|
# check if the user actually exists, and compare password given
|
||||||
|
if not user or not check_password_hash(user.password, password):
|
||||||
|
flash('Please check your login details and try again.')
|
||||||
|
return redirect(url_for('main.login'))
|
||||||
|
|
||||||
|
login_user(user, remember=remember)
|
||||||
|
return redirect(url_for('main.index'))
|
||||||
|
|
||||||
|
|
||||||
|
@blueprint.route('/signup', methods=['POST'])
|
||||||
|
def signup_post():
|
||||||
|
# validate and add user to db
|
||||||
|
username = request.form.get('username')
|
||||||
|
name = request.form.get('name')
|
||||||
|
password = request.form.get('password')
|
||||||
|
|
||||||
|
user = User.query.filter_by(username=username).first() # Check if the email exists
|
||||||
|
if user: # redirect back to sign-up page
|
||||||
|
flash('Email address already exists')
|
||||||
|
return redirect(url_for('main.signup'))
|
||||||
|
|
||||||
|
# Create new user with form data
|
||||||
|
new_user = User(username=username, name=name, password=generate_password_hash(password, method='sha256'))
|
||||||
|
|
||||||
|
# Add new user to db
|
||||||
|
db.session.add(new_user)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return redirect(url_for('main.login'))
|
||||||
|
|
||||||
|
|
||||||
|
@blueprint.route('/logout')
|
||||||
|
@login_required
|
||||||
|
def logout():
|
||||||
|
logout_user()
|
||||||
|
return redirect(url_for('main.index'))
|
||||||
@@ -5,11 +5,13 @@ from flask_login import LoginManager
|
|||||||
# init SQLAlchemy
|
# init SQLAlchemy
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
|
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
app.config['SECRET_KEY'] = 'secret key goes here'
|
app.config['SECRET_KEY'] = 'secret key goes here'
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
|
||||||
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||||
|
|
||||||
db.init_app(app)
|
db.init_app(app)
|
||||||
|
|
||||||
@@ -23,14 +25,16 @@ def create_app():
|
|||||||
def load_user(user_id):
|
def load_user(user_id):
|
||||||
return User.query.get(int(user_id))
|
return User.query.get(int(user_id))
|
||||||
|
|
||||||
# idk if i need the rest of this shit below
|
from .auth import blueprint as auth_blueprint
|
||||||
|
|
||||||
# blueprint for auth routes in app
|
|
||||||
from .auth import auth as auth_blueprint
|
|
||||||
app.register_blueprint(auth_blueprint)
|
app.register_blueprint(auth_blueprint)
|
||||||
|
|
||||||
# blueprint for non-auth parts of app
|
from .routes import blueprint as routes_blueprint
|
||||||
from .main import main as main_blueprint
|
app.register_blueprint(routes_blueprint)
|
||||||
app.register_blueprint(main_blueprint)
|
|
||||||
|
# CLI commands setup
|
||||||
|
@app.shell_context_processor
|
||||||
|
def shell_context():
|
||||||
|
"""Provides specific Flask components to the shell."""
|
||||||
|
return {'app': app, 'db': db}
|
||||||
|
|
||||||
return app
|
return app
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
from flask import Blueprint, render_template, redirect, url_for, request, flash
|
|
||||||
from flask_login import login_user, current_user, login_required, logout_user
|
|
||||||
from werkzeug.security import generate_password_hash, check_password_hash
|
|
||||||
from .models import User
|
|
||||||
from . import db
|
|
||||||
|
|
||||||
auth = Blueprint('auth', __name__)
|
|
||||||
|
|
||||||
'''
|
|
||||||
FIXME this will have to be revisited later with added funcitonality,
|
|
||||||
as right now `login`, `signup`, and `logout` only return text
|
|
||||||
|
|
||||||
There will also be routes for handling POST requests from login and signup
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
@auth.route('/login')
|
|
||||||
def login():
|
|
||||||
return render_template('login.html')
|
|
||||||
|
|
||||||
@auth.route('/login', methods=['POST'])
|
|
||||||
def login_post():
|
|
||||||
email = request.form.get('email')
|
|
||||||
password = request.form.get('password')
|
|
||||||
remember = True if request.form.get('remember') else False
|
|
||||||
|
|
||||||
user = User.query.filter_by(email=email).first()
|
|
||||||
|
|
||||||
# check if the user actually exists, and compare password given
|
|
||||||
if not user or not check_password_hash(user.password, password):
|
|
||||||
flash('Please check your login details and try again.')
|
|
||||||
return redirect(url_for('auth.login'))
|
|
||||||
|
|
||||||
login_user(user, remember=remember)
|
|
||||||
return redirect(url_for('main.profile'))
|
|
||||||
|
|
||||||
|
|
||||||
@auth.route('/signup')
|
|
||||||
def signup():
|
|
||||||
return render_template('signup.html')
|
|
||||||
|
|
||||||
|
|
||||||
@auth.route('/signup', methods=['POST'])
|
|
||||||
def signup_post():
|
|
||||||
# validate and add user to db
|
|
||||||
email = request.form.get('email')
|
|
||||||
name = request.form.get('name')
|
|
||||||
password = request.form.get('password')
|
|
||||||
|
|
||||||
user = User.query.filter_by(email=email).first() # Check if the email exists
|
|
||||||
if user: # redirect back to sign-up page
|
|
||||||
flash('Email address already exists')
|
|
||||||
return(redirect(url_for('auth.signup')))
|
|
||||||
|
|
||||||
# Create new user with form data
|
|
||||||
new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256'))
|
|
||||||
|
|
||||||
# Add new user to db
|
|
||||||
db.session.add(new_user)
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
return redirect(url_for('auth.login'))
|
|
||||||
|
|
||||||
|
|
||||||
@auth.route('/logout')
|
|
||||||
@login_required
|
|
||||||
def logout():
|
|
||||||
logout_user()
|
|
||||||
return redirect(url_for('main.index'))
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
from flask import Blueprint, render_template
|
|
||||||
from flask_login import login_required, current_user
|
|
||||||
from . import db
|
|
||||||
|
|
||||||
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.name)
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from flask_login import UserMixin
|
from flask_login import UserMixin
|
||||||
from . import db
|
from .create_app import db
|
||||||
|
|
||||||
class User(UserMixin, db.Model):
|
class User(UserMixin, db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
@@ -1,64 +1,65 @@
|
|||||||
from flask import Flask, render_template
|
from flask import Blueprint, render_template
|
||||||
|
from flask_login import login_required, current_user
|
||||||
|
|
||||||
app = Flask(__name__)
|
blueprint = Blueprint('main', __name__)
|
||||||
|
|
||||||
user = {
|
|
||||||
'username': 'Xevion',
|
|
||||||
'logged_in': False
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@blueprint.route('/profile')
|
||||||
|
@login_required
|
||||||
|
def profile():
|
||||||
|
return render_template('layouts/profile.html', name=current_user.name)
|
||||||
|
|
||||||
|
|
||||||
|
@blueprint.route('/')
|
||||||
def index(): # put application's code here
|
def index(): # put application's code here
|
||||||
return render_template('layouts/index.html', user=user)
|
return render_template('layouts/index.html', user=user)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/about')
|
@blueprint.route('/about')
|
||||||
def about():
|
def about():
|
||||||
return render_template('pages/about.html', user=user)
|
return render_template('pages/about.html', user=user)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/users')
|
@blueprint.route('/users')
|
||||||
def browse():
|
def browse():
|
||||||
return render_template('pages/browse.html', user=user)
|
return render_template('pages/browse.html', user=user)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/feed')
|
@blueprint.route('/feed')
|
||||||
def feed():
|
def feed():
|
||||||
return render_template('pages/feed.html', user=user)
|
return render_template('pages/feed.html', user=user)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/messages')
|
@blueprint.route('/messages')
|
||||||
def messages():
|
def messages():
|
||||||
return render_template('pages/messages.html', user=user)
|
return render_template('pages/messages.html', user=user)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/search')
|
@blueprint.route('/search')
|
||||||
def search():
|
def search():
|
||||||
return render_template('pages/search.html', user=user)
|
return render_template('pages/search.html', user=user)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/user/<username>')
|
@blueprint.route('/user/<username>')
|
||||||
def user(username: str):
|
def user(username: str):
|
||||||
return render_template('pages/about.html', user=user)
|
return render_template('pages/user.html', user=user)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/blogs')
|
@blueprint.route('/blogs')
|
||||||
def blogs():
|
def blogs():
|
||||||
return render_template('pages/blogs.html', user=user)
|
return render_template('pages/blogs.html', user=user)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/groups')
|
@blueprint.route('/groups')
|
||||||
def groups():
|
def groups():
|
||||||
return render_template('pages/groups.html', user=user)
|
return render_template('pages/groups.html', user=user)
|
||||||
|
|
||||||
@app.route('/login')
|
|
||||||
|
@blueprint.route('/login', methods=['GET'])
|
||||||
def login():
|
def login():
|
||||||
return render_template('pages/login.html', user=user)
|
return render_template('pages/login.html', user=user)
|
||||||
|
|
||||||
@app.route('/signup')
|
|
||||||
|
@blueprint.route('/signup', methods=['GET'])
|
||||||
def signup():
|
def signup():
|
||||||
return render_template('pages/signup.html', user=user)
|
return render_template('pages/signup.html', user=user)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app.run(host='0.0.0.0', debug=True)
|
|
||||||
Reference in New Issue
Block a user