diff --git a/smash/templates/latest.html b/smash/templates/latest.html index 1cf6f03..6f3800d 100644 --- a/smash/templates/latest.html +++ b/smash/templates/latest.html @@ -1,53 +1,55 @@ -{% extends "base.html" %} -{% block content %} - - - -{% if numpages>1 %} - -{% endif %} - -{% if quotes %} - {% for quote in quotes %} - {% if quote.approved %} -
- #{{ quote.id }} - + ({{quote.rating}}) - -
{{ quote.time }}
- - {% if session.authorized %} -
- - -
- {% endif %} -
- - -
-

{{ quote.content|safe }}

-
- -
- - Tags: - {% if quote.tags|length > 0 and quote.tags[0].name|length>0%} - {% for tag in quote.tags %} - {{tag.name}} - {% endfor %} - {% else %} - No tags - {% endif %} - -
- {% endif %} - {% endfor %} -{% endif %} - -{% endblock %} +{% extends "base.html" %} +{% block content %} + + + +{% if numpages>1 %} + +{% endif %} + +{% if quotes %} + {% for quote in quotes %} + {% if quote.approved %} +
+ {% if quote.id %} + #{{ quote.id }} + {% endif %} + + ({{quote.rating}}) - +
{{ quote.time }}
+ + {% if session.authorized and quote.id %} +
+ + +
+ {% endif %} +
+ + +
+

{{ quote.content|safe }}

+
+ +
+ + Tags: + {% if quote.tags|length > 0 and quote.tags[0].name|length>0%} + {% for tag in quote.tags %} + {{tag.name}} + {% endfor %} + {% else %} + No tags + {% endif %} + +
+ {% endif %} + {% endfor %} +{% endif %} + +{% endblock %} diff --git a/smash/views.py b/smash/views.py index 89b75f4..cfc3b4d 100644 --- a/smash/views.py +++ b/smash/views.py @@ -1,16 +1,16 @@ -import datetime -import logging -import psycopg2 -from flask import render_template, Markup, request, abort, session, g - -from smash.models_sqlalchemy import * -from smash import app, conf, db - -logger = logging.getLogger(__name__) - - -def timestamp(): - return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y") +import datetime +import logging +import psycopg2 +from flask import render_template, Markup, request, abort, session, g + +from smash.models_sqlalchemy import * +from smash import app, conf, db + +logger = logging.getLogger(__name__) + + +def timestamp(): + return datetime.datetime.now().strftime("%H:%M:%S %d/%m/%y") def message(level, msg): @@ -24,246 +24,258 @@ def message(level, msg): @app.before_request def before_request(): - g.appname = conf.config['APPNAME'] + g.appname = conf.config['APPNAME'] g.appbrand = conf.config['APPBRAND'] - - -@app.route('/') -def index(): - welcome = conf.config['MOTD'] - news = ("

{}

{} running on smash quote database" - " engine launched today

