2020-07-29 00:24:59 +00:00
|
|
|
from os import path
|
2020-08-08 06:12:33 +00:00
|
|
|
from flask import render_template, Blueprint, request, session, flash
|
2020-07-30 08:36:31 +00:00
|
|
|
from flask import send_from_directory, redirect, url_for, current_app
|
2020-07-29 00:24:59 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from suchwow.models import Post
|
|
|
|
from suchwow.utils.decorators import login_required
|
2020-07-30 08:36:31 +00:00
|
|
|
from suchwow.utils.helpers import allowed_file
|
2020-07-29 00:24:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2020-07-30 08:36:31 +00:00
|
|
|
save_path = path.join(current_app.config["UPLOAD_FOLDER"], filename)
|
2020-07-29 00:24:59 +00:00
|
|
|
file.save(save_path)
|
|
|
|
# gen wallet
|
|
|
|
post = Post(
|
|
|
|
title=post_title,
|
2020-08-08 06:12:33 +00:00
|
|
|
text=request.form.get("text", ""),
|
2020-07-29 00:24:59 +00:00
|
|
|
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))
|
2020-07-30 08:36:31 +00:00
|
|
|
return render_template("post/create.html")
|
|
|
|
|
|
|
|
@bp.route("/uploads/<path:filename>")
|
|
|
|
def uploaded_file(filename):
|
2020-08-08 06:12:33 +00:00
|
|
|
return send_from_directory(current_app.config["UPLOAD_FOLDER"], filename)
|