From 530916664ad62959a8c75640a46785e41bf013b5 Mon Sep 17 00:00:00 2001 From: Kate Date: Sat, 21 Nov 2020 18:44:08 +0000 Subject: [PATCH] Added new quote submission --- db.py | 76 ++++++++++++++++++++++---------------- main.py | 85 ++++++++++++++++++++++++++----------------- templates/layout.html | 4 +- templates/list.html | 2 +- templates/new.html | 34 +++++++++++++++++ 5 files changed, 134 insertions(+), 67 deletions(-) create mode 100644 templates/new.html diff --git a/db.py b/db.py index 8026c5c..f43e187 100644 --- a/db.py +++ b/db.py @@ -1,54 +1,68 @@ import pymongo, nanoid from config import config -connection = pymongo.MongoClient(config['db']['server']) -db = connection[config['db']['database']] -qdb = db[config['db']['collection']] -count_live_quotes = lambda: qdb.find({ "hidden": False, "approved": True }).count() -quote_live = lambda quote_id: bool(qdb.find_one({ "hidden": False, "approved": True })) -tag_live = lambda tag: bool(qdb.find_one({ "hidden": False, "approved": True, "tags": tag })) +connection = pymongo.MongoClient(config["db"]["server"]) +db = connection[config["db"]["database"]] +qdb = db[config["db"]["collection"]] + +count_live_quotes = lambda: qdb.find({"hidden": False, "approved": True}).count() +quote_live = lambda quote_id: bool(qdb.find_one({"hidden": False, "approved": True})) +tag_live = lambda tag: bool( + qdb.find_one({"hidden": False, "approved": True, "tags": tag}) +) + def get_random_quote(): - #TODO: there might be a better way to get a random document - x = list(qdb.aggregate([ - { "$match": { "hidden": False, "approved": True } }, - { "$sample": { "size": 1 } } - ]))[0] + # TODO: there might be a better way to get a random document + x = list( + qdb.aggregate( + [{"$match": {"hidden": False, "approved": True}}, {"$sample": {"size": 1}}] + ) + )[0] + + return x if x else False - return(x if x else False) def get_quote_by_id(quote_id): if quote_live(quote_id): - return qdb.find_one({ "id": quote_id }) + return qdb.find_one({"id": quote_id}) else: return False + def add_quote(quote, tags, author): - qdb.insert_one({ - "id": nanoid.generate(size=config['quote-id-length']), - "quote": quote, - "tags": tags, - "author": author, - "hidden": False, - "approved": False - }) + quote_id = nanoid.generate(size=config["quote-id-length"]) + qdb.insert_one( + { + "id": quote_id, + "quote": quote, + "tags": tags, + "author": author, + "hidden": False, + "approved": False, + } + ) + return quote_id + def get_latest_quotes(page=1): - return list(qdb \ - .find({ "hidden": False, "approved": True }) \ - .sort( "_id", -1 ) \ - .limit(page*10)[(page-1)*10:]) + 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}) \ - .sort( "_id", 1 )) + return list( + qdb.find({"hidden": False, "approved": True, "tags": tag}).sort("_id", 1) + ) + def get_all_tags(): - return qdb \ - .find({ "hidden": False, "approved": True}) \ - .distinct("tags") + return qdb.find({"hidden": False, "approved": True}).distinct("tags") + def count_live_quotes_by_tag(): quotes = {} diff --git a/main.py b/main.py index ad3f6d8..3494bae 100644 --- a/main.py +++ b/main.py @@ -4,71 +4,69 @@ import db from config import config app = Flask(__name__) -app.secret_key = config['secret-key'] +app.secret_key = config["secret-key"] -def timestamp(): - return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y") -def message(level, msg): - return render_template( - "message.html", - alertclass=level, - message=msg, - title="Message" - ) +timestamp = lambda: datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y") -@app.route('/') + +@app.context_processor +def inject_config(): + return {"site_name": config["site-name"]} + + +@app.route("/") def index(): news = "No quotes yet!" - welcome = config['MOTD'] + welcome = config["MOTD"] quotes_count = db.count_live_quotes() - if ( quotes_count > 0 ) and ( config['random-quote'] ): + if (quotes_count > 0) and (config["random-quote"]): random_quote = db.get_random_quote() - news = Markup.escape(random_quote['quote']) - permalink = str(random_quote['id']) - elif ( quotes_count > 0 ): + news = Markup.escape(random_quote["quote"]) + permalink = str(random_quote["id"]) + elif quotes_count > 0: news = "Home of " + str(quotes_count) + " quotes!" permalink = None else: news = "There are no quotes in the database!" permalink = None - return render_template( "index.html", - title="Quotes - " + config["site-name"], + title="Quotes", header=welcome, newstext=news, - permalink=permalink + permalink=permalink, ) -@app.route('/latest') + +@app.route("/latest") def latest(): return render_template( "list.html", title="Latest", header="Latest Quotes", - quotes=db.get_latest_quotes() + latest=True, + quotes=db.get_latest_quotes(), ) -@app.route('/tags') +@app.route("/tags") def tags(): return render_template( - "tags.html", - title="Tags", - tags=db.count_live_quotes_by_tag() + "tags.html", title="Tags", tags=db.count_live_quotes_by_tag() ) -@app.route('/tags/') + +@app.route("/tags/") def tag(t): if db.tag_live(t): return render_template( "list.html", title=t, header="Quotes matching: " + t, - quotes=db.get_live_quotes_by_tag(t) + quotes=db.get_live_quotes_by_tag(t), ) else: return render_template( @@ -77,18 +75,39 @@ def tag(t): message={ "type": "danger", "heading": "No matching quotes", - "message": f"There are no quotes with the tag \"{t}\" in the database" - } + "message": f'There are no quotes with the tag "{t}" in the database', + }, ) -@app.route('/quote/') +@app.route("/quote/") def quote(quote_id): return render_template( - "quote.html", - title="Quote " + quote_id, - quote= db.get_quote_by_id(quote_id) + "quote.html", title="Quote " + quote_id, quote=db.get_quote_by_id(quote_id) ) + +@app.route("/new", methods=["GET", "POST"]) +def new_quote(): + if request.method == "GET": + return render_template( + "new.html", + title="New Quote", + ) + elif request.method == "POST": + author = request.form["author"] + quote = request.form["quote"] + tags_list = [i.strip() for i in request.form["tags"].split(",")] + db.add_quote(quote, tags_list, author) + return render_template( + "message.html", + message={ + "type": "success", + "heading": "Thanks for being awesome!", + "message": "Your quote is currently awaiting approval from a site administrator", + }, + ) + + if __name__ == "__main__": app.run(host="0.0.0.0", debug=True) diff --git a/templates/layout.html b/templates/layout.html index b5f9a38..80d5db3 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,7 +12,7 @@