more refactoring, setting up individual route blueprints
parent
37af78e539
commit
336740319d
@ -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/<id>")
|
||||||
|
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")
|
@ -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
|
@ -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
|
Loading…
Reference in New Issue