From b594bec6431087e164dd01e762eb4664f56c65f8 Mon Sep 17 00:00:00 2001 From: Kate Date: Sun, 15 Nov 2020 10:35:06 +0000 Subject: [PATCH] Created usable quote IDs, and working latest quotes page --- Pipfile | 1 + Pipfile.lock | 10 +++++++++- db.py | 28 +++++++++++++++++++++------- main.py | 24 ++++++++++++++++-------- templates/index.html | 2 +- templates/latest.html | 30 ++++++++++++++++++++++++++++++ templates/layout.html | 17 ++++++++++------- 7 files changed, 88 insertions(+), 24 deletions(-) diff --git a/Pipfile b/Pipfile index 1d0a061..c873545 100644 --- a/Pipfile +++ b/Pipfile @@ -6,6 +6,7 @@ name = "pypi" [packages] flask = "*" pymongo = "*" +nanoid = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 27b14cf..842cd0d 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "1e6dcb2055e48235dc444cfc753bcacfcf9395313c7e6c95327e169d78010881" + "sha256": "21a18c278c3945c189875ebf5a376dec92fefbece3a19a7038e41584e4d8d301" }, "pipfile-spec": 6, "requires": { @@ -87,6 +87,14 @@ "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==1.1.1" }, + "nanoid": { + "hashes": [ + "sha256:5a80cad5e9c6e9ae3a41fa2fb34ae189f7cb420b2a5d8f82bd9d23466e4efa68", + "sha256:90aefa650e328cffb0893bbd4c236cfd44c48bc1f2d0b525ecc53c3187b653bb" + ], + "index": "pypi", + "version": "==2.0.0" + }, "pymongo": { "hashes": [ "sha256:03dc64a9aa7a5d405aea5c56db95835f6a2fa31b3502c5af1760e0e99210be30", diff --git a/db.py b/db.py index 889df7d..263d530 100644 --- a/db.py +++ b/db.py @@ -1,21 +1,35 @@ -import pymongo +import pymongo, nanoid connection = pymongo.MongoClient() db = connection.smash qdb = db.quotes adb = db.accounts +live_quotes_count = lambda: qdb.find({ "hidden": False, "approved": True }) + def get_random_quote(): - x = dict( qdb.aggregate([ - { "$match": { "hidden": False } }, + + #TODO: there might be a better way to get a random document + x = list(qdb.aggregate([ + { "$match": { "hidden": False, "approved": True } }, { "$sample": { "size": 1 } } - ]) ) - print(x) - return(x) + ]))[0] + + return(x if x else False) def add_quote(quote, tags, author): qdb.insert_one({ + "id": nanoid.generate(size=12), "quote": quote, "tags": tags, - "author": author + "author": author, + "hidden": False, + "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:]) diff --git a/main.py b/main.py index a2c289b..e65481b 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,14 @@ import datetime, random, json, pymongo from flask import Flask, render_template, Markup, request, abort, session, g -import db as dblib +import db #from smash import app, conf app = Flask(__name__) #Connect to and define db connection = pymongo.MongoClient() -db = connection.testdb -qdb = db.quotes +real_db = connection.testdb +qdb = real_db.quotes def timestamp(): return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y") @@ -26,13 +26,13 @@ def index(): news = "No quotes yet!" #welcome = conf.config['MOTD'] welcome = "MOTD" - print(qdb.find().count()) + #print(qdb.find().count()) qCount = qdb.find({"hidden": False}).count() - print(type(qCount)) - news = "Home of " + str(qCount) + " dumb quotes!" + #print(type(qCount)) + news = "Home of " + "5" + " dumb quotes!" if qCount > 0: - rand_quote = dblib.get_random_quote() - quote_text = Markup.escape(rand_quote['quote'])#.replace('\n', '
') + rand_quote = db.get_random_quote() + quote_text = Markup.escape(rand_quote['quote']) if rand_quote else "There are no quotes in the database!" news = quote_text permalink = str(rand_quote['id']) @@ -44,6 +44,14 @@ def index(): permalink=permalink ) +@app.route('/latest') +def latest(): + return render_template( + "latest.html", + title="Latest", + quotes=db.get_latest_quotes() + ) + if __name__ == "__main__": app.run(host="0.0.0.0", debug=True) diff --git a/templates/index.html b/templates/index.html index e0023d2..ef724ee 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,7 +9,7 @@

{{ title }}

{{ welcometext }}

{{ newstext|safe }}
- Permalink + Permalink {% endblock %} diff --git a/templates/latest.html b/templates/latest.html index e69de29..88a0bdc 100644 --- a/templates/latest.html +++ b/templates/latest.html @@ -0,0 +1,30 @@ +{% extends "layout.html" %} +{% block customhead %} + {{ title }} +{% endblock %} +{% block nav_latest %}active{% endblock %} +{% block content %} + +
+

Latest Quotes

+ + {% for quote in quotes %} +
+
+

Quote {{quote.id}}

+ + + + + +
+ +
{{ quote.quote }}
+ +

Tags: {% for tag in quote.tags %}{{tag}}{% if not loop.last %}, {% endif %}{% endfor %}

+ +
+ {% endfor %} +
+ +{% endblock %} diff --git a/templates/layout.html b/templates/layout.html index 8353c39..fc696b4 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -1,11 +1,14 @@ - + - - {% block customhead %} - {% endblock %} + + + + + {% block customhead %} + {% endblock %}