tags page first draft
This commit is contained in:
parent
d3536b929f
commit
478e9dba1b
4 changed files with 48 additions and 3 deletions
17
db.py
17
db.py
|
@ -16,7 +16,6 @@ def get_random_quote():
|
|||
|
||||
return(x if x else False)
|
||||
|
||||
|
||||
def add_quote(quote, tags, author):
|
||||
qdb.insert_one({
|
||||
"id": nanoid.generate(size=12),
|
||||
|
@ -27,9 +26,23 @@ def add_quote(quote, tags, author):
|
|||
"approved": False
|
||||
})
|
||||
|
||||
|
||||
def get_latest_quotes(page=1):
|
||||
return list(qdb \
|
||||
.find({ "hidden": False, "approved": True }) \
|
||||
.sort( "_id", 1 ) \
|
||||
.limit(page*10)[(page-1)*10:])
|
||||
|
||||
def get_live_quotes_by_tag(tag):
|
||||
return list(qdb \
|
||||
.find({ "hidden": False, "approved": True, "tags": tag}))
|
||||
|
||||
def get_all_tags():
|
||||
return qdb \
|
||||
.find({ "hidden": False, "approved": True}) \
|
||||
.distinct("tags")
|
||||
|
||||
def count_live_quotes_by_tag():
|
||||
quotes = {}
|
||||
for tag in get_all_tags():
|
||||
quotes[tag] = len(get_live_quotes_by_tag(tag))
|
||||
return quotes
|
||||
|
|
8
main.py
8
main.py
|
@ -53,5 +53,13 @@ def latest():
|
|||
)
|
||||
|
||||
|
||||
@app.route('/tags')
|
||||
def tags():
|
||||
return render_template(
|
||||
"tags.html",
|
||||
title="Tags",
|
||||
tags=db.count_live_quotes_by_tag()
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", debug=True)
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
<a class="nav-link" href="/latest">Latest</a>
|
||||
</li>
|
||||
<li class="nav-item {% block nav_tags %}{% endblock %}">
|
||||
<a class="nav-link" href="#">Tags</a>
|
||||
<a class="nav-link" href="/tags">Tags</a>
|
||||
</li>
|
||||
<li class="nav-item {% block nav_new_post %}{% endblock %}">
|
||||
<a class="nav-link" href="#">New Post</a>
|
||||
|
|
24
templates/tags.html
Normal file
24
templates/tags.html
Normal file
|
@ -0,0 +1,24 @@
|
|||
{% extends "layout.html" %}
|
||||
{% block customhead %}
|
||||
<title>{{ title }}</title>
|
||||
{% endblock %}
|
||||
{% block nav_tags %}active{% endblock %}
|
||||
{% block content %}
|
||||
|
||||
<div class = "container">
|
||||
<h1 class="display-4 mb-5">Tags</h1>
|
||||
|
||||
<div class="col-12 col-md-5">
|
||||
<ul class="list-group">
|
||||
{% for tag,count in tags.items() %}
|
||||
<li class="list-group-item d-flex justify-content-between align-items-center">
|
||||
{{ tag }}
|
||||
<span class="badge badge-primary badge-pill">{{ count }}</span>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
Loading…
Add table
Reference in a new issue