Publishing service endpoint uri to javascript

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
==== 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<id>[\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 (?P<name>pattern)

            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 ====

More like this

  1. Generic AJAX app by Rupe 4 years, 10 months ago
  2. Ajax form with jQuery by Donn 5 years, 8 months ago
  3. ajax error sink by amitu 5 years, 6 months ago
  4. FieldsetForm by Ciantic 7 years ago
  5. Django Uri Param Generator by ioan1k 4 years, 7 months ago

Comments

(Forgotten your password?)