move code around™

pull/16/head
Martin Zimmermann 11 years ago
parent cb20f159fb
commit 2d075f867a

@ -45,12 +45,17 @@ from werkzeug.exceptions import HTTPException, NotFound, InternalServerError
from werkzeug.wsgi import SharedDataMiddleware
from werkzeug.serving import run_simple
from isso import comment, db, utils, migrate
from jinja2 import Environment, FileSystemLoader
from isso import db, utils, migrate
from isso.views import comment, admin
url_map = Map([
Rule('/', methods=['HEAD', 'GET'], endpoint='comment.get'),
Rule('/', methods=['PUT', 'DELETE'], endpoint='comment.modify'),
Rule('/new', methods=['POST'], endpoint='comment.create'),
Rule('/', methods=['HEAD', 'GET'], endpoint=views.comment.get),
Rule('/', methods=['PUT', 'DELETE'], endpoint=views.comment.modify),
Rule('/new', methods=['POST'], endpoint=views.comment.create),
Rule('/admin/', endpoint=views.admin.index)
])
@ -67,6 +72,7 @@ class Isso(object):
self.db = db.SQLite(dbpath, moderation=False)
self.signer = URLSafeTimedSerializer(secret)
self.j2env = Environment(loader=FileSystemLoader(join(dirname(__file__), 'templates/')))
def sign(self, obj):
return self.signer.dumps(obj)
@ -79,6 +85,10 @@ class Isso(object):
| misaka.EXT_SUPERSCRIPT | misaka.EXT_AUTOLINK \
| misaka.HTML_SKIP_HTML | misaka.HTML_SKIP_IMAGES | misaka.HTML_SAFELINK)
def render(self, tt, **ctx):
tt = self.j2env.get_template(tt)
return tt.render(**ctx)
@classmethod
def dumps(cls, obj, **kw):
return json.dumps(obj, cls=utils.IssoEncoder, **kw)
@ -86,12 +96,7 @@ class Isso(object):
def dispatch(self, request, start_response):
adapter = url_map.bind_to_environ(request.environ)
try:
endpoint, values = adapter.match()
if hasattr(endpoint, '__call__'):
handler = endpoint
else:
module, function = endpoint.split('.', 1)
handler = getattr(globals()[module], function)
handler, values = adapter.match()
return handler(self, request.environ, request, **values)
except NotFound as e:
return Response('Not Found', 404)
@ -118,6 +123,8 @@ def main():
help="database location"),
make_option("--base-url", dest="base_url", default="http://localhost:8080/",
help="set base url for comments"),
make_option("--secret-key", dest="secret", default=None,
help="fixed secret key (admin auth etc.)"),
make_option("--max-age", dest="max_age", default=15*60, type=int,
help="..."),
@ -134,7 +141,7 @@ def main():
print('isso', dist.version)
sys.exit(0)
isso = Isso(dbpath=options.dbpath, secret=utils.mksecret(12),
isso = Isso(dbpath=options.dbpath, secret=options.secret or utils.mksecret(12),
base_url=options.base_url, max_age=options.max_age)
if len(args) > 0 and args[0] == 'import':

@ -1,34 +0,0 @@
# -*- encoding: utf-8 -*-
#
# Copyright 2012, Martin Zimmermann <info@posativ.org>. All rights reserved.
# License: BSD Style, 2 clauses. see isso/__init__.py
from os.path import join, dirname
from mako.lookup import TemplateLookup
from itsdangerous import SignatureExpired, BadSignature
from isso.wsgi import setcookie
mako = TemplateLookup(directories=[join(dirname(__file__), 'templates')], input_encoding='utf-8')
render = lambda template, **context: mako.get_template(template).render_unicode(**context)
def index(app, environ, request):
if request.method == 'POST':
if request.form.getfirst('secret') == app.SECRET:
return 301, '', {
'Location': '/admin/',
'Set-Cookie': setcookie('admin', app.signer.dumps('*'),
max_age=app.MAX_AGE, path='/')}
return 403, '', {}
else:
try:
app.unsign(request.cookies.get('admin', ''))
except (SignatureExpired, BadSignature):
return 200, render('login.mako').encode('utf-8'), {'Content-Type': 'text/html'}
ctx = {'app': app, 'request': request}
return 200, render('admin.mako', **ctx).encode('utf-8'), {'Content-Type': 'text/html'}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

@ -0,0 +1,32 @@
# -*- encoding: utf-8 -*-
#
# Copyright 2012, Martin Zimmermann <info@posativ.org>. All rights reserved.
# License: BSD Style, 2 clauses. see isso/__init__.py
from os.path import join, dirname
from werkzeug.wrappers import Response
from werkzeug.exceptions import abort
from werkzeug.utils import redirect
from itsdangerous import SignatureExpired, BadSignature
def index(app, environ, request):
if request.method == 'POST':
print request.form.get('secret'), app.signer.secret_key
if request.form.get('secret') == app.signer.secret_key:
resp = redirect('/admin/', 301)
resp.set_cookie('admin', app.signer.dumps('*'), max_age=app.MAX_AGE)
return resp
else:
return abort(403)
else:
try:
app.unsign(request.cookies.get('admin', ''))
except (SignatureExpired, BadSignature):
return Response(app.render('login.j2'), content_type='text/html')
ctx = {'app': app, 'request': request}
return Response(app.render('admin.j2', app=app, request=request), content_type='text/html')

@ -11,7 +11,7 @@ from itsdangerous import SignatureExpired, BadSignature
from werkzeug.wrappers import Response
from werkzeug.exceptions import abort
from isso import models, utils, requires
from isso import models, utils
class requires:
Loading…
Cancel
Save