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
|
import pymongo, nanoid
|
||||||
from config import config
|
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()
|
connection = pymongo.MongoClient(config["db"]["server"])
|
||||||
quote_live = lambda quote_id: bool(qdb.find_one({ "hidden": False, "approved": True }))
|
db = connection[config["db"]["database"]]
|
||||||
tag_live = lambda tag: bool(qdb.find_one({ "hidden": False, "approved": True, "tags": tag }))
|
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():
|
def get_random_quote():
|
||||||
|
|
||||||
#TODO: there might be a better way to get a random document
|
# TODO: there might be a better way to get a random document
|
||||||
x = list(qdb.aggregate([
|
x = list(
|
||||||
{ "$match": { "hidden": False, "approved": True } },
|
qdb.aggregate(
|
||||||
{ "$sample": { "size": 1 } }
|
[{"$match": {"hidden": False, "approved": True}}, {"$sample": {"size": 1}}]
|
||||||
]))[0]
|
)
|
||||||
|
)[0]
|
||||||
|
|
||||||
|
return x if x else False
|
||||||
|
|
||||||
return(x if x else False)
|
|
||||||
|
|
||||||
def get_quote_by_id(quote_id):
|
def get_quote_by_id(quote_id):
|
||||||
if quote_live(quote_id):
|
if quote_live(quote_id):
|
||||||
return qdb.find_one({ "id": quote_id })
|
return qdb.find_one({"id": quote_id})
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def add_quote(quote, tags, author):
|
def add_quote(quote, tags, author):
|
||||||
qdb.insert_one({
|
quote_id = nanoid.generate(size=config["quote-id-length"])
|
||||||
"id": nanoid.generate(size=config['quote-id-length']),
|
qdb.insert_one(
|
||||||
|
{
|
||||||
|
"id": quote_id,
|
||||||
"quote": quote,
|
"quote": quote,
|
||||||
"tags": tags,
|
"tags": tags,
|
||||||
"author": author,
|
"author": author,
|
||||||
"hidden": False,
|
"hidden": False,
|
||||||
"approved": False
|
"approved": False,
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
return quote_id
|
||||||
|
|
||||||
|
|
||||||
def get_latest_quotes(page=1):
|
def get_latest_quotes(page=1):
|
||||||
return list(qdb \
|
return list(
|
||||||
.find({ "hidden": False, "approved": True }) \
|
qdb.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):
|
||||||
return list(qdb \
|
return list(
|
||||||
.find({ "hidden": False, "approved": True, "tags": tag}) \
|
qdb.find({"hidden": False, "approved": True, "tags": tag}).sort("_id", 1)
|
||||||
.sort( "_id", 1 ))
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_all_tags():
|
def get_all_tags():
|
||||||
return qdb \
|
return qdb.find({"hidden": False, "approved": True}).distinct("tags")
|
||||||
.find({ "hidden": False, "approved": True}) \
|
|
||||||
.distinct("tags")
|
|
||||||
|
|
||||||
def count_live_quotes_by_tag():
|
def count_live_quotes_by_tag():
|
||||||
quotes = {}
|
quotes = {}
|
||||||
|
|
85
main.py
85
main.py
|
@ -4,71 +4,69 @@ import db
|
||||||
from config import config
|
from config import config
|
||||||
|
|
||||||
app = Flask(__name__)
|
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):
|
timestamp = lambda: datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y")
|
||||||
return render_template(
|
|
||||||
"message.html",
|
|
||||||
alertclass=level,
|
|
||||||
message=msg,
|
|
||||||
title="Message"
|
|
||||||
)
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
|
@app.context_processor
|
||||||
|
def inject_config():
|
||||||
|
return {"site_name": config["site-name"]}
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
news = "No quotes yet!"
|
news = "No quotes yet!"
|
||||||
welcome = config['MOTD']
|
welcome = config["MOTD"]
|
||||||
quotes_count = db.count_live_quotes()
|
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()
|
random_quote = db.get_random_quote()
|
||||||
news = Markup.escape(random_quote['quote'])
|
news = Markup.escape(random_quote["quote"])
|
||||||
permalink = str(random_quote['id'])
|
permalink = str(random_quote["id"])
|
||||||
elif ( quotes_count > 0 ):
|
elif quotes_count > 0:
|
||||||
news = "Home of " + str(quotes_count) + " quotes!"
|
news = "Home of " + str(quotes_count) + " quotes!"
|
||||||
permalink = None
|
permalink = None
|
||||||
else:
|
else:
|
||||||
news = "There are no quotes in the database!"
|
news = "There are no quotes in the database!"
|
||||||
permalink = None
|
permalink = None
|
||||||
|
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
"index.html",
|
"index.html",
|
||||||
title="Quotes - " + config["site-name"],
|
title="Quotes",
|
||||||
header=welcome,
|
header=welcome,
|
||||||
newstext=news,
|
newstext=news,
|
||||||
permalink=permalink
|
permalink=permalink,
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.route('/latest')
|
|
||||||
|
@app.route("/latest")
|
||||||
def latest():
|
def latest():
|
||||||
return render_template(
|
return render_template(
|
||||||
"list.html",
|
"list.html",
|
||||||
title="Latest",
|
title="Latest",
|
||||||
header="Latest Quotes",
|
header="Latest Quotes",
|
||||||
quotes=db.get_latest_quotes()
|
latest=True,
|
||||||
|
quotes=db.get_latest_quotes(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/tags')
|
@app.route("/tags")
|
||||||
def tags():
|
def tags():
|
||||||
return render_template(
|
return render_template(
|
||||||
"tags.html",
|
"tags.html", title="Tags", tags=db.count_live_quotes_by_tag()
|
||||||
title="Tags",
|
|
||||||
tags=db.count_live_quotes_by_tag()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.route('/tags/<t>')
|
|
||||||
|
@app.route("/tags/<t>")
|
||||||
def tag(t):
|
def tag(t):
|
||||||
if db.tag_live(t):
|
if db.tag_live(t):
|
||||||
return render_template(
|
return render_template(
|
||||||
"list.html",
|
"list.html",
|
||||||
title=t,
|
title=t,
|
||||||
header="Quotes matching: " + t,
|
header="Quotes matching: " + t,
|
||||||
quotes=db.get_live_quotes_by_tag(t)
|
quotes=db.get_live_quotes_by_tag(t),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return render_template(
|
return render_template(
|
||||||
|
@ -77,18 +75,39 @@ def tag(t):
|
||||||
message={
|
message={
|
||||||
"type": "danger",
|
"type": "danger",
|
||||||
"heading": "No matching quotes",
|
"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):
|
def quote(quote_id):
|
||||||
return render_template(
|
return render_template(
|
||||||
"quote.html",
|
"quote.html", title="Quote " + quote_id, quote=db.get_quote_by_id(quote_id)
|
||||||
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__":
|
if __name__ == "__main__":
|
||||||
app.run(host="0.0.0.0", debug=True)
|
app.run(host="0.0.0.0", debug=True)
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
|
<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">
|
<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>
|
<span class="navbar-toggler-icon"></span>
|
||||||
</button>
|
</button>
|
||||||
|
@ -28,7 +28,7 @@
|
||||||
<a class="nav-link" href="/tags">Tags</a>
|
<a class="nav-link" href="/tags">Tags</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item {% block nav_new_post %}{% endblock %}">
|
<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>
|
||||||
<li class="nav-item {% block nav_administration_login %}{% endblock %}">
|
<li class="nav-item {% block nav_administration_login %}{% endblock %}">
|
||||||
<a class="nav-link" href="#">Administration Login</a>
|
<a class="nav-link" href="#">Administration Login</a>
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
{% block customhead %}
|
{% block customhead %}
|
||||||
<title>{{ title }}</title>
|
<title>{{ title }}</title>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block nav_latest %}active{% endblock %}
|
{% block nav_latest %}{% if latest %}active{% endif %}{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
<div class="container">
|
<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