From 336740319d60e1dd59ce222ffa0d253acef6e87b Mon Sep 17 00:00:00 2001 From: lza_menace Date: Tue, 28 Jul 2020 17:24:59 -0700 Subject: [PATCH] more refactoring, setting up individual route blueprints --- suchwow/app.py | 79 ++----------------- suchwow/routes/auth.py | 0 suchwow/routes/comment.py | 0 suchwow/routes/post.py | 51 ++++++++++++ suchwow/routes/profile.py | 0 suchwow/templates/index.html | 4 +- .../{create_post.html => post/create.html} | 0 .../templates/{post.html => post/read.html} | 0 suchwow/utils/__init__.py | 0 suchwow/utils/decorators.py | 15 ++++ suchwow/utils/helpers.py | 16 ++++ 11 files changed, 90 insertions(+), 75 deletions(-) create mode 100644 suchwow/routes/auth.py create mode 100644 suchwow/routes/comment.py create mode 100644 suchwow/routes/post.py create mode 100644 suchwow/routes/profile.py rename suchwow/templates/{create_post.html => post/create.html} (100%) rename suchwow/templates/{post.html => post/read.html} (100%) create mode 100644 suchwow/utils/__init__.py create mode 100644 suchwow/utils/decorators.py create mode 100644 suchwow/utils/helpers.py diff --git a/suchwow/app.py b/suchwow/app.py index 1a5c7fb..8c4b7c8 100644 --- a/suchwow/app.py +++ b/suchwow/app.py @@ -1,14 +1,14 @@ -from functools import wraps import uuid -import os import json import requests -from flask import Flask, request, redirect, url_for, abort -from flask import jsonify, render_template, flash, session +from flask import Flask, request, redirect, url_for +from flask import render_template, flash, session from flask import send_from_directory, make_response +from flask import jsonify from flask_session import Session -from werkzeug.utils import secure_filename from suchwow.models import Post, Profile, Comment, Notification, db +from suchwow.routes import auth, comment, post, profile +from suchwow.utils.decorators import login_required app = Flask(__name__) @@ -21,31 +21,7 @@ OPENID_CLIENT_ID = app.config["OIDC_CLIENT_ID"] OPENID_CLIENT_SECRET = app.config["OIDC_CLIENT_SECRET"] OPENID_REDIRECT_URI = "http://localhost:5000/auth" - -def allowed_file(filename): - return "." in filename and \ - filename.rsplit(".", 1)[1].lower() in app.config["ALLOWED_EXTENSIONS"] - -def debug_login(): - session["auth"] = { - "state": "active", - "session_state": "debug", - "code": "abcdefg", - "preferred_username": "debuguser", - "debug": True - } - return - -def login_required(f): - @wraps(f) - def decorated_function(*args, **kwargs): - if app.debug: - debug_login() - return f(*args, **kwargs) - if "auth" not in session or not session["auth"]: - return redirect(url_for("login")) - return f(*args, **kwargs) - return decorated_function +app.register_blueprint(post.bp) @app.route("/") @@ -58,53 +34,10 @@ def index(): posts = Post.select().order_by(Post.timestamp).paginate(int(page), 10) return render_template("index.html", posts=posts, page=page) -@app.route("/post/") -def read_post(id): - if Post.filter(id=id): - post = Post.get(Post.id == id) - return render_template("post.html", post=post) - else: - return "no meme there brah" - @app.route("/uploads/") def uploaded_file(filename): return send_from_directory(app.config["UPLOAD_FOLDER"], filename) -@app.route("/post/create", methods=["GET", "POST"]) -@login_required -def create_post(): - if request.method == "POST": - post_title = request.form.get("title") - # check if the post request has the file part - if "file" not in request.files: - flash("You didn't upload a caliente meme, bro! You're fuckin up!") - return redirect(request.url) - file = request.files["file"] - # if user does not select file, browser also - # submit an empty part without filename - if file.filename == "": - flash("You didn't upload a caliente meme, bro! You're fuckin up!") - return redirect(request.url) - if post_title is "": - flash("You didn't give your meme a spicy title, bro! You're fuckin up!") - return redirect(request.url) - if file and allowed_file(file.filename): - filename = secure_filename(file.filename) - save_path = os.path.join(app.config["UPLOAD_FOLDER"], filename) - file.save(save_path) - # gen wallet - post = Post( - title=post_title, - subtitle=request.form.get("subtitle", ""), - submitter=session["auth"]["preferred_username"], - image_name=filename, - account_index=0, - address_index=0 - ) - post.save() - return redirect(url_for("read_post", id=post.id)) - return render_template("create_post.html") - @app.route("/login") def login(): state = uuid.uuid4().hex diff --git a/suchwow/routes/auth.py b/suchwow/routes/auth.py new file mode 100644 index 0000000..e69de29 diff --git a/suchwow/routes/comment.py b/suchwow/routes/comment.py new file mode 100644 index 0000000..e69de29 diff --git a/suchwow/routes/post.py b/suchwow/routes/post.py new file mode 100644 index 0000000..88b77a3 --- /dev/null +++ b/suchwow/routes/post.py @@ -0,0 +1,51 @@ +from os import path +from flask import render_template, Blueprint, request, session +from werkzeug.utils import secure_filename +from suchwow.models import Post +from suchwow.utils.decorators import login_required + + +bp = Blueprint("post", "post") + +@bp.route("/post/") +def read(id): + if Post.filter(id=id): + post = Post.get(Post.id == id) + return render_template("post/read.html", post=post) + else: + return "no meme there brah" + +@bp.route("/post/create", methods=["GET", "POST"]) +@login_required +def create(): + if request.method == "POST": + post_title = request.form.get("title") + # check if the post request has the file part + if "file" not in request.files: + flash("You didn't upload a caliente meme, bro! You're fuckin up!") + return redirect(request.url) + file = request.files["file"] + # if user does not select file, browser also + # submit an empty part without filename + if file.filename == "": + flash("You didn't upload a caliente meme, bro! You're fuckin up!") + return redirect(request.url) + if post_title is "": + flash("You didn't give your meme a spicy title, bro! You're fuckin up!") + return redirect(request.url) + if file and allowed_file(file.filename): + filename = secure_filename(file.filename) + save_path = path.join(app.config["UPLOAD_FOLDER"], filename) + file.save(save_path) + # gen wallet + post = Post( + title=post_title, + subtitle=request.form.get("subtitle", ""), + submitter=session["auth"]["preferred_username"], + image_name=filename, + account_index=0, + address_index=0 + ) + post.save() + return redirect(url_for("post.read", id=post.id)) + return render_template("post/create.html") \ No newline at end of file diff --git a/suchwow/routes/profile.py b/suchwow/routes/profile.py new file mode 100644 index 0000000..e69de29 diff --git a/suchwow/templates/index.html b/suchwow/templates/index.html index 10d6f6a..cb4ea70 100644 --- a/suchwow/templates/index.html +++ b/suchwow/templates/index.html @@ -2,7 +2,7 @@ {% block content %} - Submit A Meme
+ Submit A Meme
Debug {% block header %} @@ -11,7 +11,7 @@ diff --git a/suchwow/templates/create_post.html b/suchwow/templates/post/create.html similarity index 100% rename from suchwow/templates/create_post.html rename to suchwow/templates/post/create.html diff --git a/suchwow/templates/post.html b/suchwow/templates/post/read.html similarity index 100% rename from suchwow/templates/post.html rename to suchwow/templates/post/read.html diff --git a/suchwow/utils/__init__.py b/suchwow/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/suchwow/utils/decorators.py b/suchwow/utils/decorators.py new file mode 100644 index 0000000..615ce8d --- /dev/null +++ b/suchwow/utils/decorators.py @@ -0,0 +1,15 @@ +from flask import current_app, session, redirect, url_for +from functools import wraps +from suchwow.utils.helpers import debug_login + + +def login_required(f): + @wraps(f) + def decorated_function(*args, **kwargs): + if current_app.debug: + debug_login() + return f(*args, **kwargs) + if "auth" not in session or not session["auth"]: + return redirect(url_for("login")) + return f(*args, **kwargs) + return decorated_function \ No newline at end of file diff --git a/suchwow/utils/helpers.py b/suchwow/utils/helpers.py new file mode 100644 index 0000000..f8f6c7b --- /dev/null +++ b/suchwow/utils/helpers.py @@ -0,0 +1,16 @@ +from flask import current_app, session + + +def allowed_file(filename): + return "." in filename and \ + filename.rsplit(".", 1)[1].lower() in current_app.config["ALLOWED_EXTENSIONS"] + +def debug_login(): + session["auth"] = { + "state": "active", + "session_state": "debug", + "code": "abcdefg", + "preferred_username": "debuguser", + "debug": True + } + return \ No newline at end of file