move code around™

This commit is contained in:
Martin Zimmermann 2013-09-03 12:15:43 +02:00
parent cb20f159fb
commit 2d075f867a
11 changed files with 51 additions and 2332 deletions

View File

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

View File

@ -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
isso/views/__init__.py Normal file
View File

32
isso/views/admin.py Normal file
View File

@ -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')

View File

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