|
|
|
@ -3,13 +3,12 @@ import uuid
|
|
|
|
|
import os
|
|
|
|
|
import json
|
|
|
|
|
import requests
|
|
|
|
|
from flask import Flask, g, request, redirect, url_for, abort
|
|
|
|
|
from flask import Flask, request, redirect, url_for, abort
|
|
|
|
|
from flask import jsonify, render_template, flash, session
|
|
|
|
|
from flask import send_from_directory, make_response
|
|
|
|
|
from flask_session import Session
|
|
|
|
|
from playhouse.flask_utils import PaginatedQuery
|
|
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
|
from suchwow.models import Meme, db
|
|
|
|
|
from suchwow.models import Post, Profile, Comment, Notification, db
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
@ -17,9 +16,9 @@ app.config.from_envvar("FLASK_SECRETS")
|
|
|
|
|
app.secret_key = app.config["SECRET_KEY"]
|
|
|
|
|
Session(app)
|
|
|
|
|
|
|
|
|
|
OPENID_URL = app.config["OIDC_URL"][0]
|
|
|
|
|
OPENID_CLIENT_ID = app.config["OIDC_CLIENT_ID"][0]
|
|
|
|
|
OPENID_CLIENT_SECRET = app.config["OIDC_CLIENT_SECRET"][0]
|
|
|
|
|
OPENID_URL = app.config["OIDC_URL"]
|
|
|
|
|
OPENID_CLIENT_ID = app.config["OIDC_CLIENT_ID"]
|
|
|
|
|
OPENID_CLIENT_SECRET = app.config["OIDC_CLIENT_SECRET"]
|
|
|
|
|
OPENID_REDIRECT_URI = "http://localhost:5000/auth"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -27,9 +26,22 @@ def allowed_file(filename):
|
|
|
|
|
return "." in filename and \
|
|
|
|
|
filename.rsplit(".", 1)[1].lower() in app.config["ALLOWED_EXTENSIONS"]
|
|
|
|
|
|
|
|
|
|
def debug_login():
|
|
|
|
|
session["auth"] = {
|
|
|
|
|
"state": "active",
|
|
|
|
|
"session_state": "debug",
|
|
|
|
|
"code": "abcdefg",
|
|
|
|
|
"preferred_username": "debuguser",
|
|
|
|
|
"debug": True
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def login_required(f):
|
|
|
|
|
@wraps(f)
|
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
|
if 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)
|
|
|
|
@ -38,31 +50,31 @@ def login_required(f):
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
|
def index():
|
|
|
|
|
page = request.args.get('page')
|
|
|
|
|
page = request.args.get("page", "1")
|
|
|
|
|
if page.isdigit() is False:
|
|
|
|
|
flash("Wow, wtf hackerman. Cool it.")
|
|
|
|
|
page = 1
|
|
|
|
|
|
|
|
|
|
memes = Meme.select().order_by(Meme.timestamp).paginate(int(page), 10)
|
|
|
|
|
return render_template("index.html", memes=memes, page=page)
|
|
|
|
|
posts = Post.select().order_by(Post.timestamp).paginate(int(page), 10)
|
|
|
|
|
return render_template("index.html", posts=posts, page=page)
|
|
|
|
|
|
|
|
|
|
@app.route("/meme/<id>")
|
|
|
|
|
def meme(id):
|
|
|
|
|
if Meme.filter(id=id):
|
|
|
|
|
meme = Meme.get(Meme.id == id)
|
|
|
|
|
return render_template("meme.html", meme=meme)
|
|
|
|
|
@app.route("/post/<id>")
|
|
|
|
|
def read_post(id):
|
|
|
|
|
if Post.filter(id=id):
|
|
|
|
|
post = Post.get(Post.id == id)
|
|
|
|
|
return render_template("post.html", post=post)
|
|
|
|
|
else:
|
|
|
|
|
return "no meme there brah"
|
|
|
|
|
|
|
|
|
|
@app.route("/uploads/<path:filename>")
|
|
|
|
|
def uploaded_file(filename):
|
|
|
|
|
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
|
|
|
|
return send_from_directory(app.config["UPLOAD_FOLDER"], filename)
|
|
|
|
|
|
|
|
|
|
@app.route("/submit", methods=["GET", "POST"])
|
|
|
|
|
@app.route("/post/create", methods=["GET", "POST"])
|
|
|
|
|
@login_required
|
|
|
|
|
def submit():
|
|
|
|
|
def create_post():
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
meme_title = request.form.get('title')
|
|
|
|
|
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!")
|
|
|
|
@ -73,21 +85,25 @@ def submit():
|
|
|
|
|
if file.filename == "":
|
|
|
|
|
flash("You didn't upload a caliente meme, bro! You're fuckin up!")
|
|
|
|
|
return redirect(request.url)
|
|
|
|
|
if meme_title is "":
|
|
|
|
|
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 = os.path.join(app.config["UPLOAD_FOLDER"], filename)
|
|
|
|
|
file.save(save_path)
|
|
|
|
|
meme = Meme(
|
|
|
|
|
title=meme_title,
|
|
|
|
|
# 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
|
|
|
|
|
)
|
|
|
|
|
meme.save()
|
|
|
|
|
return redirect(url_for("meme", id=meme.id))
|
|
|
|
|
return render_template("submit.html")
|
|
|
|
|
post.save()
|
|
|
|
|
return redirect(url_for("read_post", id=post.id))
|
|
|
|
|
return render_template("create_post.html")
|
|
|
|
|
|
|
|
|
|
@app.route("/login")
|
|
|
|
|
def login():
|
|
|
|
@ -99,7 +115,11 @@ def login():
|
|
|
|
|
f"response_type=code&" \
|
|
|
|
|
f"state={state}"
|
|
|
|
|
|
|
|
|
|
return redirect(url)
|
|
|
|
|
if app.debug:
|
|
|
|
|
debug_login()
|
|
|
|
|
return redirect(url_for("index"))
|
|
|
|
|
else:
|
|
|
|
|
return redirect(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/auth/")
|
|
|
|
@ -169,9 +189,9 @@ def logout():
|
|
|
|
|
def not_found(error):
|
|
|
|
|
return make_response(jsonify({"error": "Page not found"}), 404)
|
|
|
|
|
|
|
|
|
|
@app.cli.command('dbinit')
|
|
|
|
|
@app.cli.command("dbinit")
|
|
|
|
|
def dbinit():
|
|
|
|
|
db.create_tables([Meme])
|
|
|
|
|
db.create_tables([Post, Profile, Comment, Notification])
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
app.run()
|
|
|
|
|