suchwow/suchwow/routes/post.py

118 lines
4.2 KiB
Python
Raw Normal View History

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 secrets import token_urlsafe
from suchwow import wownero
2020-08-10 16:54:44 +00:00
from suchwow.models import Post, Comment
2020-08-10 06:59:45 +00:00
from suchwow.utils.decorators import login_required, profile_required
from suchwow.utils.helpers import allowed_file
bp = Blueprint("post", "post")
2020-08-10 22:58:39 +00:00
@bp.route("/posts/top")
def top():
top_posts = {}
posts = Post.select()
for post in posts:
transfers = []
incoming = wownero.Wallet().incoming_transfers(post.account_index)
if "transfers" in incoming:
for xfer in incoming["transfers"]:
transfers.append(wownero.from_atomic(xfer["amount"]))
total = sum(transfers)
if total > 0:
2020-08-27 04:47:14 +00:00
top_posts[float(total)] = post
return render_template("post/top.html", posts=sorted(top_posts.items(), reverse=True))
2020-08-10 22:58:39 +00:00
@bp.route("/post/<id>")
def read(id):
if Post.filter(id=id):
wallet = wownero.Wallet()
post = Post.get(id=id)
2020-08-10 16:54:44 +00:00
comments = Comment.select().where(Comment.post==post.id)
if wallet.connected:
address = wallet.addresses(account=post.account_index)[0]
transfers = wallet.transfers(account=post.account_index)
else:
address = "?"
transfers = "?"
return render_template(
"post/read.html",
post=post,
address=address,
comments=comments,
transfers=transfers
)
else:
2020-09-24 05:13:28 +00:00
flash("No meme there, brah")
return redirect(url_for("index"))
@bp.route("/post/create", methods=["GET", "POST"])
@login_required
2020-08-10 06:59:45 +00:00
@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 = "{}-{}".format(
token_urlsafe(12),
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")
2020-09-24 05:10:24 +00:00
@bp.route("/post/<id>/delete")
@login_required
def delete(id):
filtered = Post.filter(id=id)
user = session["auth"]["preferred_username"]
if filtered:
post = filtered.first()
if user == post.submitter:
post.delete_instance()
flash("Deleted that shit, brah!")
return redirect(url_for("index"))
else:
flash("You can't delete a meme you don't own, brah")
return redirect(url_for("post.read", id=post.id))
else:
flash("No meme there, brah")
return redirect(url_for("index"))
@bp.route("/uploads/<path:filename>")
def uploaded_file(filename):
file_path = path.join(current_app.config["DATA_FOLDER"], "uploads")
return send_from_directory(file_path, filename)