default url routing and shortcut

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from django import http
from django.conf.urls.defaults import url
import re
 
def execute(request, urls, views):
    """
    urls [methodName/]param1/param2/.../
    methodName default index
    """
    def get_method(views, methodName):
        try:
            return getattr(views, methodName)
        except Exception, e:
            return None
    method = None
    params = [e for e in urls.split("/") if e]
    params.reverse()
    if params:
        method = get_method(views, params.pop())
    if not method:
        method = get_method(views, 'index')
    if not method:
        raise http.Http404('The requested admin page does not exist.')
    return method(request, *params)
 
def url_(*args,**dic):
    regex = args[0]
    if regex[0] == "/":
        regex = regex[1:]
    regex = '^' + regex
    regex = regex + '$'
    regex = re.sub(":[^/]+",
            lambda matchobj: "(?P<%s>[^/]+)" % matchobj.group(0)[1:],
            regex)
    return url(regex, *args[1:], **dic)

More like this

  1. Resolve URLs to view name by UloPe 5 years, 1 month ago
  2. A action decorator for URLs by Batiste 6 years ago
  3. Resolve URLs to view name and args/kwargs by fahhem 3 years, 5 months ago
  4. @url decorator - getting rid of urlpatterns by southern_sun 6 years, 7 months ago
  5. Simple views dispatcher by http methods by kmerenkov 4 years, 5 months ago

Comments

bl4th3rsk1t3 (on June 12, 2009):

what does this do?

#

(Forgotten your password?)