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.
# See the License for the specific language governing permissions and
# limitations under the License.
from milla.dispatch import UnresolvedPath
import functools
import re
import sys
import urllib
import urlparse
'''URL router
:Created: Mar 13, 2011
@ -19,11 +25,6 @@
:Updater: $Author$
'''
from milla.dispatch import UnresolvedPath
import functools
import re
import sys
import urllib
class Router(object):
'''A dispatcher that maps arbitrary paths to controller callables
@ -164,17 +165,22 @@ class Generator(object):
url = Generator(request).generate
'''
def __init__(self, request):
def __init__(self, request, path_only=True):
self.request = request
self.path_only = path_only
def generate(self, *segments, **vars):
'''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)
if not path.startswith('/'):
path = '/' + path
url = self.request.relative_url(path, to_application=True)
if self.path_only:
split = urlparse.urlsplit(url)
url = split.path
if vars:
path += '?' + urllib.urlencode(vars)
return base_url + path
url += '?' + urllib.urlencode(vars)
return url