").format( - datetime.datetime.now().strftime("%d/%m/%y"), - conf.config['APPNAME'] - ) - - return render_template( - "index.html", - title="Quotes", - welcometext=welcome, - newstext=news - ) - - -@app.route('/login', methods=['GET', 'POST']) -def login_page(): - if request.method == 'POST': - if request.form["secret"] == conf.config['ADMINSECRET']: - session['authorized'] = True - - return render_template( - "login.html", - ) - - -@app.route('/latest') -def latest(): - quotes = Quote.query.filter_by(approved=True).order_by(Quote.id.desc()).all() - allquotes = len(quotes) - quotes = quotes[:10] - - if len(quotes)>0: - # Replace line breaks with html breaks and escape special characters - for quote in quotes: - quote.content = str(Markup.escape(quote.content)).replace('\n', '
') - - return render_template( - "latest.html", - title="Latest", - quotes=quotes, - numpages=1 + allquotes//10, - curpage=0, - page_type="latest" - ) - else: - return message("alert-warning", "No quotes in the database.") - - -@app.route('/latest/') -def latest_page(page): - allquotes = len(Quote.query.filter_by(approved=True).order_by(Quote.id.desc()).all()) - quotes = Quote.query.filter_by(approved=True).order_by(Quote.id.desc()).all()[(page-1)*10:page*10] - - for quote in quotes: - quote.content = str(Markup.escape(quote.content)).replace('\n', '
') - - return render_template( - "latest.html", - title="Latest - page {}".format(page), - quotes=quotes, - numpages=1 + allquotes//10, - curpage=page-1, - page_type="latest" - ) - -@app.route('/queue') -def queue(): - if not session.get('authorized'): - return message("alert-danger", "You are not authorized to view this page.") - - quotes = Quote.query.filter_by(approved=False).order_by(Quote.id).all() - - if len(quotes)>0: - # Replace line breaks with html breaks and escape special characters - for quote in quotes: - quote.content = str(Markup.escape(quote.content)).replace('\n', '
') - - return render_template( - "queue.html", - title="Queue", - quotes=quotes - ) - else: - return message("alert-warning", "No quotes in the database.") - - -@app.route('/moderate', methods=['POST']) -def moderate(): - if not session.get('authorized'): - return message("alert-danger", "You are not authorized to perform this action.") - - if request.form['submit'] == "Approve": - quote = Quote.query.filter_by(id=request.form['quoteid']).first() - quote.approved = True - db.session.commit() - - return message("alert-success", "Quote approved.") - - elif request.form['submit'] == "Delete": - quote = Quote.query.filter_by(id=request.form['quoteid']).first() - db.session.delete(quote) - db.session.commit() - - return message("alert-success", "Quote deleted.") - - abort(501) - - -@app.route('/quote/') -def quote(id): - quote = Quote.query.filter_by(id=id, approved=True).first() - - if quote is None: - return render_template( - "message.html", - alertclass="alert-warning", - message="No such quote." - ) - else: - quote.content = str(Markup.escape(quote.content)).replace('\n', '
') - return render_template( - "latest.html", - title="Quote #{}".format(quote.id), - quotes=[quote,], - numpages=1, - curpage=0, - page_type="quote" - ) - - -@app.route('/tag/') -def tag(tagname): - tag = Tag.query.filter_by(name=tagname).first() - - if len(list(tag.quotes))>0: - allquotes = len(list(tag.quotes)) - tag.quotes = tag.quotes[:10] - - # Replace line breaks with html breaks and escape special characters - for quote in tag.quotes: - quote.content = str(Markup.escape(quote.content)).replace('\n', '
') - - return render_template( - "latest.html", - title="Tag - {}".format(tagname), - quotes=tag.quotes, - numpages=1 + allquotes//10, - curpage=0, - page_type="tag/{}".format(tagname) - ) - else: - return message("alert-warning", "No quotes with this tag.") - - -@app.route('/tag//') -def tag_page(tagname, page): - tag = Tag.query.filter_by(name=tagname).first() - - if len(list(tag.quotes))>0: - allquotes = len(list(tag.quotes)) - tag.quotes = tag.quotes[(page-1)*10:page*10] - - for quote in tag.quotes: - quote.content = str(Markup.escape(quote.content)).replace('\n', '
') - - return render_template( - "latest.html", - title="Tag - {} - page {}".format(tagname, page), - quotes=tag.quotes, - numpages=1 + allquotes//10, - curpage=0, - page_type="tag/{}".format(tagname) - ) - - -@app.route('/tags') -def tags(): - tags = Tag.query.order_by(Tag.name).distinct().all() - tags = list(set([x.name for x in tags])) - - return render_template( - "tags.html", - title="Tags", - tags=tags - ) - - -@app.route('/search', methods=['POST']) -def search(): - if request.method == 'POST': - return render_template( - "message.html", - alertclass="alert-warning", - message="Not implemented yet. " - ) - else: - return 'Invalid request.' - - -@app.route('/add', methods=['GET', 'POST']) -def add_new(): - if request.method == 'POST': - if request.form['submit'] == "Submit": - quote_body = request.form["newquote"] - quote_tags = request.form["tags"].split(',') - - quote = Quote(quote_body, request.remote_addr, timestamp()) - quote_tags = [Tag(tag) for tag in quote_tags] - - for tag in quote_tags: - dbtag = Tag.query.filter_by(name=tag.name).first() - if dbtag is not None: - quote.tags.append(dbtag) - else: - quote.tags.append(tag) - #quote.tags.extend(quote_tags) - - db.session.add(quote) - db.session.commit() - - return render_template( - "message.html", - alertclass="alert-success", - message="Quote added succesfully. It will need to be reviewed by the administrators before it shows up." - ) - - elif request.form['submit'] == "Preview": - return str(request.form) - else: - abort(501) - - elif request.method == 'GET': - return render_template( - "add.html", - title="Add new" - ) + + +@app.route('/') +def index(): + welcome = conf.config['MOTD'] + news = ("

{}

{} running on smash quote database" + " engine launched today

