From 2bfbad39cad7e92a6e1eeb52789c717ff479eacc Mon Sep 17 00:00:00 2001 From: posativ Date: Wed, 24 Oct 2012 19:53:56 +0200 Subject: [PATCH] begin dashboard using mako templates --- isso/__init__.py | 6 ++++-- isso/admin.py | 26 +++++++++++++++++++++++++- isso/migrate.py | 3 --- isso/templates/admin.mako | 17 +++++++++++++++++ isso/templates/base.mako | 23 +++++++++++++++++++++++ isso/templates/login.mako | 38 ++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- 7 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 isso/templates/admin.mako create mode 100644 isso/templates/base.mako create mode 100644 isso/templates/login.mako diff --git a/isso/__init__.py b/isso/__init__.py index 7f446ab..728a05e 100644 --- a/isso/__init__.py +++ b/isso/__init__.py @@ -51,7 +51,8 @@ url = lambda path, endpoint, methods: Rule(path, endpoint=endpoint, methods=meth url_map = Map([ # moderation panel - url('/', 'admin.index', ['GET', 'POST']), + url('/', 'admin.login', ['GET', 'POST']), + url('/admin/', 'admin.index', ['GET', 'POST']), # comment API, note that the client side quotes the URL, but this is # actually unnecessary. PEP 333 aka WSGI always unquotes PATH_INFO. @@ -65,6 +66,7 @@ url_map = Map([ class Isso: PRODUCTION = True + SECRET = 'secret' SECRET_KEY = ',\x1e\xbaY\xbb\xdf\xe7@\x85\xe3\xd9\xb4A9\xe4G\xa6O' MODERATION = False SQLITE = None @@ -134,7 +136,7 @@ def main(): if len(args) > 0 and args[0] == 'import': if len(args) < 2: - print 'usage: isso import FILE' + print 'Usage: isso import FILE' sys.exit(2) with io.open(args[1], encoding='utf-8') as fp: diff --git a/isso/admin.py b/isso/admin.py index 883ec97..099c226 100644 --- a/isso/admin.py +++ b/isso/admin.py @@ -3,8 +3,32 @@ # Copyright 2012, Martin Zimmermann . All rights reserved. # License: BSD Style, 2 clauses. see isso/__init__.py +from werkzeug.utils import redirect from werkzeug.wrappers import Response +from mako.lookup import TemplateLookup +from itsdangerous import SignatureExpired, BadSignature + +mako = TemplateLookup(directories=['isso/templates'], input_encoding='utf-8') +render = lambda template, **context: mako.get_template(template).render_unicode(**context) + + +def login(app, environ, request): + + if request.method == 'POST': + if request.form.get('secret') == app.SECRET: + rdr = redirect('/admin/', 301) + rdr.set_cookie('session-admin', app.signer.dumps('*'), max_age=app.MAX_AGE) + return rdr + + return Response(render('login.mako'), content_type='text/html') + def index(app, environ, request): - return Response('', 200) + + try: + app.unsign(request.cookies.get('session-admin', '')) + except (SignatureExpired, BadSignature): + return redirect('/') + + return Response(render('admin.mako'), content_type='text/html') diff --git a/isso/migrate.py b/isso/migrate.py index 1a86692..9bed511 100644 --- a/isso/migrate.py +++ b/isso/migrate.py @@ -33,7 +33,6 @@ def insert(db, thread, comments): author=item['author'], email=item['email'], parent=parent) rv = db.add(path, comment) - print rv.id, rv.text[:25], rv.author remap[item['dsq:id']] = rv.id @@ -62,5 +61,3 @@ def disqus(db, xml): id = thread.attrib.get(dsq + 'id') if id in res: insert(db, thread, res[id]) - # for comment in res[_id]: - # print ' ', comment['author'], comment['text'][:25] diff --git a/isso/templates/admin.mako b/isso/templates/admin.mako new file mode 100644 index 0000000..ed79fb5 --- /dev/null +++ b/isso/templates/admin.mako @@ -0,0 +1,17 @@ +<%inherit file="base.mako"/> + +<%block name="title"> + Isso – Dashboard + + +

Dashboard

+ +
+

Pending

+ +
+ +
+

Recent

+ +
diff --git a/isso/templates/base.mako b/isso/templates/base.mako new file mode 100644 index 0000000..1008e76 --- /dev/null +++ b/isso/templates/base.mako @@ -0,0 +1,23 @@ + + + <%block name="title" /> + + + + + ${self.body()} + \ No newline at end of file diff --git a/isso/templates/login.mako b/isso/templates/login.mako new file mode 100644 index 0000000..b7b757c --- /dev/null +++ b/isso/templates/login.mako @@ -0,0 +1,38 @@ +<%inherit file="base.mako"/> + +<%block name="title"> + Isso – Login + + +<%block name="style"> + + #login { + margin: 280px 270px 0 270px; + padding: 30px; + background-color: #DDD; + border-radius: 8px; + text-align: center; + } + + .button { + border: 1px solid #006; + margin-left: 2px; + padding: 2px 4px 2px 4px; + } + + #login input { + margin-top: 8px; + text-align: center; + } + #login input:-moz-placeholder, #login input::-webkit-input-placeholder {} { + color: #CCC; + } + + + +
+
+ + +
+
diff --git a/setup.py b/setup.py index 99e8d47..8a02e44 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ setup( "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7" ], - install_requires=['werkzeug', 'itsdangerous'], + install_requires=['werkzeug', 'mako', 'itsdangerous'], entry_points={ 'console_scripts': ['isso = isso:main'],