Jan Brohl avatar Jan Brohl committed 9fbb0aa

fast kompletter rewrite

Comments (0)

Files changed (8)

src/default.pt

-	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
-     "http://www.w3.org/TR/html4/loose.dtd">
-<html>
-<head>
-<title>Titel</title>
-</head>
-<body tal:content="options/context/group/1460">
- 
-</body>
-</html> 

src/templates/collection_event.pt

+<!DOCTYPE html>
+<head>
+<title>Ereignisse
+</title>
+</head>
+<body>
+<div tal:repeat="event here/elements/values"><a tal:content="repeat/event"></a></div>
+</body>

src/templates/exception.pt

+<!DOCTYPE html>
+<head>
+<title>Fehler</title>
+</head>
+<body>
+<h1>Fehler</h1>
+<p tal:content="here/message">fehlertext</p>
+</body>

src/templates/group.pt

+<!DOCTYPE html>
+<head>
+<title tal:content="here/name">
+</title>
+</head>
+<body>
+<h1 tal:content="here/name">Name</h1>
+<p>In <span class="stadt" tal:content="here/city">Stadt</span> <span class="bundesland" tal:content="string: (${here/federal_state})">Bundesland</span></p>
+<a tal:attributes="href string: ${env/PATH_INFO}/parent">Zur übergeordneten Einheit</a>
+<a tal:attributes="href string: ${env/PATH_INFO}/events">Termine</a>
+</body>

src/templates4scoutnet/__init__.py

-from main import application
+from main import TemplateApp

src/templates4scoutnet/examples/cgi.py

 #!/usr/bin/env python
+import cgitb
+cgitb.enable()
 from wsgiref.handlers import CGIHandler
 from templates4scoutnet import application
 CGIHandler().run(application)

src/templates4scoutnet/examples/wsgiserver.py

 @author: Jan
 '''
 import wsgiref.simple_server
-from templates4scoutnet import application
+from templates4scoutnet import TemplateApp
 
 try:
-    server = wsgiref.simple_server.make_server("localhost", 80, application)
+    server = wsgiref.simple_server.make_server("localhost", 80, TemplateApp())
     server.serve_forever()
 finally:
     server.close()

src/templates4scoutnet/main.py

 @copyright: Copyright (c) 2012, Jan Brohl <janbrohl@t-online.de>. All rights reserved.
 '''
 
-from scoutnet.lazyapi import Lazy
-from werkzeug.wrappers import Response, Request
+from pyscoutnet.lazyapi import Lazy
 from simpletal.simpleTAL import compileHTMLTemplate
 from simpletal.simpleTALES import Context
 from urllib import urlopen
 from cStringIO import StringIO
 from simplerest.util import NoCallWrapper, TALVirtualPath
-from urlparse import urlsplit, urlunsplit
+from urlparse import urlsplit, urlunsplit, parse_qs
 
-template_pfad = "http://localhost/templates4scoutnet/templates/" # oder sowas in der art
 
-@Request.application
-def application(request):
-    charset = request.charset or "UTF-8"
-    args = request.args
-    template_url = args.get("template", None)
-    if template_url:
-        tu_parts = urlsplit(template_url)
-        if tu_parts.scheme:
-            pp = urlunsplit(tu_parts[:2] + ([""] * 3))
+class TemplateApp(object):
+    def __init__(self, basepath=["templates"]):
+        self.basepath = basepath
+
+    def __call__(self, environ, start_response):
+        charset = "utf-8"
+        path = environ["PATH_INFO"].strip("/").split("/")
+        args = parse_qs(environ["QUERY_STRING"], keep_blank_values=True)
+        template_root = args.get("template", None)
+        kind = str(Lazy(path=path + ["kind"]))
+        if kind == "collection":
+            kind = "collection_" + str(Lazy(path=path + ["element_kind"]))
+        if template_root:
+            tvp = RemoteTemplatePath(path=[kind], basepath=template_root[0])
         else:
-            pp = template_pfad
-        bp = tu_parts[2].strip("/")
-        if bp:
-            bp = bp.split("/")
-            path = bp.pop(-1) + tu_parts[3]
-        else:
-            bp = []
-            path = tu_parts[3]
-        tvp = TemplateVirtualPath(path=[], basepath=bp, path_prefix=pp)
-        template = tvp[path]()
-        wrapper = TemplateWrapper(tvp)
-    else:
-        wrapper = None
-        f = open("default.pt")
-        incharset = "UTF-8"
-        template = compileHTMLTemplate(f, incharset)
-        f.close()
-    ctx = NoCallWrapper(Lazy(path=args["context"]))
-    d = dict(args.to_dict(False))
-    s = render(template, d, ctx, wrapper, charset)
-    return Response(s, content_type="text/html;charset=" + charset)
+            tvp = LocalTemplatePath(path=[kind], basepath=self.basepath)
+        wrapper = TemplateWrapper(tvp["container"])
+        ctx = NoCallWrapper(Lazy(path=path))
+        d = args
+        s = render(tvp(), d, ctx, wrapper, environ, charset)
+        status = '200 OK'
+        headers = [('Content-Type', "text/html;charset=" + charset), ("Content-Length", str(len(s)))]
+        start_response(status, headers)
+        return [s]
 
-class TemplateVirtualPath(TALVirtualPath):
+class RemoteTemplatePath(TALVirtualPath):
     def __call__(self):
-        f = urlopen(self.kwarg("path_prefix") + self.path_as_list)
+        f = urlopen((self.kwarg("path_prefix") + "/" if self.kwarg("path_prefix") else "")
+                    + "/".join(self.path_as_list))
         incharset = f.info().getparam("charset")
         return compileHTMLTemplate(f, incharset)
 
+
+class LocalTemplatePath(TALVirtualPath):
+    def __call__(self):
+        f = open((self.kwarg("path_prefix") + "/" if self.kwarg("path_prefix") else "")
+                    + "/".join(self.path_as_list) + ".pt", "rb")
+        incharset = self.kwarg("charset") or "utf-8"
+        return compileHTMLTemplate(f, incharset)
+
+
 class TemplateWrapper(NoCallWrapper):
     def macros(self):
         return self().macros
 
-def render(template, options, context, container, charset):
+
+def render(template, options, context, container, env, charset):
     ctx = Context(options)
-    ctx.addGlobal("context", context)
+    ctx.addGlobal("here", context)
     ctx.addGlobal("container", container)
+    ctx.addGlobal("env", env)
     f = StringIO()
     template.expand(ctx, f, charset)
     return f.getvalue()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.