adding paginated listings on index, improved submission flows, better html templating

This commit is contained in:
lza_menace 2020-07-16 00:41:19 -07:00
parent 95c5882e39
commit d199943b45
4 changed files with 49 additions and 12 deletions

View file

@ -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 jsonify, render_template, flash, session
from flask import send_from_directory, make_response from flask import send_from_directory, make_response
from flask_session import Session from flask_session import Session
from playhouse.flask_utils import PaginatedQuery
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from suchwow.models import Meme, db from suchwow.models import Meme, db
@ -37,13 +38,19 @@ def login_required(f):
@app.route("/") @app.route("/")
def index(): 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/<id>") @app.route("/meme/<id>")
def view(id): def meme(id):
if Meme.filter(id=id): if Meme.filter(id=id):
m = Meme.get(Meme.id == id) meme = Meme.get(Meme.id == id)
return render_template("view.html", meme=m) return render_template("meme.html", meme=meme)
else: else:
return "no meme there brah" return "no meme there brah"
@ -55,27 +62,31 @@ def uploaded_file(filename):
@login_required @login_required
def submit(): def submit():
if request.method == "POST": if request.method == "POST":
meme_title = request.form.get('title')
# check if the post request has the file part # check if the post request has the file part
if "file" not in request.files: 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) return redirect(request.url)
file = request.files["file"] file = request.files["file"]
# if user does not select file, browser also # if user does not select file, browser also
# submit an empty part without filename # submit an empty part without filename
if file.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) return redirect(request.url)
if file and allowed_file(file.filename): if file and allowed_file(file.filename):
filename = secure_filename(file.filename) filename = secure_filename(file.filename)
save_path = os.path.join(app.config["UPLOAD_FOLDER"], filename) save_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
file.save(save_path) file.save(save_path)
meme = Meme( meme = Meme(
title=request.form.get('title'), title=meme_title,
submitter=session["auth"]["preferred_username"], submitter=session["auth"]["preferred_username"],
image_name=filename, image_name=filename,
) )
meme.save() meme.save()
return redirect(url_for("view", id=meme.id)) return redirect(url_for("meme", id=meme.id))
return render_template("submit.html") return render_template("submit.html")
@app.route("/login") @app.route("/login")

View file

@ -3,15 +3,29 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1"> <meta name="viewport" content="width=device-width,initial-scale=1">
<title>Meme Factory</title> <title>SuchWOW!</title>
<meta name="author" content="name"> <meta name="author" content="name">
<meta name="description" content="description here"> <meta name="description" content="description here">
<meta name="keywords" content="keywords,here"> <meta name="keywords" content="keywords,here">
</head> </head>
<body> <body>
<a href="/">Home</a><br> <a href="/">Home</a><br>
<a href="/view">See da meemz</a> {% if session.auth == None %}
<a href="{{ url_for('login') }}">Login</a><br>
{% else %}
<a href="{{ url_for('logout') }}">Logout</a><br>
{% endif %}
<hr> <hr>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<hr>
{% block content %}{% endblock %} {% block content %}{% endblock %}
</body> </body>
</html> </html>

View file

@ -1,7 +1,19 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% block content %}
<a href="{{ url_for('login') }}">Login</a><br>
<a href="{{ url_for('submit') }}">Submit A Meme</a><br> <a href="{{ url_for('submit') }}">Submit A Meme</a><br>
<a href="{{ url_for('debug') }}">Visit the secret page!</a> <a href="{{ url_for('debug') }}">Debug</a>
{% block header %}
<h1>{% block title %}Latest Submissions{% endblock %}</h1>
{% endblock %}
<ul>
{% for meme in memes %}
<li><a href="{{ url_for('meme', id=meme.id) }}">{{ meme.title }}</a></li>
{% endfor %}
</ul>
{{ page }}
{% endblock %} {% endblock %}