Commits

Benoit Chesneau  committed b113c89

test for subdomain handling.

  • Participants
  • Parent commits 01a9a3f

Comments (0)

Files changed (8)

File couchit.fcgi

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# Copyright 2008 by Benoît Chesneau <benoitc@e-engura.com>
+# 
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from flup.server.fcgi_fork import WSGIServer
+from werkzeug import script
+
+def make_couchit():
+    from couchit.application import CouchitApp
+    return CouchitApp()
+
+app = make_couchit()
+srv = WSGIServer(app)
+
+if __name__ == '__main__':
+    srv.run()

File couchit/api.py

 
 ALL_COLORSHEME = list(get_all_styles())
 
-def get_site(db, name):
-    rows = Site.view(db, '_view/site/by_cname', key=name)
+def get_site(db, name, by_alias=False):
+    if by_alias:
+        vn = '_view/site/alias'
+    else:
+        vn = '_view/site/by_cname'
+    rows = Site.view(db, vn, key=name)
     lrows = list(iter(rows))
     if lrows:
         return lrows[0]

File couchit/application.py

     
     def dispatch(self, environ, start_response):
         local.request = request = BCRequest(self, environ)
-        local.url_adapter = adapter = urls_map.bind_to_environ(environ)
+        local.url_adapter = adapter = urls_map.bind_to_environ(environ, 
+                                        server_name=settings.SERVER_NAME)
         try:
             endpoint, args = adapter.match()
             response = self.views[endpoint](request, **args)
             response = e
 
         if request.session.should_save:
-            print 'here'
             permanent = request.session.get('permanent', True)
             max_age=None
             expires=None

File couchit/models.py

 
 
 class Site(Document):
+    alias = TextField()
     cname = TextField()
     title = TextField()
     subtitle = TextField()

File couchit/settings.py

 
 DEBUG = True
 
+SERVER_NAME = 'couch.it:5000'
+
 # database
 SERVER_URI ='http://127.0.0.1:5984'
 DATABASE_NAME ='couchit'

File couchit/urls.py

     Rule('/<cname>/<pagename>/edit', endpoint='edit_page'),
     Rule('/<cname>/<pagename>/diff', endpoint='diff_page'),
     Rule('/<cname>', defaults={'pagename': 'home' }, endpoint='show_page'),
-    Rule('/<cname>/<pagename>', endpoint='show_page'),
-    Subdomain('<cname>',[
-        Rule('/', defaults={'pagename': 'home' }, endpoint='show_page'),
+    Rule('/<cname>/<pagename>', endpoint='show_page', subdomain=''),
+    Subdomain('<string:alias>',[
+        Rule('/', defaults={ 'pagename': 'home' }, endpoint='show_page'),
         Rule('/proxy', endpoint='proxy'),
         Rule('/site/design', endpoint='site_design'),
         Rule('/site/claim', endpoint='site_claim'),
         Rule('/<pagename>/diff', endpoint='diff_page'),
         Rule('/<pagename>', endpoint='show_page'),
     ])
-])
+], default_subdomain='')

File couchit/views.py

         site = None
         if not hasattr(request, 'site'):
             cname = kwargs.get('cname', None)
-            if cname is None:
-                return redirect('/')
-
+            alias = kwargs.get('alias', None)
+            if alias is not None:
+                site = get_site(local.db, alias, by_alias=True)
+                print "alias %s" % alias
+                print site
+            elif cname is not None:
+                site = get_site(local.db, cname)
+                print "cname %s, site %s" % (cname, site)
             if site is None:
-                site = get_site(local.db, cname)
-                if site is None:
-                    return redirect('/')
+                print "bordel"
+                return redirect("http://%s" % settings.SERVER_NAME)
+                
             request.site = site  
         return f(request, **kwargs)
     return decorated
   
 @site_required  
 def show_page(request, cname=None, pagename=None, alias=None):
-    print alias
     if pagename is None:
         pagename ='home'
     page = get_page(local.db, request.site.id, pagename)

File templates/sidebar.html

     <h4>Pages</h4>  
     <ul class="pagelist">
         {% for p in pages %}
-        <li><a href="{{ url_for('show_page', cname=site.cname, pagename=p.title|replace(" ", "_"))  }}">{{ p.title }}</a></li>
+        <li><a href="{{ url_for('show_page', cname=site.cname, pagename=p.title|replace(" ", "_"), alias=site.alias)  }}">{{ p.title }}</a></li>
         {% endfor %}
     </ul>
 </div>