Add dynamic sorting
This commit is contained in:
parent
d2b227035b
commit
780d74fe89
186
app/app.py
186
app/app.py
|
|
@ -11,55 +11,6 @@ import time
|
|||
|
||||
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('/')
|
||||
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/<path:subreddit>')
|
||||
|
|
@ -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/<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):
|
||||
select = """
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
@ -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 %}
|
||||
</div>
|
||||
<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 %}
|
||||
<div class="post">
|
||||
<h3>{{ post.title }}</h3>
|
||||
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue