Changes between Version 6 and Version 7 of ReplacingGetAbsoluteUrl


Ignore:
Timestamp:
Sep 10, 2008, 6:46:40 PM (16 years ago)
Author:
simon
Comment:

Added prototype implementation

Legend:

Unmodified
Added
Removed
Modified
  • ReplacingGetAbsoluteUrl

    v6 v7  
    9191
    9292I don't think this needs to be all that complicated, and in fact the above scheme could allow us to delete a whole bunch of weird special case code scattered throughout Django.
     93
     94'''Update 11th September 2008:''' Here's a prototype implementation (as a mixin class): http://code.google.com/p/django-urls/
     95
     96The code for the prototype mixin is as follows:
     97
     98{{{
     99#!python
     100from django.conf import settings
     101import urlparse
     102
     103class UrlMixin(object):
     104   
     105    def get_url(self):
     106        if hasattr(self.get_url_path, 'dont_recurse'):
     107            raise NotImplemented
     108        try:
     109            path = self.get_url_path()
     110        except NotImplemented:
     111            raise
     112        # Should we look up a related site?
     113        #if getattr(self._meta, 'url_by_site'):
     114        prefix = getattr(settings, 'DEFAULT_URL_PREFIX', 'http://localhost')
     115        return prefix + path
     116    get_url.dont_recurse = True
     117   
     118    def get_url_path(self):
     119        if hasattr(self.get_url, 'dont_recurse'):
     120            raise NotImplemented
     121        try:
     122            url = self.get_url()
     123        except NotImplemented:
     124            raise
     125        bits = urlparse.urlparse(url)
     126        return urlparse.urlunparse(('', '') + bits[2:])
     127    get_url_path.dont_recurse = True
     128}}}
Back to Top