").format( + datetime.datetime.now().strftime("%d/%m/%y"), + conf.config['APPNAME'] + ) + + return render_template( + "index.html", + title="Quotes", + welcometext=welcome, + newstext=news + ) + + +@app.route('/login', methods=['GET', 'POST']) +def login_page(): + if request.method == 'POST': + if request.form["secret"] == conf.config['ADMINSECRET']: + session['authorized'] = True + + return render_template( + "login.html", + ) + + +@app.route('/latest') +def latest(): + quotes = Quote.query.filter_by(approved=True).order_by(Quote.id.desc()).all() + allquotes = len(quotes) + quotes = quotes[:10] + + if len(quotes)>0: + # Replace line breaks with html breaks and escape special characters + for quote in quotes: + quote.content = str(Markup.escape(quote.content)).replace('\n', '
') + + return render_template( + "latest.html", + title="Latest", + quotes=quotes, + numpages=1 + allquotes//10, + curpage=0, + page_type="latest" + ) + else: + return message("alert-warning", "No quotes in the database.") + + +@app.route('/latest/') +def latest_page(page): + allquotes = len(Quote.query.filter_by(approved=True).order_by(Quote.id.desc()).all()) + quotes = Quote.query.filter_by(approved=True).order_by(Quote.id.desc()).all()[(page-1)*10:page*10] + + for quote in quotes: + quote.content = str(Markup.escape(quote.content)).replace('\n', '
') + + return render_template( + "latest.html", + title="Latest - page {}".format(page), + quotes=quotes, + numpages=1 + allquotes//10, + curpage=page-1, + page_type="latest" + ) + +@app.route('/queue') +def queue(): + if not session.get('authorized'): + return message("alert-danger", "You are not authorized to view this page.") + + quotes = Quote.query.filter_by(approved=False).order_by(Quote.id).all() + + if len(quotes)>0: + # Replace line breaks with html breaks and escape special characters + for quote in quotes: + quote.content = str(Markup.escape(quote.content)).replace('\n', '
') + + return render_template( + "queue.html", + title="Queue", + quotes=quotes + ) + else: + return message("alert-warning", "No quotes in the database.") + + +@app.route('/moderate', methods=['POST']) +def moderate(): + if not session.get('authorized'): + return message("alert-danger", "You are not authorized to perform this action.") + + if request.form['submit'] == "Approve": + quote = Quote.query.filter_by(id=request.form['quoteid']).first() + quote.approved = True + db.session.commit() + + return message("alert-success", "Quote approved.") + + elif request.form['submit'] == "Delete": + quote = Quote.query.filter_by(id=request.form['quoteid']).first() + db.session.delete(quote) + db.session.commit() + + return message("alert-success", "Quote deleted.") + + abort(501) + + +@app.route('/quote/') +def quote(id): + quote = Quote.query.filter_by(id=id, approved=True).first() + + if quote is None: + return render_template( + "message.html", + alertclass="alert-warning", + message="No such quote." + ) + else: + quote.content = str(Markup.escape(quote.content)).replace('\n', '
') + return render_template( + "latest.html", + title="Quote #{}".format(quote.id), + quotes=[quote,], + numpages=1, + curpage=0, + page_type="quote" + ) + + +@app.route('/tag/') +def tag(tagname): + tag = Tag.query.filter_by(name=tagname).first() + + if len(list(tag.quotes))>0: + allquotes = len(list(tag.quotes)) + tag.quotes = tag.quotes[:10] + + # Replace line breaks with html breaks and escape special characters + for quote in tag.quotes: + quote.content = str(Markup.escape(quote.content)).replace('\n', '
') + + return render_template( + "latest.html", + title="Tag - {}".format(tagname), + quotes=tag.quotes, + numpages=1 + allquotes//10, + curpage=0, + page_type="tag/{}".format(tagname) + ) + else: + return message("alert-warning", "No quotes with this tag.") + + +@app.route('/tag//') +def tag_page(tagname, page): + tag = Tag.query.filter_by(name=tagname).first() + + if len(list(tag.quotes))>0: + allquotes = len(list(tag.quotes)) + tag.quotes = tag.quotes[(page-1)*10:page*10] + + for quote in tag.quotes: + quote.content = str(Markup.escape(quote.content)).replace('\n', '
') + + return render_template( + "latest.html", + title="Tag - {} - page {}".format(tagname, page), + quotes=tag.quotes, + numpages=1 + allquotes//10, + curpage=0, + page_type="tag/{}".format(tagname) + ) + + +@app.route('/tags') +def tags(): + tags = Tag.query.order_by(Tag.name).distinct().all() + tags = list(set([x.name for x in tags])) + + return render_template( + "tags.html", + title="Tags", + tags=tags + ) + + +@app.route('/search', methods=['POST']) +def search(): + if request.method == 'POST': + return render_template( + "message.html", + alertclass="alert-warning", + message="Not implemented yet. " + ) + else: + return 'Invalid request.' + + +@app.route('/add', methods=['GET', 'POST']) +def add_new(): + if request.method == 'POST': + if request.form['submit'] == "Submit": + quote_body = request.form["newquote"] + quote_tags = request.form["tags"].split(',') + + quote = Quote(quote_body, request.remote_addr, timestamp()) + quote_tags = [Tag(tag) for tag in quote_tags] + + for tag in quote_tags: + dbtag = Tag.query.filter_by(name=tag.name).first() + if dbtag is not None: + quote.tags.append(dbtag) + else: + quote.tags.append(tag) + #quote.tags.extend(quote_tags) + + db.session.add(quote) + db.session.commit() + + return render_template( + "message.html", + alertclass="alert-success", + message="Quote added succesfully. It will need to be reviewed by the administrators before it shows up." + ) + + elif request.form['submit'] == "Preview": + preview = Quote(request.form['newquote'], request.remote_addr, timestamp()) + preview_tags = request.form["tags"].split(',') + preview.approved = True + preview.tags = [Tag(tag) for tag in preview_tags] + + return render_template( + "latest.html", + title="Quote preview", + quotes=[preview,], + numpages=1, + curpage=0, + page_type="quote" + ) + else: + abort(501) + + elif request.method == 'GET': + return render_template( + "add.html", + title="Add new" + )