From d199943b45814843a670bc6c8f9fa864c5393c51 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Thu, 16 Jul 2020 00:41:19 -0700 Subject: [PATCH] adding paginated listings on index, improved submission flows, better html templating --- suchwow/app.py | 27 +++++++++++++++------- suchwow/templates/base.html | 18 +++++++++++++-- suchwow/templates/index.html | 16 +++++++++++-- suchwow/templates/{view.html => meme.html} | 0 4 files changed, 49 insertions(+), 12 deletions(-) rename suchwow/templates/{view.html => meme.html} (100%) diff --git a/suchwow/app.py b/suchwow/app.py index 15d8da1..57b05e0 100644 --- a/suchwow/app.py +++ b/suchwow/app.py @@ -7,6 +7,7 @@ from flask import Flask, g, 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 @@ -37,13 +38,19 @@ def login_required(f): @app.route("/") def index(): - return render_template("index.html") + page = request.args.get('page') + 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) @app.route("/meme/") -def view(id): +def meme(id): if Meme.filter(id=id): - m = Meme.get(Meme.id == id) - return render_template("view.html", meme=m) + meme = Meme.get(Meme.id == id) + return render_template("meme.html", meme=meme) else: return "no meme there brah" @@ -55,27 +62,31 @@ def uploaded_file(filename): @login_required def submit(): if request.method == "POST": + meme_title = request.form.get('title') # check if the post request has the file part if "file" not in request.files: - flash("No file part") + 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("No selected file") + flash("You didn't upload a caliente meme, bro! You're fuckin up!") + return redirect(request.url) + if meme_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=request.form.get('title'), + title=meme_title, submitter=session["auth"]["preferred_username"], image_name=filename, ) meme.save() - return redirect(url_for("view", id=meme.id)) + return redirect(url_for("meme", id=meme.id)) return render_template("submit.html") @app.route("/login") diff --git a/suchwow/templates/base.html b/suchwow/templates/base.html index 7380181..d9dadf4 100644 --- a/suchwow/templates/base.html +++ b/suchwow/templates/base.html @@ -3,15 +3,29 @@ - Meme Factory + SuchWOW! Home
- See da meemz + {% if session.auth == None %} + Login
+ {% else %} + Logout
+ {% endif %}
+ {% with messages = get_flashed_messages() %} + {% if messages %} + + {% endif %} + {% endwith %} +
{% block content %}{% endblock %} diff --git a/suchwow/templates/index.html b/suchwow/templates/index.html index 6903dc8..c10fe1b 100644 --- a/suchwow/templates/index.html +++ b/suchwow/templates/index.html @@ -1,7 +1,19 @@ {% extends 'base.html' %} {% block content %} - Login
+ Submit A Meme
- Visit the secret page! + Debug + + {% block header %} +

{% block title %}Latest Submissions{% endblock %}

+ {% endblock %} + + + +{{ page }} {% endblock %} diff --git a/suchwow/templates/view.html b/suchwow/templates/meme.html similarity index 100% rename from suchwow/templates/view.html rename to suchwow/templates/meme.html