django.utils.functional.lazy - python examples

Here are the examples of the python api django.utils.functional.lazy taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

59 Examples 7

3 View Complete Implementation : context_processors.py
Copyright GNU General Public License v2.0
Author : blackye
def csrf(request):
    """
    Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
    it has not been provided by either a view decorator or the middleware
    """
    def _get_val():
        token = get_token(request)
        if token is None:
            # In order to be able to provide debugging info in the
            # case of misconfiguration, we use a sentinel value
            # instead of returning an empty dict.
            return 'NOTPROVIDED'
        else:
            return smart_text(token)
    _get_val = lazy(_get_val, six.text_type)

    return {'csrf_token': _get_val() }

3 View Complete Implementation : context_processors.py
Copyright MIT License
Author : bpgc-cte
def debug(request):
    """
    Returns context variables helpful for debugging.
    """
    context_extras = {}
    if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
        context_extras['debug'] = True
        from django.db import connections
        # Return a lazy reference that computes connection.queries on access,
        # to ensure it contains queries triggered after this function runs.
        context_extras['sql_queries'] = lazy(
            lambda: list(itertools.chain(*[connections[x].queries for x in connections])),
            list
        )
    return context_extras

3 View Complete Implementation : context_processors.py
Copyright Apache License 2.0
Author : datamate-rethink-it
def debug(request):
    """
    Returns context variables helpful for debugging.
    """
    context_extras = {}
    if dj_settings.DEBUG and request.META.get('REMOTE_ADDR') in dj_settings.INTERNAL_IPS or \
       dj_settings.DEBUG and request.GET.get('_dev', '') == '1' or \
       dj_settings.DEBUG and not dj_settings.COMPRESS_ENABLED:
        context_extras['debug'] = True
        from django.db import connection
        # Return a lazy reference that computes connection.queries on access,
        # to ensure it contains queries triggered after this function runs.
        context_extras['sql_queries'] = lazy(lambda: connection.queries, list)
    return context_extras

3 View Complete Implementation : context_processors.py
Copyright Apache License 2.0
Author : drexly
def debug(request):
    """
    Returns context variables helpful for debugging.
    """
    context_extras = {}
    if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
        context_extras['debug'] = True
        from django.db import connection
        # Return a lazy reference that computes connection.queries on access,
        # to ensure it contains queries triggered after this function runs.
        context_extras['sql_queries'] = lazy(lambda: connection.queries, list)
    return context_extras

3 View Complete Implementation : override.py
Copyright GNU General Public License v3.0
Author : evernote
def override_gettext(real_translation):
    """Replace Django's translation functions with safe versions."""
    translation.gettext = real_translation.gettext
    translation.ugettext = real_translation.ugettext
    translation.ngettext = real_translation.ngettext
    translation.ungettext = real_translation.ungettext
    translation.gettext_lazy = lazy(real_translation.gettext, str)
    translation.ugettext_lazy = lazy(real_translation.ugettext, unicode)
    translation.ngettext_lazy = lazy(real_translation.ngettext, str)
    translation.ungettext_lazy = lazy(real_translation.ungettext, unicode)

3 View Complete Implementation : blocks.py
Copyright MIT License
Author : ixc
    def __init__(self, target_model, **kwargs):
        super().__init__(**kwargs)

        self._target_model = target_model

        if self.meta.icon == "placeholder":
            # The models/selectors may not have been registered yet, depending upon
            # import orders and things, so get the icon lazily
            self.meta.icon = lazy(self.get_instance_selector_icon, str)

3 View Complete Implementation : urlresolvers.py
Copyright GNU Affero General Public License v3.0
Author : liqd
def patch_reverse():
    """Overwrite the default reverse implementation.

    Monkey-patches the urlresolvers.reverse function. Will not patch twice.
    """
    global django_reverse, django_reverse_lazy
    if hasattr(settings, 'REVERSE_METHOD') and django_reverse is None:
        django_reverse = urls.reverse
        django_reverse_lazy = urls.reverse_lazy

        module_name, func_name = settings.REVERSE_METHOD.rsplit('.', 1)
        reverse = getattr(importlib.import_module(module_name), func_name)

        urls.reverse = reverse
        urls.reverse_lazy = lazy(reverse, six.text_type)

3 View Complete Implementation : blocks.py
Copyright BSD 2-Clause "Simplified" License
Author : neon-jungle
    def __init__(self, target_model, filter_name=None, **kwargs):
        super(ModelChooserBlock, self).__init__(**kwargs)
        self._target_model = target_model

        self.filter_name = filter_name

        if self.meta.icon == 'placeholder':
            # Get the icon from the chooser.
            # The chooser may not have been registered yet, depending upon
            # import orders and things, so get the icon lazily
            self.meta.icon = lazy(lambda: self.chooser.icon, str)()

3 View Complete Implementation : test_escapejs.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_lazy_string(self):
        append_script = lazy(lambda string: r'<script>this</script>' + string, str)
        self.astertEqual(
            escapejs_filter(append_script('whitespace: \r\n\t\v\f\b')),
            '\\u003Cscript\\u003Ethis\\u003C/script\\u003E'
            'whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008'
        )

3 View Complete Implementation : test_linebreaks.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_lazy_string_input(self):
        add_header = lazy(lambda string: 'Header\n\n' + string, str)
        self.astertEqual(
            linebreaks_filter(add_header('line 1\r\nline2')),
            '<p>Header</p>\n\n<p>line 1<br>line2</p>'
        )