==== view function ==== def endpoints(request, namespace = ""): """ Returns javascript for mapping service endpoint names to urls. For this view to work properly, all urls that are to be made available to javascript must be named and end in '-svc'. If these urls use regular expressions for defining parameters, all parameters must be named as well. The view uses Django internal url resolver to iterate over a list of all currently defined url patterns. It looks for named patterns with the name ending in '-svc'. For these patterns, the named regex group definition is replaced with the group name enclosed in curley braces. Url pattern names will be translated into javascript variable names by converting all letters to the upper case and replacing '-' with '_'. For example: url('^blog/(?P[\d]+/$', 'sample.views.showblog', name='blog-entry') will be exported as svc.__BLOG_ENTRY = "/blog/{id}/" if the namespace parameter is set (e.g. 'my.namespace'), the service will be exported as my.namespace.svc.BLOG_ENTRY the the template for additional documentation """ resolver = get_resolver(None) endpoints = {} for name in resolver.reverse_dict: if isinstance(name, str) and name.endswith('-svc'): url_regex = resolver.reverse_dict.get(name)[1] param_names = resolver.reverse_dict.get(name)[0][0][1] arg_pattern = r'\(\?P\<[^\)]+\)' #matches named groups in the form of (?Ppattern) i = 0 for match in re.findall(arg_pattern, url_regex): url_regex = url_regex.replace(match, "{%s}"%param_names[i]) i += 1 name = name.upper().replace("-","_") endpoints[name] = "/" + url_regex[:-1] return render("endpoints.js", {'endpoints':endpoints, 'ns':namespace}, mimetype="application/javascript") ==== end view function ==== ==== endpoints.js template ==== // Namespace is a javascript library by Maxime Bouroumeau-Fuseau // available from http://code.google.com/p/namespacedotjs/ {%if ns%}Namespace('{{ns}}'){%endif%} /* This function provides a map into available service endpoint urls. To get a service url, call this function with the service name. For parametrized urls, also pass a dictionary with parameter name/value paris. For example, calling: svc('CRTRACKER_PL_CLIENT_BUILDS_SVC', {change_request:345, pl:'DFDSF.0', client:'MT'}) will return /crtracker/345/productline/DFDSF.0/client/MT/build/ */ {%if ns%}{{ns}}.{%endif%}svc = function(name, kwargs) { var url = {%if ns%}{{ns}}.{%endif%}svc["__"+name]; for( name in kwargs) { url = url.replace("{"+name+"}", kwargs[name]); } return url; } {% for name, url in endpoints.items %} {%if ns%}{{ns}}.{%endif%}svc.__{{name}} = '{{url}}'; {% endfor %} ==== end template ====