diff --git a/config.py b/config.py index 6f9c16a..93ef9fb 100644 --- a/config.py +++ b/config.py @@ -8,9 +8,21 @@ config = { # Show random quote on homepage "random-quote": True, + # Length of nanoid quote IDs + "quote-id-length": 12, + # Used to protect against session data tampering "secret-key": "This should be a complex random value", # For approving or removing quotes "administrator-password": "This should be a different complex random value", + + # Database connection info + "db": { + "server": "mongodb://localhost:27017/", + + "database": "smash", + + "collection": "quotes", + }, } diff --git a/db.py b/db.py index 8e8d877..8026c5c 100644 --- a/db.py +++ b/db.py @@ -1,8 +1,8 @@ import pymongo, nanoid -connection = pymongo.MongoClient() -db = connection.smash -qdb = db.quotes -adb = db.accounts +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 })) @@ -26,7 +26,7 @@ def get_quote_by_id(quote_id): def add_quote(quote, tags, author): qdb.insert_one({ - "id": nanoid.generate(size=12), + "id": nanoid.generate(size=config['quote-id-length']), "quote": quote, "tags": tags, "author": author, @@ -37,7 +37,7 @@ def add_quote(quote, tags, author): def get_latest_quotes(page=1): return list(qdb \ .find({ "hidden": False, "approved": True }) \ - .sort( "_id", 1 ) \ + .sort( "_id", -1 ) \ .limit(page*10)[(page-1)*10:]) def get_live_quotes_by_tag(tag): diff --git a/main.py b/main.py index f22ebcd..ad3f6d8 100644 --- a/main.py +++ b/main.py @@ -6,11 +6,6 @@ from config import config app = Flask(__name__) app.secret_key = config['secret-key'] -#Connect to and define db -connection = pymongo.MongoClient() -real_db = connection.testdb -qdb = real_db.quotes - def timestamp(): return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y")