Forráskód Böngészése

Yes we can do AJAX

oz123 9 éve
szülő
commit
58351e8902
2 módosított fájl, 32 hozzáadás és 10 törlés
  1. 2 0
      pwman/ui/templates/base.tpl
  2. 30 10
      scripts/webui.py

+ 2 - 0
pwman/ui/templates/base.tpl

@@ -12,6 +12,8 @@
         <link rel="stylesheet" href="/static/css/normalize.min.css">
         <link rel="stylesheet" href="/static/css/main.css">
         <script src="/static/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
+	<script type="text/javascript"
+	  src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
     </head>
 <body>
     <!--[if lt IE 7]>

+ 30 - 10
scripts/webui.py

@@ -27,6 +27,7 @@ from pkg_resources import resource_filename
 import sys
 from signal import SIGTERM
 import os
+import json
 
 templates_path = [resource_filename('pwman', 'ui/templates')]
 statics = [resource_filename('pwman', 'ui/templates/static')][0]
@@ -57,11 +58,6 @@ def require_auth(fn):
     return check_auth
 
 
-@route('/static/<filename:path>')
-def send_static(filename):
-    return static_file(filename, root=statics)
-
-
 @route('/node/:no')
 @require_auth
 def view_node(no):
@@ -73,8 +69,23 @@ def view_node(no):
                                        node[3],
                                        node[4],
                                        node[5:])
-    output = template("view.tpl", node=node, template_lookup=templates_path)
-    return output
+    return template("ajax.tpl", request=request, node=node,
+                    template_lookup=templates_path)
+
+
+@route('/_add_numbers')
+def add_numbers():
+    """Add two numbers server side, ridiculous but well..."""
+    a = request.params.get('a', 0, type=int)
+    b = request.params.get('b', 0, type=int)
+    return json.dumps({'result': a+b})
+
+
+
+
+@route('/static/<filename:path>')
+def send_static(filename):
+    return static_file(filename, root=statics)
 
 
 def submit_node(id, request):
@@ -99,7 +110,7 @@ def edit_node(no=None):
                                            node[4],
                                            node[5:])
 
-    output = template('edit.tpl', node=node,
+    output = template('edit.tpl', node=node,  
                       template_lookup=templates_path)
     return output
 
@@ -131,7 +142,16 @@ def is_authenticated():
         AUTHENTICATED = True
         redirect('/')
     else:
-        return template("login.tpl", template_lookup=templates_path)
+        return template("login.tpl", template_lookup=templates_path, request=request)
+
+
+@route('/auth', method=['GET', 'POST'])
+def is_authenticated():
+    crypto = CryptoEngine.get()
+    crypto.authenticate('foobar')
+    global AUTHENTICATED
+    AUTHENTICATED = True
+    redirect('/')
 
 
 @route('/', method=['GET', 'POST'])
@@ -163,7 +183,7 @@ def listnodes(apply=['require_login']):
     nodesd = _nodes_inst
     ce = CryptoEngine.get()
     tags = [ce.decrypt(t).decode() for t in DB.listtags()]
-    html_nodes = template("main.tpl", nodes=nodesd, tags=tags,
+    html_nodes = template("main.tpl", nodes=nodesd, tags=tags, request=request,
                           template_lookup=[resource_filename('pwman',
                                                              'ui/templates')])
     return html_nodes