diff --git a/build.py b/build.py index 3e2dd1b..6853983 100755 --- a/build.py +++ b/build.py @@ -16,17 +16,19 @@ sys.path.insert(0, ".") from mako.template import Template from mako.lookup import TemplateLookup -def myapp(environ, start_response): - start_response("200 OK", [("Content-Type", "text/html")]) +def main(environ, start_response): lookup = TemplateLookup(directories=["{{pages_dir}}"], input_encoding="utf8") template = Template(filename="{{src}}", module_directory="{{temp_dir}}", lookup=lookup, format_exceptions=True) - return [template.render(environ=environ).encode("utf8")] + headers = [("Content-Type", "text/html")] + page = template.render(environ=environ, headers=headers).encode("utf8") + start_response("200 OK", headers) + return [page] if __name__ == "__main__": from flup.server.fcgi import WSGIServer - WSGIServer(myapp).run() + WSGIServer(main).run() """ rewrite_script_src = """match URL into $ with ^/~earwig/{0}(\?.*?)?$ diff --git a/pages/copyvios.mako b/pages/copyvios.mako index b6f73cd..c2426a7 100644 --- a/pages/copyvios.mako +++ b/pages/copyvios.mako @@ -1,6 +1,7 @@ <%include file="/support/header.mako" args="environ=environ, title='Copyvio Detector', add_css=('copyvios.css',), add_js=('copyvios.js',)"/>\ <%namespace module="toolserver.copyvios" import="main, highlight_delta"/>\ -<%namespace module="toolserver.misc" import="parse_cookies, urlstrip"/>\ +<%namespace module="toolserver.cookies" import="parse_cookies"/>\ +<%namespace module="toolserver.misc" import="urlstrip"/>\ <% query, bot, all_langs, all_projects, page, result = main(environ) %> <% cookies = parse_cookies(environ) %>

Copyvio Detector

diff --git a/pages/settings.mako b/pages/settings.mako index 0cf673b..478c0f4 100644 --- a/pages/settings.mako +++ b/pages/settings.mako @@ -1,21 +1,20 @@ <%include file="/support/header.mako" args="environ=environ, title='Settings'"/>\ -<%namespace module="toolserver.misc" import="get_bot, parse_cookies"/>\ -<%namespace module="toolserver.sites" import="get_sites"/>\ -<% cookies = parse_cookies(environ) %> -<% langs, projects = get_sites(get_bot()) %> +<%namespace module="toolserver.settings" import="main"/>\ +<% bot, cookies, langs, projects = main(environ, headers) %>

Settings

This page contains some configurable options for this Toolserver site. Settings are saved as cookies. You can view and delete all cookies generated by this site at the bottom of this page.

Options

- - - - + + +
+ + + + + + - - - - - -
Default site: http:// . .org
Background:
Background:
+ + +

Cookies

% if cookies:
- +
diff --git a/static/js/copyvios.js b/static/js/copyvios.js index 7a77aae..901e486 100644 --- a/static/js/copyvios.js +++ b/static/js/copyvios.js @@ -16,6 +16,7 @@ function get_cookie(name) { } function set_cookie(name, value, days) { + value = window.btoa(value); var path = window.location.pathname.split("/", 2)[1]; if (days) { var date = new Date(); @@ -39,7 +40,7 @@ function copyvio_toggle_details(details) { if (link.innerHTML == "Show details:") { details.style.display = "block"; link.innerHTML = "Hide details:"; - set_cookie("EarwigCVShowDetails", "True", 180); + set_cookie("EarwigCVShowDetails", "True", 365); } else { details.style.display = "none"; link.innerHTML = "Show details:"; diff --git a/toolserver/cookies.py b/toolserver/cookies.py new file mode 100644 index 0000000..ba7e1e1 --- /dev/null +++ b/toolserver/cookies.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +import base64 +from Cookie import BaseCookie +from datetime import datetime, timedelta +from os import path + +class _CookieManager(BaseCookie): + def __init__(self, environ): + self._path = path.split(environ["PATH_INFO"])[0] + try: + self.load(environ["HTTP_COOKIE"]) + except AttributeError: + pass + + def value_decode(self, value): + try: + return base64.b64decode(value).decode("utf8") + except (TypeError, UnicodeDecodeError): + return u"False" + + def value_encode(self, value): + return base64.b64encode(value.encode("utf8")) + + @property + def path(self): + return self._path + + +def parse_cookies(context, environ): + return _CookieManager(environ) + +def set_cookie(headers, cookies, key, value, days=0): + cookies[key] = value + if days: + expires = datetime.utcnow() + timedelta(days=days) + cookies[key]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") + cookies[key]["path"] = cookies.path + headers.append(("Set-Cookie", cookies[key].OutputString())) + +def delete_cookie(headers, cookies, key): + set_cookie(headers, cookies, key, "", days=-1) diff --git a/toolserver/copyvios/__init__.py b/toolserver/copyvios/__init__.py index effecba..4baecbf 100644 --- a/toolserver/copyvios/__init__.py +++ b/toolserver/copyvios/__init__.py @@ -14,8 +14,8 @@ def main(context, environ): if query.project: query.project = query.project.lower() - bot = get_bot(context) - all_langs, all_projects = get_sites(context, bot) + bot = get_bot() + all_langs, all_projects = get_sites(bot) page = result = None if query.lang and query.project and query.title: site = get_site(bot, query, all_projects) diff --git a/toolserver/misc.py b/toolserver/misc.py index c546bd6..9f65e65 100644 --- a/toolserver/misc.py +++ b/toolserver/misc.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -from Cookie import CookieError, SimpleCookie from os.path import expanduser from urlparse import parse_qs @@ -48,11 +47,5 @@ def urlstrip(context, url): url = url[:-1] return url -def get_bot(context): - return Bot(".earwigbot") - -def parse_cookies(context, environ): - try: - return SimpleCookie(environ["HTTP_COOKIE"]) - except CookieError: - return SimpleCookie() +def get_bot(): + return Bot(".earwigbot", 100) # Don't print any logs to the console diff --git a/toolserver/settings.py b/toolserver/settings.py new file mode 100644 index 0000000..b86e458 --- /dev/null +++ b/toolserver/settings.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +from ..cookies import parse_cookies, set_cookie +from ..misc import get_bot, Query +from ..sites import get_sites + +def main(context, environ, headers): + cookies = parse_cookies(context, environ) + query = Query(environ) + + if query.action == "set": + if query.lang: + key = "EarwigDefaultLang" + if key not in cookies or cookies[key].value != query.lang: + set_cookie(headers, cookies, key, query.lang, 365) + if query.project: + key = "EarwigDefaultProject" + if key not in cookies or cookies[key].value != query.project: + set_cookie(headers, cookies, key, query.project, 365) + elif query.action == "delete": + if query.cookie in cookies: + delete_cookie(headers, cookies, query.cookie) + elif query.all: + for cookie in cookies.values: + if cookie.path.startswith(cookies.path): + delete_cookie(headers, cookies, cookie.key) + + bot = get_bot() + langs, projects = get_sites(bot) + return bot, cookies, langs, projects diff --git a/toolserver/sites.py b/toolserver/sites.py index bc496fc..e0260aa 100644 --- a/toolserver/sites.py +++ b/toolserver/sites.py @@ -27,7 +27,7 @@ def get_site(bot, query, all_projects): except (exceptions.APIError, exceptions.LoginError): return None -def get_sites(context, bot): +def get_sites(bot): max_staleness = 60 * 60 * 24 * 7 conn = open_sql_connection(bot, "globals") query1 = "SELECT update_time FROM updates WHERE update_service = ?"