From e682dbbddafa1f9231c706af67d696354db66990 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 30 Jun 2014 02:54:19 -0400 Subject: [PATCH] Move application logic around; improve exceptions. --- app.fcgi | 24 +++- copyvios/settings.py | 10 +- templates/debug.mako | 14 +-- templates/error.mako | 6 + templates/index.mako | 294 ++++++++++++++++++++++++------------------------ templates/settings.mako | 209 +++++++++++++++++----------------- 6 files changed, 284 insertions(+), 273 deletions(-) create mode 100644 templates/error.mako diff --git a/app.fcgi b/app.fcgi index a80f455..f7703a1 100755 --- a/app.fcgi +++ b/app.fcgi @@ -5,12 +5,16 @@ from functools import wraps from logging import DEBUG from logging.handlers import TimedRotatingFileHandler from time import asctime +from traceback import format_exc from flask import Flask, g, request from flask.ext.mako import MakoTemplates, render_template, TemplateError from flup.server.fcgi import WSGIServer from copyvios.cookies import parse_cookies +from copyvios.misc import get_bot +from copyvios.settings import process_settings +from copyvios.sites import get_sites app = Flask(__name__) MakoTemplates(app) @@ -20,13 +24,15 @@ app.logger.addHandler(TimedRotatingFileHandler( "logs/app.log", when="D", interval=1, backupCount=7)) app.logger.info(u"Flask server started " + asctime()) -def debug_exceptions(func): +def catch_errors(func): @wraps(func) def inner(*args, **kwargs): try: return func(*args, **kwargs) except TemplateError as exc: - return "
" + exc.text + "
" + return render_template("error.mako", traceback=exc.text) + except Exception: + return render_template("error.mako", traceback=format_exc()) return inner @app.before_request @@ -50,17 +56,23 @@ def write_access_log(response): return response @app.route("/") -@debug_exceptions +@catch_errors def index(): return render_template("index.mako") @app.route("/settings", methods=["GET", "POST"]) -@debug_exceptions +@catch_errors def settings(): - return render_template("settings.mako") + status = process_settings() if request.method == "POST" else None + bot = get_bot() + langs, projects = get_sites(bot) + default = bot.wiki.get_site() + kwargs = {"status": status, "langs": langs, "projects": projects, + "default_lang": default.lang, "default_project": default.project} + return render_template("settings.mako", **kwargs) @app.route("/debug") -@debug_exceptions +@catch_errors def debug(): return render_template("debug.mako") diff --git a/copyvios/settings.py b/copyvios/settings.py index 4009cdd..0ab3b4c 100644 --- a/copyvios/settings.py +++ b/copyvios/settings.py @@ -4,10 +4,9 @@ from flask import g from markupsafe import escape from .cookies import set_cookie, delete_cookie -from .misc import get_bot, Query -from .sites import get_sites +from .misc import Query -def main(): +def process_settings(): query = Query(method="POST") if query.action == "set": status = _do_set(query) @@ -15,10 +14,7 @@ def main(): status = _do_delete(query) else: status = None - - bot = get_bot() - langs, projects = get_sites(bot) - return bot, status, langs, projects + return status def _do_set(query): cookies = g.cookies diff --git a/templates/debug.mako b/templates/debug.mako index 5c43e61..7332663 100644 --- a/templates/debug.mako +++ b/templates/debug.mako @@ -1,10 +1,10 @@ <%include file="/support/header.mako" args="title='Debug - Earwig\'s Copyvio Detector'"/> <%! from flask import request %>\ - + <%include file="/support/footer.mako"/> diff --git a/templates/error.mako b/templates/error.mako new file mode 100644 index 0000000..3b0cdf2 --- /dev/null +++ b/templates/error.mako @@ -0,0 +1,6 @@ +<%include file="/support/header.mako" args="title='Error! - Earwig\'s Copyvio Detector'"/> +
+

An error occured! If it hasn't been reported (try to check), please file an issue or email me. Include the following information:

+
{{traceback | h}}
+
+<%include file="/support/footer.mako"/> diff --git a/templates/index.mako b/templates/index.mako index 920c10a..33e1176 100644 --- a/templates/index.mako +++ b/templates/index.mako @@ -7,155 +7,155 @@ lang, orig_lang, title, oldid, url, nocache = query.lang, query.orig_lang, query.title, query.oldid, query.url, query.nocache bot, site, page, result = query.bot, query.site, query.page, query.result %>\ - % if query.project and lang and (title or oldid): - % if not site: -
-

The given site (project=${query.project | h}, language=${lang | h}) doesn't seem to exist. It may also be closed or private. Confirm its URL.

-
- % elif title and not result: -
-

The given page doesn't seem to exist: ${page.title | h}.

-
- % elif oldid and not result: -
-

The given revision ID doesn't seem to exist: ${oldid | h}.

-
- % elif url and result == "bad URI": - <% result = None %> -
-

Unsupported URI scheme: ${url | h}.

-
- % endif - %endif -

This tool attempts to detect copyright violations in articles. Simply give the title of the page or ID of the revision you want to check and hit Submit. The tool will search for similar content elsewhere on the web and display a report if a match is found. If you also provide a URL, it will not query any search engines and instead display a report comparing the article to that particular webpage, like the Duplication Detector. Check out the FAQ for more information and technical details.

-

Note: The tool is still in beta. You are completely welcome to use it and provide feedback, but be aware that it may produce strange or broken results.

-
- - - - - - - - - - - - - - - - % if nocache or (result and result.cached): - - - - - % endif - - - -
Site: - http:// - - . - - .org -
Page title: - % if page: - - % elif title: - - % else: - - % endif - or revision ID: - % if oldid: - - % else: - - % endif -
URL (optional): - % if url: - - % else: - - % endif -
Bypass cache: - % if nocache: - - % else: - - % endif -
- -
-
- % if result: - <% show_details = "CopyviosShowDetails" in cookies and cookies["CopyviosShowDetails"].value == "True" %> -
-
- % if result.violation: -

${page.title | h} is a suspected violation of ${result.url | urlstrip, h}.

- % else: -

No violations detected in ${page.title | h}.

- % endif -