| 93 | |
| 94 | '''Update 11th September 2008:''' Here's a prototype implementation (as a mixin class): http://code.google.com/p/django-urls/ |
| 95 | |
| 96 | The code for the prototype mixin is as follows: |
| 97 | |
| 98 | {{{ |
| 99 | #!python |
| 100 | from django.conf import settings |
| 101 | import urlparse |
| 102 | |
| 103 | class 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 | }}} |