Пример #1
0
def kolibri_set_urls(context):
    # Modified from:
    # https://github.com/ierror/django-js-reverse/blob/master/django_js_reverse/core.py#L101
    js_global_object_name = "window"
    js_var_name = "kolibriUrls"
    script_prefix = get_script_prefix()

    if "request" in context:
        default_urlresolver = get_resolver(
            getattr(context["request"], "urlconf", None))
    else:
        default_urlresolver = get_resolver(None)

    js = render_to_string(
        "django_js_reverse/urls_js.tpl",
        {
            "urls": sorted(list(prepare_url_list(default_urlresolver))),
            "url_prefix": script_prefix,
            "js_var_name": js_var_name,
            "js_global_object_name": js_global_object_name,
        },
    )

    return mark_safe("""<script type="text/javascript">""" + jsmin(js) + """
        {global_object}.staticUrl = '{static_url}';
        {global_object}.mediaUrl = '{media_url}';
        </script>
        """.format(
        global_object=js_global_object_name,
        static_url=settings.STATIC_URL,
        media_url=settings.MEDIA_URL,
    ))
Пример #2
0
    def url_tag(self):
        # Modified from:
        # https://github.com/ierror/django-js-reverse/blob/master/django_js_reverse/core.py#L101
        js_name = "window.kolibriPluginDataGlobal['{bundle}'].urls".format(
            bundle=self.unique_id
        )
        default_urlresolver = get_resolver(None)

        data = generate_json(default_urlresolver)

        # Generate the JS that exposes functions to reverse all Django URLs
        # in the frontend.
        js = render_to_string(
            "django_js_reverse/urls_js.tpl",
            {"data": _safe_json(data), "js_name": "__placeholder__"},
            # For some reason the js_name gets escaped going into the template
            # so this was the easiest way to inject it.
        ).replace("__placeholder__", js_name)
        zip_content_origin, zip_content_port = get_zip_content_config()
        return [
            mark_safe(
                """<script type="text/javascript">"""
                # Minify the generated Javascript
                + jsmin(js)
                # Add URL references for our base static URL, the Django media URL
                # and our content storage URL - this allows us to calculate
                # the path at which to access a local file on the frontend if needed.
                + """
            {js_name}.__staticUrl = '{static_url}';
            {js_name}.__mediaUrl = '{media_url}';
            {js_name}.__contentUrl = '{content_url}';
            {js_name}.__zipContentUrl = '{zip_content_url}';
            {js_name}.__hashiUrl = '{hashi_url}';
            {js_name}.__zipContentOrigin = '{zip_content_origin}';
            {js_name}.__zipContentPort = {zip_content_port};
            </script>
            """.format(
                    js_name=js_name,
                    static_url=settings.STATIC_URL,
                    media_url=settings.MEDIA_URL,
                    content_url=get_content_storage_url(
                        baseurl=OPTIONS["Deployment"]["URL_PATH_PREFIX"]
                    ),
                    zip_content_url=get_zip_content_base_path(),
                    hashi_url=get_hashi_path(),
                    zip_content_origin=zip_content_origin,
                    zip_content_port=zip_content_port,
                )
            )
        ]
Пример #3
0
def url_tag(self):
    # Modified from:
    # https://github.com/learningequality/kolibri/blob/release-v0.14.x/kolibri/core/kolibri_plugin.py#L36
    # This version revised to remove bundle-namespaced data.

    default_urlresolver = get_resolver(None)

    data = generate_json(default_urlresolver)

    # Generate the JS that exposes functions to reverse all Django URLs
    # in the frontend.
    js = render_to_string(
        "django_js_reverse/urls_js.tpl",
        context={
            "data": _safe_json(data),
            "js_name": "window.Urls"
        },
    )
    return {
        'I18N_URLS':
        mark_safe("""<script type="text/javascript">"""
                  # Minify the generated Javascript
                  + jsmin(js) + """</script>""")
    }