diff --git a/db.py b/db.py index 263d530..5ac45d2 100644 --- a/db.py +++ b/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 diff --git a/main.py b/main.py index e65481b..9b05e3a 100644 --- a/main.py +++ b/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) diff --git a/templates/layout.html b/templates/layout.html index fc696b4..b5f9a38 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -25,7 +25,7 @@ Latest
  • New Post diff --git a/templates/tags.html b/templates/tags.html new file mode 100644 index 0000000..937a290 --- /dev/null +++ b/templates/tags.html @@ -0,0 +1,24 @@ +{% extends "layout.html" %} +{% block customhead %} + {{ title }} +{% endblock %} +{% block nav_tags %}active{% endblock %} +{% block content %} + +
    +

    Tags

    + +
    +
      + {% for tag,count in tags.items() %} +
    • + {{ tag }} + {{ count }} +
    • + {% endfor %} +
    +
    + +
    + +{% endblock %}