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
|
2020-09-24 04:55:00 +00:00
|
|
|
from secrets import token_urlsafe
|
2020-08-10 05:47:53 +00:00
|
|
|
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
|
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")
|
|
|
|
|
2020-08-10 22:58:39 +00:00
|
|
|
@bp.route("/posts/top")
|
|
|
|
def top():
|
|
|
|
top_posts = {}
|
|
|
|
posts = Post.select()
|
|
|
|
for post in posts:
|
2020-08-16 09:02:02 +00:00
|
|
|
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
|
|
|
|
2020-07-29 00:24:59 +00:00
|
|
|
@bp.route("/post/<id>")
|
|
|
|
def read(id):
|
|
|
|
if Post.filter(id=id):
|
2020-08-10 05:47:53 +00:00
|
|
|
wallet = wownero.Wallet()
|
|
|
|
post = Post.get(id=id)
|
2020-08-10 16:54:44 +00:00
|
|
|
comments = Comment.select().where(Comment.post==post.id)
|
2020-08-10 05:47:53 +00:00
|
|
|
if wallet.connected:
|
|
|
|
address = wallet.addresses(account=post.account_index)[0]
|
2020-08-10 22:11:53 +00:00
|
|
|
transfers = wallet.transfers(account=post.account_index)
|
2020-08-10 05:47:53 +00:00
|
|
|
else:
|
|
|
|
address = "?"
|
2020-08-10 22:11:53 +00:00
|
|
|
transfers = "?"
|
|
|
|
return render_template(
|
|
|
|
"post/read.html",
|
|
|
|
post=post,
|
|
|
|
address=address,
|
|
|
|
comments=comments,
|
|
|
|
transfers=transfers
|
|
|
|
)
|
2020-07-29 00:24:59 +00:00
|
|
|
else:
|
2020-09-24 05:13:28 +00:00
|
|
|
flash("No meme there, brah")
|
|
|
|
return redirect(url_for("index"))
|
2020-07-29 00:24:59 +00:00
|
|
|
|
|
|
|
@bp.route("/post/create", methods=["GET", "POST"])
|
|
|
|
@login_required
|
2020-08-10 06:59:45 +00:00
|
|
|
@profile_required
|
2020-07-29 00:24:59 +00:00
|
|
|
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):
|
2020-09-24 04:55:00 +00:00
|
|
|
filename = "{}-{}".format(
|
|
|
|
token_urlsafe(12),
|
|
|
|
secure_filename(file.filename)
|
|
|
|
)
|
2020-08-10 05:47:53 +00:00
|
|
|
save_path_base = path.join(current_app.config["DATA_FOLDER"], "uploads")
|
|
|
|
save_path = path.join(save_path_base, filename)
|
2020-07-29 00:24:59 +00:00
|
|
|
file.save(save_path)
|
2020-08-10 05:47:53 +00:00
|
|
|
try:
|
|
|
|
wallet = wownero.Wallet()
|
|
|
|
account_index = wallet.new_account()
|
|
|
|
except:
|
|
|
|
account_index = 0
|
2020-07-29 00:24:59 +00:00
|
|
|
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,
|
2020-08-10 05:47:53 +00:00
|
|
|
account_index=account_index,
|
2020-07-29 00:24:59 +00:00
|
|
|
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")
|
|
|
|
|
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"))
|
|
|
|
|
2020-07-30 08:36:31 +00:00
|
|
|
@bp.route("/uploads/<path:filename>")
|
|
|
|
def uploaded_file(filename):
|
2020-08-10 05:47:53 +00:00
|
|
|
file_path = path.join(current_app.config["DATA_FOLDER"], "uploads")
|
|
|
|
return send_from_directory(file_path, filename)
|