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
"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",
},
}

12
db.py
View file

@ -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):

View file

@ -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")