Add dynamic sorting

This commit is contained in:
John Stephani 2026-01-02 02:18:09 -06:00
parent d2b227035b
commit 780d74fe89
3 changed files with 176 additions and 65 deletions

View File

@ -11,55 +11,6 @@ import time
app = Flask(__name__) app = Flask(__name__)
@app.route('/file/<path:filename>')
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/<path:permalink>')
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/<path:permalink>')
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('/') @app.route('/')
def index(): def index():
connection = sqlite3.connect(config.db_file) connection = sqlite3.connect(config.db_file)
@ -166,7 +117,8 @@ def front_page():
connection = sqlite3.connect(config.db_file) connection = sqlite3.connect(config.db_file)
cursor = connection.cursor() cursor = connection.cursor()
sidebar_links = get_sidebar_links(cursor) sidebar_links = get_sidebar_links(cursor)
select = """ sort = get_config("sort") or "created_utc asc"
select = f"""
SELECT SELECT
post post
FROM FROM
@ -175,13 +127,18 @@ def front_page():
hidden = ? AND hidden = ? AND
saved = ? saved = ?
ORDER BY ORDER BY
created_utc asc {sort}
LIMIT ? LIMIT ?
""" """
binds = [False, False, config.posts_per_page_load] binds = [False, False, config.posts_per_page_load]
posts = get_posts_from_select(cursor, select, binds) posts = get_posts_from_select(cursor, select, binds)
connection.close() 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') @app.route('/r/other')
def other_page(): def other_page():
@ -189,7 +146,8 @@ def other_page():
connection = sqlite3.connect(config.db_file) connection = sqlite3.connect(config.db_file)
cursor = connection.cursor() cursor = connection.cursor()
sidebar_links = get_sidebar_links(cursor) sidebar_links = get_sidebar_links(cursor)
select = """ sort = get_config("sort") or "created_utc asc"
select = f"""
SELECT SELECT
post post
FROM FROM
@ -218,13 +176,18 @@ def other_page():
count <= ? count <= ?
) )
ORDER BY ORDER BY
created_utc asc {sort}
LIMIT ? LIMIT ?
""" """
binds = [False, False, False, False, config.other_posts_cutoff, config.posts_per_page_load] binds = [False, False, False, False, config.other_posts_cutoff, config.posts_per_page_load]
posts = get_posts_from_select(cursor, select, binds) posts = get_posts_from_select(cursor, select, binds)
connection.close() 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') @app.route('/r/saved')
def get_saved(): def get_saved():
@ -232,7 +195,8 @@ def get_saved():
connection = sqlite3.connect(config.db_file) connection = sqlite3.connect(config.db_file)
cursor = connection.cursor() cursor = connection.cursor()
sidebar_links = get_sidebar_links(cursor) sidebar_links = get_sidebar_links(cursor)
select = """ sort = get_config("sort") or "created_utc desc"
select = f"""
SELECT SELECT
post post
FROM FROM
@ -240,13 +204,19 @@ def get_saved():
WHERE WHERE
saved = ? saved = ?
ORDER BY ORDER BY
created_utc desc {sort}
LIMIT ? LIMIT ?
""" """
binds = [True, config.posts_per_page_load] binds = [True, config.posts_per_page_load]
posts = get_posts_from_select(cursor, select, binds) posts = get_posts_from_select(cursor, select, binds)
connection.close() 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/<path:subreddit>') @app.route('/r/<path:subreddit>')
@ -255,7 +225,8 @@ def get_subreddit(subreddit):
connection = sqlite3.connect(config.db_file) connection = sqlite3.connect(config.db_file)
cursor = connection.cursor() cursor = connection.cursor()
sidebar_links = get_sidebar_links(cursor) sidebar_links = get_sidebar_links(cursor)
select = """ sort = get_config("sort") or "created_utc asc"
select = f"""
SELECT SELECT
post post
FROM FROM
@ -265,13 +236,106 @@ def get_subreddit(subreddit):
hidden = ? AND hidden = ? AND
saved = ? saved = ?
ORDER BY ORDER BY
created_utc asc {sort}
LIMIT ? LIMIT ?
""" """
binds = [subreddit, False, False, config.posts_per_page_load] binds = [subreddit, False, False, config.posts_per_page_load]
posts = get_posts_from_select(cursor, select, binds) posts = get_posts_from_select(cursor, select, binds)
connection.close() 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/<path:filename>')
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/<path:permalink>')
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/<path:permalink>')
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): def get_sidebar_links(cursor):
select = """ select = """

View File

@ -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 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 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 block(name primary key)")
cursor.execute("CREATE TABLE IF NOT EXISTS config(key primary key, value)")
connection.commit() connection.commit()
connection.close() connection.close()

View File

@ -96,6 +96,13 @@
flex-direction: column; flex-direction: column;
} }
.header-container {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin: 0px 10px;
}
.media-div, .text-content, .button-wrapper { .media-div, .text-content, .button-wrapper {
padding: 0; padding: 0;
margin: 15px 0 0 0; margin: 15px 0 0 0;
@ -115,7 +122,7 @@
/* desktop */ /* desktop */
@media (min-aspect-ratio: 1) { @media (min-aspect-ratio: 1) {
div.post { div.post, .header-container {
width: 70vw; width: 70vw;
} }
@ -136,7 +143,7 @@
/* phone */ /* phone */
@media (max-aspect-ratio: 1) { @media (max-aspect-ratio: 1) {
div.post { div.post, .header-container {
width: calc(100vw - 40px); width: calc(100vw - 40px);
} }
@ -152,7 +159,6 @@
overflow-x: auto; overflow-x: auto;
align-items: center; align-items: center;
padding: 5px, 0px; padding: 5px, 0px;
margin-bottom: 10px;
} }
.content { .content {
@ -272,7 +278,13 @@
{% endfor %} {% endfor %}
</div> </div>
<div class="content"> <div class="content">
<h1>{{ title }}</h1> <div class="header-container">
<h3>{{ title }}</h3>
<h3>
<span onclick="changeSortKey()">{{ sort[0] }}</span>
<span onclick="changeSortDir()">{{ sort[1] }}</span>
</h3>
</div>
{% for post in posts %} {% for post in posts %}
<div class="post"> <div class="post">
<h3>{{ post.title }}</h3> <h3>{{ post.title }}</h3>
@ -558,6 +570,40 @@
setTimeout(() => {window.location.href = window.location.href;}, 1000); 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);
});
}
</script> </script>
</body> </body>