Added new quote submission
This commit is contained in:
parent
4b703c4bd7
commit
530916664a
5 changed files with 134 additions and 67 deletions
68
db.py
68
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_id = nanoid.generate(size=config["quote-id-length"])
|
||||
qdb.insert_one(
|
||||
{
|
||||
"id": quote_id,
|
||||
"quote": quote,
|
||||
"tags": tags,
|
||||
"author": author,
|
||||
"hidden": False,
|
||||
"approved": 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 = {}
|
||||
|
|
85
main.py
85
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/<t>')
|
||||
|
||||
@app.route("/tags/<t>")
|
||||
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/<quote_id>')
|
||||
@app.route("/quote/<quote_id>")
|
||||
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)
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
|
||||
<a class="navbar-brand" href="#">Shit Haplo Says</a>
|
||||
<a class="navbar-brand" href="#">{{ site_name }}</a>
|
||||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
|
@ -28,7 +28,7 @@
|
|||
<a class="nav-link" href="/tags">Tags</a>
|
||||
</li>
|
||||
<li class="nav-item {% block nav_new_post %}{% endblock %}">
|
||||
<a class="nav-link" href="#">New Post</a>
|
||||
<a class="nav-link" href="/new">New Post</a>
|
||||
</li>
|
||||
<li class="nav-item {% block nav_administration_login %}{% endblock %}">
|
||||
<a class="nav-link" href="#">Administration Login</a>
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
{% block customhead %}
|
||||
<title>{{ title }}</title>
|
||||
{% endblock %}
|
||||
{% block nav_latest %}active{% endblock %}
|
||||
{% block nav_latest %}{% if latest %}active{% endif %}{% endblock %}
|
||||
{% block content %}
|
||||
|
||||
<div class="container">
|
||||
|
|
34
templates/new.html
Normal file
34
templates/new.html
Normal file
|
@ -0,0 +1,34 @@
|
|||
{% extends "layout.html" %}
|
||||
{% block customhead %}
|
||||
<title>{{ title }}</title>
|
||||
{% endblock %}
|
||||
{% block nav_new_post %}active{% endblock %}
|
||||
{% block content %}
|
||||
|
||||
<div class="container">
|
||||
<form method="POST" action="/new">
|
||||
<div class="form-group">
|
||||
<div class="input-group mb-2">
|
||||
<div class="input-group-prepend">
|
||||
<div class="input-group-text"><i class='fa fa-fw fa-user'></i></div>
|
||||
</div>
|
||||
<input type="text" class="form-control" name="author" placeholder="Your Name">
|
||||
</div>
|
||||
<div class="input-group mb-2">
|
||||
<div class="input-group-prepend">
|
||||
<div class="input-group-text"><i class='fa fa-fw fa-quote-left'></i></div>
|
||||
</div>
|
||||
<textarea class="form-control" name="quote" rows="5" placeholder="Quote goes here"></textarea>
|
||||
</div>
|
||||
<div class="input-group mb-2">
|
||||
<div class="input-group-prepend">
|
||||
<div class="input-group-text"><i class='fa fa-fw fa-tags'></i></div>
|
||||
</div>
|
||||
<input type="text" class="form-control" name="tags" placeholder="Comma separated tags">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
Loading…
Add table
Reference in a new issue