From 780d74fe896c75ef7ec2429fc6eb52ddbf5feddd Mon Sep 17 00:00:00 2001 From: John Stephani Date: Fri, 2 Jan 2026 02:18:09 -0600 Subject: [PATCH] Add dynamic sorting --- app/app.py | 186 ++++++++++++++++++++++++++------------- app/make_db.py | 1 + app/templates/index.html | 54 +++++++++++- 3 files changed, 176 insertions(+), 65 deletions(-) diff --git a/app/app.py b/app/app.py index f58b30b..80a70e7 100755 --- a/app/app.py +++ b/app/app.py @@ -11,55 +11,6 @@ import time app = Flask(__name__) -@app.route('/file/') -def serve_file(filename): - try: - return send_file('{0}/{1}'.format(config.media_dir, filename), as_attachment=False) - except FileNotFoundError: - return "File not found", 404 - -@app.route('/hide/') -def hide_post(permalink): - if permalink[0] != "/": - permalink = "/" + permalink - connection = sqlite3.connect(config.db_file) - cursor = connection.cursor() - update = """ - UPDATE - post - SET - hidden = ? - WHERE - permalink = ? AND - saved = ? - """ - binds = [True, permalink, False] - cursor.execute(update,binds) - connection.commit() - connection.close() - return "" - -@app.route('/save/') -def save_post(permalink): - if permalink[0] != "/": - permalink = "/" + permalink - connection = sqlite3.connect(config.db_file) - cursor = connection.cursor() - action = request.args.get("action") - update = """ - UPDATE - post - SET - saved = ? - WHERE - permalink = ? - """ - binds = [(action == "save"), permalink] - cursor.execute(update,binds) - connection.commit() - connection.close() - return "" - @app.route('/') def index(): connection = sqlite3.connect(config.db_file) @@ -166,7 +117,8 @@ def front_page(): connection = sqlite3.connect(config.db_file) cursor = connection.cursor() sidebar_links = get_sidebar_links(cursor) - select = """ + sort = get_config("sort") or "created_utc asc" + select = f""" SELECT post FROM @@ -175,13 +127,18 @@ def front_page(): hidden = ? AND saved = ? ORDER BY - created_utc asc + {sort} LIMIT ? """ binds = [False, False, config.posts_per_page_load] posts = get_posts_from_select(cursor, select, binds) connection.close() - return render_template('index.html', title=title, posts=posts, sidebar_links=sidebar_links) + return render_template( + 'index.html', + title=title, + posts=posts, + sidebar_links=sidebar_links, + sort=sort.split()) @app.route('/r/other') def other_page(): @@ -189,7 +146,8 @@ def other_page(): connection = sqlite3.connect(config.db_file) cursor = connection.cursor() sidebar_links = get_sidebar_links(cursor) - select = """ + sort = get_config("sort") or "created_utc asc" + select = f""" SELECT post FROM @@ -218,13 +176,18 @@ def other_page(): count <= ? ) ORDER BY - created_utc asc + {sort} LIMIT ? """ binds = [False, False, False, False, config.other_posts_cutoff, config.posts_per_page_load] posts = get_posts_from_select(cursor, select, binds) connection.close() - return render_template('index.html', title=title, posts=posts, sidebar_links=sidebar_links) + return render_template( + 'index.html', + title=title, + posts=posts, + sidebar_links=sidebar_links, + sort=sort.split()) @app.route('/r/saved') def get_saved(): @@ -232,7 +195,8 @@ def get_saved(): connection = sqlite3.connect(config.db_file) cursor = connection.cursor() sidebar_links = get_sidebar_links(cursor) - select = """ + sort = get_config("sort") or "created_utc desc" + select = f""" SELECT post FROM @@ -240,13 +204,19 @@ def get_saved(): WHERE saved = ? ORDER BY - created_utc desc + {sort} LIMIT ? """ binds = [True, config.posts_per_page_load] posts = get_posts_from_select(cursor, select, binds) connection.close() - return render_template('index.html', title=title, posts=posts, sidebar_links=sidebar_links, saved=True) + return render_template( + 'index.html', + title=title, + posts=posts, + sidebar_links=sidebar_links, + saved=True, + sort=sort.split()) @app.route('/r/') @@ -255,7 +225,8 @@ def get_subreddit(subreddit): connection = sqlite3.connect(config.db_file) cursor = connection.cursor() sidebar_links = get_sidebar_links(cursor) - select = """ + sort = get_config("sort") or "created_utc asc" + select = f""" SELECT post FROM @@ -265,13 +236,106 @@ def get_subreddit(subreddit): hidden = ? AND saved = ? ORDER BY - created_utc asc + {sort} LIMIT ? """ binds = [subreddit, False, False, config.posts_per_page_load] posts = get_posts_from_select(cursor, select, binds) connection.close() - return render_template('index.html', title=title, posts=posts, sidebar_links=sidebar_links) + return render_template( + 'index.html', + title=title, + posts=posts, + sidebar_links=sidebar_links, + sort=sort.split()) + +@app.route('/file/') +def serve_file(filename): + try: + return send_file('{0}/{1}'.format(config.media_dir, filename), as_attachment=False) + except FileNotFoundError: + return "File not found", 404 + +@app.route('/hide/') +def hide_post(permalink): + if permalink[0] != "/": + permalink = "/" + permalink + connection = sqlite3.connect(config.db_file) + cursor = connection.cursor() + update = """ + UPDATE + post + SET + hidden = ? + WHERE + permalink = ? AND + saved = ? + """ + binds = [True, permalink, False] + cursor.execute(update,binds) + connection.commit() + connection.close() + return "" + +@app.route('/save/') +def save_post(permalink): + if permalink[0] != "/": + permalink = "/" + permalink + connection = sqlite3.connect(config.db_file) + cursor = connection.cursor() + action = request.args.get("action") + update = """ + UPDATE + post + SET + saved = ? + WHERE + permalink = ? + """ + binds = [(action == "save"), permalink] + cursor.execute(update,binds) + connection.commit() + connection.close() + return "" + +@app.route('/config', methods=['POST']) +def save_config(): + connection = sqlite3.connect(config.db_file) + cursor = connection.cursor() + key = request.args.get("key") + value = request.args.get("value") + insert = f""" + INSERT INTO + config (key, value) + VALUES + (?, ?) + ON CONFLICT + (key) + DO UPDATE SET + value=excluded.value + """ + binds = [key, value] + cursor.execute(insert, binds) + connection.commit() + connection.close() + return "" + +def get_config(key): + connection = sqlite3.connect(config.db_file) + cursor = connection.cursor() + select = """ + SELECT + value + FROM + config + WHERE + key = ? + """ + binds = [key] + result = cursor.execute(select, binds).fetchone() + if result is None: + return None + return result[0] def get_sidebar_links(cursor): select = """ diff --git a/app/make_db.py b/app/make_db.py index 21aecbd..354b6ae 100755 --- a/app/make_db.py +++ b/app/make_db.py @@ -10,5 +10,6 @@ cursor.execute("CREATE TABLE IF NOT EXISTS post(permalink primary key, subreddit cursor.execute("CREATE TABLE IF NOT EXISTS media(permalink, url , local, PRIMARY KEY (permalink, url))") cursor.execute("CREATE TABLE IF NOT EXISTS subreddit(subreddit primary key, minimum_score, fetch_by, fetch_max)") cursor.execute("CREATE TABLE IF NOT EXISTS block(name primary key)") +cursor.execute("CREATE TABLE IF NOT EXISTS config(key primary key, value)") connection.commit() connection.close() \ No newline at end of file diff --git a/app/templates/index.html b/app/templates/index.html index e3751b6..ca7f5fb 100755 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -96,6 +96,13 @@ flex-direction: column; } + .header-container { + display: flex; + justify-content: space-between; + align-items: flex-start; + margin: 0px 10px; + } + .media-div, .text-content, .button-wrapper { padding: 0; margin: 15px 0 0 0; @@ -115,7 +122,7 @@ /* desktop */ @media (min-aspect-ratio: 1) { - div.post { + div.post, .header-container { width: 70vw; } @@ -136,7 +143,7 @@ /* phone */ @media (max-aspect-ratio: 1) { - div.post { + div.post, .header-container { width: calc(100vw - 40px); } @@ -152,7 +159,6 @@ overflow-x: auto; align-items: center; padding: 5px, 0px; - margin-bottom: 10px; } .content { @@ -272,7 +278,13 @@ {% endfor %}
-

{{ title }}

+
+

{{ title }}

+

+ {{ sort[0] }} + {{ sort[1] }} +

+
{% for post in posts %}

{{ post.title }}

@@ -558,6 +570,40 @@ setTimeout(() => {window.location.href = window.location.href;}, 1000); } } + + function changeSortKey() { + newSort = "created_utc " + "{{ sort[1] }}"; + if ("{{ sort[0] }}" == "created_utc"){ + newSort = "score " + "{{ sort[1] }}"; + } + params = { + key: "sort", + value: newSort + }; + params = new URLSearchParams(params).toString() + fetch("/config?" + params, { + method: 'POST' + }).then(() => { + setTimeout(() => {window.location.href = window.location.href;}, 1000); + }); + } + + function changeSortDir() { + newSort = "{{ sort[0] }}" + " asc"; + if ("{{ sort[1] }}" == "asc"){ + newSort = "{{ sort[0] }}" + " desc"; + } + params = { + key: "sort", + value: newSort + }; + params = new URLSearchParams(params).toString() + fetch("/config?" + params, { + method: 'POST' + }).then(() => { + setTimeout(() => {window.location.href = window.location.href;}, 1000); + }); + }