Added database and quote id length to config file

This commit is contained in:
Kate 2020-11-21 05:52:21 +00:00
parent 0febd5b6a3
commit 4b703c4bd7
3 changed files with 18 additions and 11 deletions

View file

@ -8,9 +8,21 @@ config = {
# Show random quote on homepage # Show random quote on homepage
"random-quote": True, "random-quote": True,
# Length of nanoid quote IDs
"quote-id-length": 12,
# Used to protect against session data tampering # Used to protect against session data tampering
"secret-key": "This should be a complex random value", "secret-key": "This should be a complex random value",
# For approving or removing quotes # For approving or removing quotes
"administrator-password": "This should be a different complex random value", "administrator-password": "This should be a different complex random value",
# Database connection info
"db": {
"server": "mongodb://localhost:27017/",
"database": "smash",
"collection": "quotes",
},
} }

12
db.py
View file

@ -1,8 +1,8 @@
import pymongo, nanoid import pymongo, nanoid
connection = pymongo.MongoClient() from config import config
db = connection.smash connection = pymongo.MongoClient(config['db']['server'])
qdb = db.quotes db = connection[config['db']['database']]
adb = db.accounts qdb = db[config['db']['collection']]
count_live_quotes = lambda: qdb.find({ "hidden": False, "approved": True }).count() count_live_quotes = lambda: qdb.find({ "hidden": False, "approved": True }).count()
quote_live = lambda quote_id: bool(qdb.find_one({ "hidden": False, "approved": True })) 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): def add_quote(quote, tags, author):
qdb.insert_one({ qdb.insert_one({
"id": nanoid.generate(size=12), "id": nanoid.generate(size=config['quote-id-length']),
"quote": quote, "quote": quote,
"tags": tags, "tags": tags,
"author": author, "author": author,
@ -37,7 +37,7 @@ def add_quote(quote, tags, author):
def get_latest_quotes(page=1): def get_latest_quotes(page=1):
return list(qdb \ return list(qdb \
.find({ "hidden": False, "approved": True }) \ .find({ "hidden": False, "approved": True }) \
.sort( "_id", 1 ) \ .sort( "_id", -1 ) \
.limit(page*10)[(page-1)*10:]) .limit(page*10)[(page-1)*10:])
def get_live_quotes_by_tag(tag): def get_live_quotes_by_tag(tag):

View file

@ -6,11 +6,6 @@ from config import config
app = Flask(__name__) app = Flask(__name__)
app.secret_key = config['secret-key'] app.secret_key = config['secret-key']
#Connect to and define db
connection = pymongo.MongoClient()
real_db = connection.testdb
qdb = real_db.quotes
def timestamp(): def timestamp():
return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y") return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y")