[cc-commits] [SCM] License chooser & etc (branch master) updated. 9f36f9d98133a4d5d7056d24bd117f2068390690

git version control git at a7.creativecommons.org
Mon Jan 4 23:57:45 EST 2010


The branch, master has been updated
       via  9f36f9d98133a4d5d7056d24bd117f2068390690 (commit)
       via  f439fce9d507794d6bd557a86e1367001855a377 (commit)
       via  dfc94091ec92f053b24788d5fce35eba32cfc4bf (commit)
       via  020e529033342e9a5b695ce8e1715142997799f4 (commit)
      from  23ff6d81d5a3ecbead374dd54eda4069b17bbbf3 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 9f36f9d98133a4d5d7056d24bd117f2068390690
Author: Christopher Allan Webber <cwebber at dustycloud.org>
Date:   Mon Jan 4 22:57:39 2010 -0600

    Static resource stuff now works, kinda

commit f439fce9d507794d6bd557a86e1367001855a377
Author: Christopher Allan Webber <cwebber at dustycloud.org>
Date:   Mon Jan 4 17:47:03 2010 -0600

    Adjusting indentation here

commit dfc94091ec92f053b24788d5fce35eba32cfc4bf
Author: Christopher Allan Webber <cwebber at dustycloud.org>
Date:   Mon Jan 4 17:45:28 2010 -0600

    Adding request.urlgen.  Now we can generate named urls!

commit 020e529033342e9a5b695ce8e1715142997799f4
Author: Christopher Allan Webber <cwebber at dustycloud.org>
Date:   Mon Jan 4 17:40:53 2010 -0600

    Now serving static resources

-----------------------------------------------------------------------

Summary of changes (followed by patch):
 .gitignore                  |    6 ++++-
 buildout.cfg                |    2 +-
 cc/engine/app.py            |   50 ++++++++++++++++++++++++-----------------
 cc/engine/licenses/views.py |   20 ++++++++--------
 cc/engine/routing.py        |    3 ++
 cc/engine/static.py         |   51 ++++++++++++++++++++++++++++++++++++++----
 cc/engine/views.py          |   17 ++++++++++++++
 7 files changed, 111 insertions(+), 38 deletions(-)

diff --git a/.gitignore b/.gitignore
index 90bd267..ef57065 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,8 @@
-cc/engine/resources/svn
+cc/engine/resources/cc3
+cc/engine/resources/cc4
+cc/engine/resources/cc5
+cc/engine/resources/images
+cc/engine/resources/includes
 dist/
 bin/
 develop-eggs/
diff --git a/buildout.cfg b/buildout.cfg
index 866dec3..c5636fa 100644
--- a/buildout.cfg
+++ b/buildout.cfg
@@ -20,4 +20,4 @@ urls =
     http://code.creativecommons.org/svnroot/ccwordpress/trunk/www/wp-content/themes/cc5 cc5
     http://code.creativecommons.org/svnroot/ccwordpress/trunk/www/images images
     http://code.creativecommons.org/svnroot/ccwordpress/trunk/www/includes includes
-location = cc/engine/resources/svn
\ No newline at end of file
+location = cc/engine/resources
\ No newline at end of file
diff --git a/cc/engine/app.py b/cc/engine/app.py
index d45f34b..cc65b93 100644
--- a/cc/engine/app.py
+++ b/cc/engine/app.py
@@ -1,9 +1,10 @@
 import sys
 import urllib
 
+import routes
 from webob import Request, exc
 
-from cc.engine import routing
+from cc.engine import routing, static
 
 
 def load_controller(string):
@@ -14,29 +15,36 @@ def load_controller(string):
     return func
 
 
-def ccengine_app(environ, start_response):
+class CCEngineApp(object):
     """
     Really basic wsgi app using routes and WebOb.
     """
-    request = Request(environ)
-    path_info = request.path_info
-    route_match = routing.mapping.match(path_info)
-    if route_match is None:
-        if not path_info.endswith('/') \
-                and request.method == 'GET' \
-                and routing.mapping.match(path_info + '/'):
-            new_path_info = path_info + '/'
-            if request.GET:
-                new_path_info = '%s?%s' % (
-                    new_path_info, urllib.urlencode(request.GET))
-            redirect = exc.HTTPTemporaryRedirect(location=new_path_info)
-            return request.get_response(redirect)(environ, start_response)
-        return exc.HTTPNotFound()(environ, start_response)
-    controller = load_controller(route_match['controller'])
-    request.start_response = start_response
-    request.matchdict = route_match
-    return controller(request)(environ, start_response)
+    def __init__(self):
+        self.staticdirect = static.LocalStaticDirect()
+
+    def __call__(self, environ, start_response):
+        request = Request(environ)
+        path_info = request.path_info
+        route_match = routing.mapping.match(path_info)
+        if route_match is None:
+            if not path_info.endswith('/') \
+                    and request.method == 'GET' \
+                    and routing.mapping.match(path_info + '/'):
+                new_path_info = path_info + '/'
+                if request.GET:
+                    new_path_info = '%s?%s' % (
+                        new_path_info, urllib.urlencode(request.GET))
+                redirect = exc.HTTPTemporaryRedirect(location=new_path_info)
+                return request.get_response(redirect)(environ, start_response)
+            return exc.HTTPNotFound()(environ, start_response)
+        controller = load_controller(route_match['controller'])
+        request.start_response = start_response
+        request.matchdict = route_match
+        request.urlgen = routes.URLGenerator(routing.mapping, environ)
+        request.staticdirect = lambda filepath: self.staticdirect(
+            request, filepath)
+        return controller(request)(environ, start_response)
 
 
 def ccengine_app_factory(global_config, **kw):
