from os import path from flask import render_template, Blueprint, request, session, flash from flask import send_from_directory, redirect, url_for, current_app from werkzeug.utils import secure_filename from suchwow import wownero from suchwow.models import Post from suchwow.utils.decorators import login_required, profile_required from suchwow.utils.helpers import allowed_file bp = Blueprint("post", "post") @bp.route("/post/") def read(id): if Post.filter(id=id): wallet = wownero.Wallet() post = Post.get(id=id) if wallet.connected: address = wallet.addresses(account=post.account_index)[0] else: address = "?" return render_template("post/read.html", post=post, address=address) else: return "no meme there brah" @bp.route("/post/create", methods=["GET", "POST"]) @login_required @profile_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_base = path.join(current_app.config["DATA_FOLDER"], "uploads") save_path = path.join(save_path_base, filename) file.save(save_path) try: wallet = wownero.Wallet() account_index = wallet.new_account() except: account_index = 0 post = Post( title=post_title, text=request.form.get("text", ""), submitter=session["auth"]["preferred_username"], image_name=filename, account_index=account_index, address_index=0 ) post.save() return redirect(url_for("post.read", id=post.id)) return render_template("post/create.html") @bp.route("/uploads/") def uploaded_file(filename): file_path = path.join(current_app.config["DATA_FOLDER"], "uploads") return send_from_directory(file_path, filename)