Alter URL generator to return URLs without host info by default

master
Dustin C. Hatch 2011-04-07 23:25:38 -05:00
parent 0fba1c95b9
commit b11438b69c
1 changed files with 15 additions and 9 deletions

View File

@ -11,6 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from milla.dispatch import UnresolvedPath
import functools
import re
import sys
import urllib
import urlparse
'''URL router '''URL router
:Created: Mar 13, 2011 :Created: Mar 13, 2011
@ -19,11 +25,6 @@
:Updater: $Author$ :Updater: $Author$
''' '''
from milla.dispatch import UnresolvedPath
import functools
import re
import sys
import urllib
class Router(object): class Router(object):
'''A dispatcher that maps arbitrary paths to controller callables '''A dispatcher that maps arbitrary paths to controller callables
@ -164,17 +165,22 @@ class Generator(object):
url = Generator(request).generate url = Generator(request).generate
''' '''
def __init__(self, request): def __init__(self, request, path_only=True):
self.request = request self.request = request
self.path_only = path_only
def generate(self, *segments, **vars): def generate(self, *segments, **vars):
'''Combines segments and the application's URL into a new URL '''Combines segments and the application's URL into a new URL
''' '''
base_url = self.request.application_url
path = '/'.join(str(s) for s in segments) path = '/'.join(str(s) for s in segments)
if not path.startswith('/'): if not path.startswith('/'):
path = '/' + path path = '/' + path
url = self.request.relative_url(path, to_application=True)
if self.path_only:
split = urlparse.urlsplit(url)
url = split.path
if vars: if vars:
path += '?' + urllib.urlencode(vars) url += '?' + urllib.urlencode(vars)
return base_url + path return url