-    return ccengine_app
+    return CCEngineApp()
diff --git a/cc/engine/licenses/views.py b/cc/engine/licenses/views.py
index 34f5859..c182a84 100644
--- a/cc/engine/licenses/views.py
+++ b/cc/engine/licenses/views.py
@@ -90,16 +90,16 @@ def license_deed_view(request, license):
             target_lang=target_lang)
 
     context = {
-            'license_code': license.license_code,
-            'license_title': license_title,
-            'license': license,
-            'multi_language': multi_language,
-            'color': color,
-            'conditions': conditions,
-            'deed_template': deed_template,
-            'active_languages': active_languages,
-            'support_template': support_template,
-            'target_lang': target_lang}
+        'license_code': license.license_code,
+        'license_title': license_title,
+        'license': license,
+        'multi_language': multi_language,
+        'color': color,
+        'conditions': conditions,
+        'deed_template': deed_template,
+        'active_languages': active_languages,
+        'support_template': support_template,
+        'target_lang': target_lang}
     context.update(util.rtl_context_stuff(request))
 
     return Response(main_template.pt_render(context))
diff --git a/cc/engine/routing.py b/cc/engine/routing.py
index cbca342..491811a 100644
--- a/cc/engine/routing.py
+++ b/cc/engine/routing.py
@@ -14,3 +14,6 @@ mapping.connect(
 mapping.extend(licenses_routing.licenses_routes, '/licenses')
 mapping.extend(licenses_routing.cc0_routes, '/publicdomain/zero')
 mapping.extend(chooser_routing.chooser_routes, '/choose')
+mapping.connect(
+    'staticserve', '/static/{filename:.*?}',
+    controller="cc.engine.views:staticserve_view")
diff --git a/cc/engine/static.py b/cc/engine/static.py
index 46c127c..99a1e8d 100644
--- a/cc/engine/static.py
+++ b/cc/engine/static.py
@@ -1,7 +1,48 @@
-from repoze.bfg.view import static
+import urlparse
 
-import pkg_resources
 
-static_view = static(
-    pkg_resources.resource_filename(
-        'cc.engine', 'resources'))
+class StaticDirect(object):
+    def __init__(self):
+        self.cache = {}
+
+    def __call__(self, request, filepath):
+        if self.cache.has_key(filepath):
+            return self.cache[filepath]
+
+        static_direction = self.cache[filepath] = self.get(request, filepath)
+        return static_direction
+        
+
+    def get(self, request, filepath):
+        # should be implemented by the individual staticdirector
+        pass
+
+
+class LocalStaticDirect(StaticDirect):
+    """
+    Static serving handled by cc.engine.  Generally not a good idea,
+    except for development!
+    """
+    def get(self, request, filepath):
+        return request.urlgen('staticserve', filename=filepath)
+
+
+class RemoteStaticDirect(StaticDirect):
+    def __init__(self, remotepath):
+        self.remotepath = remotepath
+
+    def get(self, request, filepath):
+        return urlparse.urljoin(self.remotepath, filepath)
+
+
+class MultiRemoteStaticDirect(StaticDirect):
+    """
+    """
+    def __init__(self, remotepath):
+        self.remotepaths = remotepaths
+
+    def get(self, request, filepath):
+        section, rest = filepath.strip('/').split('/', 1)
+
+        return urlparse.urljoin(
+            self.remotepaths[section], rest)
diff --git a/cc/engine/views.py b/cc/engine/views.py
index bf67c63..758052b 100644
--- a/cc/engine/views.py
+++ b/cc/engine/views.py
@@ -1,5 +1,22 @@
+import mimetypes
+
 from webob import Response
 
+from cc.engine import util
+
 
 def root_view(request):
     return Response("This is the root")
+
+
+def staticserve_view(request):
+    filename = request.matchdict['filename']
+    mimetype = mimetypes.guess_type(filename)[0]
+
+    response = Response(content_type=mimetype)
+    content_file = file(
+        util.safer_resource_filename(
+            'cc.engine.resources', filename))
+
+    response.body_file.write(content_file.read())
+    return response


hooks/post-receive
-- 
License chooser & etc



More information about the cc-commits mailing list