浏览代码

Massively update cookie handling.

pull/24/head
Ben Kurtovic 12 年前
父节点
当前提交
3e934fbaef
共有 9 个文件被更改,包括 108 次插入37 次删除
  1. +6
    -4
      build.py
  2. +2
    -1
      pages/copyvios.mako
  3. +21
    -19
      pages/settings.mako
  4. +2
    -1
      static/js/copyvios.js
  5. +42
    -0
      toolserver/cookies.py
  6. +2
    -2
      toolserver/copyvios/__init__.py
  7. +2
    -9
      toolserver/misc.py
  8. +30
    -0
      toolserver/settings.py
  9. +1
    -1
      toolserver/sites.py

+ 6
- 4
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}(\?.*?)?$


+ 2
- 1
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) %>
<h1>Copyvio Detector</h1>


+ 21
- 19
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) %>
<h1>Settings</h1>
<p>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.</p>
<h2>Options</h2>
<table>
<tr>
<form action="${environ['PATH_INFO']}" method="post">
<input type="hidden" name="action" value="setDefaultSite">
<form action="${environ['PATH_INFO']}" method="post">
<input type="hidden" name="action" value="set">
<table>
<tr>
<td>Default site:</td>
<td>
<tt>http://</tt>
<select name="lang">
<% selected_lang = cookies["EarwigDefaultLang"].value if "EarwigDefaultLang" in cookies else bot.wiki.get_site().lang %>
% for code, name in langs:
% if "EarwigDefaultLang" in cookies and code == cookies["EarwigDefaultLang"].value:
% if code == selected_lang:
<option value="${code}" selected="selected">${name}</option>
% else:
<option value="${code}">${name}</option>
@@ -24,8 +23,9 @@
</select>
<tt>.</tt>
<select name="project">
<% selected_project = cookies["EarwigDefaultProject"].value if "EarwigDefaultProject" in cookies else bot.wiki.get_site().lang %>
% for code, name in projects:
% if "EarwigDefaultProject" in cookies and code == cookies["EarwigDefaultProject"].value:
% if code == selected_project:
<option value="${code}" selected="selected">${name}</option>
% else:
<option value="${code}">${name}</option>
@@ -34,21 +34,23 @@
</select>
<tt>.org</tt>
</td>
</tr>
<tr>
<td>Background:</td>
</tr>
<tr>
<td><button type="submit">Save</button></td>
</form>
</tr>
<tr>
<td>Background:</td>
</tr>
</table>
</tr>
</table>
</form>
<h2>Cookies</h2>
% if cookies:
<ul>
% for cookie in cookies.itervalues():
<li>
<tt>${cookie.key | h}</tt>: <tt>${cookie.value | h}</tt>
<tt>${cookie.key | h}</tt>: <tt>${cookie.value | h}</tt> (<tt>${cookie.path | h}</tt>)
<form action="${environ['PATH_INFO']}" method="post">
<input type="hidden" name="action" value="deleteCookie">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="cookie" value="${cookie.key | h}">
<button type="submit">Delete</button>
</form>
@@ -56,7 +58,7 @@
% endfor
</ul>
<form action="${environ['PATH_INFO']}" method="post">
<input type="hidden" name="action" value="deleteCookie">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="all" value="1">
<button type="submit">Delete all</button>
</form>


+ 2
- 1
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:";


+ 42
- 0
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)

+ 2
- 2
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)


+ 2
- 9
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

+ 30
- 0
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

+ 1
- 1
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 = ?"


正在加载...
取消
保存