Want more features on Pastebin? Sign Up, it's FREE!
Guest

Fahrzin Hemmati

By: a guest on Nov 12th, 2010  |  syntax: Python  |  size: 1.31 KB  |  views: 211  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from django.core.urlresolvers import RegexURLResolver, RegexURLPattern, Resolver404, get_resolver
  2.  
  3. __all__ = ('resolve_to_name',)
  4.  
  5. def _pattern_resolve_to_name(self, path):
  6.     match = self.regex.search(path)
  7.     if match:
  8.         name = ""
  9.         if self.name:
  10.             name = self.name
  11.         elif hasattr(self, '_callback_str'):
  12.             name = self._callback_str
  13.         else:
  14.             name = "%s.%s" % (self.callback.__module__, self.callback.func_name)
  15.         return name
  16.  
  17. def _resolver_resolve_to_name(self, path):
  18.     tried = []
  19.     match = self.regex.search(path)
  20.     if match:
  21.         new_path = path[match.end():]
  22.         for pattern in self.url_patterns:
  23.             try:
  24.                 name = _pattern_resolve_to_name(pattern,new_path)
  25.             except Resolver404, e:
  26.                 tried.extend([(pattern.regex.pattern + '   ' + t) for t in e.args[0]['tried']])
  27.             else:
  28.                 if name:
  29.                     return name
  30.                 tried.append(pattern.regex.pattern)
  31.         raise Resolver404, {'tried': tried, 'path': new_path}
  32.  
  33.  
  34. def resolve_to_name(path, urlconf=None):
  35.     r = get_resolver(urlconf)
  36.     if isinstance(r,RegexURLPattern):
  37.         return _pattern_resolve_to_name(r,path)
  38.     else:
  39.         return _resolver_resolve_to_name(r,path)
clone this paste RAW Paste Data