Python django.utils.safestring.SafeData() Examples
The following are 30
code examples of django.utils.safestring.SafeData().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.utils.safestring
, or try the search function
.
Example #1
Source File: trans_real.py From bioforum with MIT License | 6 votes |
def gettext(message): """ Translate the 'message' string. It uses the current thread to find the translation object to use. If no current translation is activated, the message will be run through the default translation object. """ global _default eol_message = message.replace('\r\n', '\n').replace('\r', '\n') if len(eol_message) == 0: # Return an empty value of the corresponding type if an empty message # is given, instead of metadata, which is the default gettext behavior. result = type(message)("") else: _default = _default or translation(settings.LANGUAGE_CODE) translation_object = getattr(_active, "value", _default) result = translation_object.gettext(eol_message) if isinstance(message, SafeData): return mark_safe(result) return result
Example #2
Source File: user_translations.py From online-judge with GNU Affero General Public License v3.0 | 6 votes |
def do_translate(message, translation_function): """Copied from django.utils.translation.trans_real""" # str() is allowing a bytestring message to remain bytestring on Python 2 eol_message = message.replace(str('\r\n'), str('\n')).replace(str('\r'), str('\n')) if len(eol_message) == 0: # Returns an empty value of the corresponding type if an empty message # is given, instead of metadata, which is the default gettext behavior. result = '' else: translation_object = translation(get_language()) result = getattr(translation_object, translation_function)(eol_message) if not isinstance(result, six.text_type): result = result.decode('utf-8') if isinstance(message, SafeData): return mark_safe(result) return result
Example #3
Source File: i18n.py From Hands-On-Application-Development-with-PyCharm with MIT License | 6 votes |
def render(self, context): self.filter_expression.var.translate = not self.noop if self.message_context: self.filter_expression.var.message_context = ( self.message_context.resolve(context)) output = self.filter_expression.resolve(context) value = render_value_in_context(output, context) # Restore percent signs. Percent signs in template text are doubled # so they are not interpreted as string format flags. is_safe = isinstance(value, SafeData) value = value.replace('%%', '%') value = mark_safe(value) if is_safe else value if self.asvar: context[self.asvar] = value return '' else: return value
Example #4
Source File: i18n.py From openhgsenti with Apache License 2.0 | 6 votes |
def render(self, context): self.filter_expression.var.translate = not self.noop if self.message_context: self.filter_expression.var.message_context = ( self.message_context.resolve(context)) output = self.filter_expression.resolve(context) value = render_value_in_context(output, context) # Restore percent signs. Percent signs in template text are doubled # so they are not interpreted as string format flags. is_safe = isinstance(value, SafeData) value = value.replace('%%', '%') value = mark_safe(value) if is_safe else value if self.asvar: context[self.asvar] = value return '' else: return value
Example #5
Source File: trans_real.py From Hands-On-Application-Development-with-PyCharm with MIT License | 6 votes |
def gettext(message): """ Translate the 'message' string. It uses the current thread to find the translation object to use. If no current translation is activated, the message will be run through the default translation object. """ global _default eol_message = message.replace('\r\n', '\n').replace('\r', '\n') if eol_message: _default = _default or translation(settings.LANGUAGE_CODE) translation_object = getattr(_active, "value", _default) result = translation_object.gettext(eol_message) else: # Return an empty value of the corresponding type if an empty message # is given, instead of metadata, which is the default gettext behavior. result = type(message)('') if isinstance(message, SafeData): return mark_safe(result) return result
Example #6
Source File: i18n.py From python2017 with MIT License | 6 votes |
def render(self, context): self.filter_expression.var.translate = not self.noop if self.message_context: self.filter_expression.var.message_context = ( self.message_context.resolve(context)) output = self.filter_expression.resolve(context) value = render_value_in_context(output, context) # Restore percent signs. Percent signs in template text are doubled # so they are not interpreted as string format flags. is_safe = isinstance(value, SafeData) value = value.replace('%%', '%') value = mark_safe(value) if is_safe else value if self.asvar: context[self.asvar] = value return '' else: return value
Example #7
Source File: defaultfilters.py From bioforum with MIT License | 6 votes |
def stringfilter(func): """ Decorator for filters which should only receive strings. The object passed as the first positional argument will be converted to a string. """ def _dec(*args, **kwargs): if args: args = list(args) args[0] = str(args[0]) if (isinstance(args[0], SafeData) and getattr(_dec._decorated_function, 'is_safe', False)): return mark_safe(func(*args, **kwargs)) return func(*args, **kwargs) # Include a reference to the real function (used to check original # arguments by the template parser, and to bear the 'is_safe' attribute # when multiple decorators are applied). _dec._decorated_function = getattr(func, '_decorated_function', func) return wraps(func)(_dec) ################### # STRINGS # ###################
Example #8
Source File: defaultfilters.py From Hands-On-Application-Development-with-PyCharm with MIT License | 6 votes |
def stringfilter(func): """ Decorator for filters which should only receive strings. The object passed as the first positional argument will be converted to a string. """ def _dec(*args, **kwargs): args = list(args) args[0] = str(args[0]) if (isinstance(args[0], SafeData) and getattr(_dec._decorated_function, 'is_safe', False)): return mark_safe(func(*args, **kwargs)) return func(*args, **kwargs) # Include a reference to the real function (used to check original # arguments by the template parser, and to bear the 'is_safe' attribute # when multiple decorators are applied). _dec._decorated_function = getattr(func, '_decorated_function', func) return wraps(func)(_dec) ################### # STRINGS # ###################
Example #9
Source File: debug.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def render(self, context): try: output = self.filter_expression.resolve(context) output = template_localtime(output, use_tz=context.use_tz) output = localize(output, use_l10n=context.use_l10n) output = force_text(output) except UnicodeDecodeError: return '' except Exception as e: if not hasattr(e, 'django_template_source'): e.django_template_source = self.source raise if (context.autoescape and not isinstance(output, SafeData)) or isinstance(output, EscapeData): return escape(output) else: return output
Example #10
Source File: i18n.py From bioforum with MIT License | 6 votes |
def render(self, context): self.filter_expression.var.translate = not self.noop if self.message_context: self.filter_expression.var.message_context = ( self.message_context.resolve(context)) output = self.filter_expression.resolve(context) value = render_value_in_context(output, context) # Restore percent signs. Percent signs in template text are doubled # so they are not interpreted as string format flags. is_safe = isinstance(value, SafeData) value = value.replace('%%', '%') value = mark_safe(value) if is_safe else value if self.asvar: context[self.asvar] = value return '' else: return value
Example #11
Source File: trans_real.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def do_translate(message, translation_function): """ Translates 'message' using the given 'translation_function' name -- which will be either gettext or ugettext. It uses the current thread to find the translation object to use. If no current translation is activated, the message will be run through the default translation object. """ global _default # str() is allowing a bytestring message to remain bytestring on Python 2 eol_message = message.replace(str('\r\n'), str('\n')).replace(str('\r'), str('\n')) t = getattr(_active, "value", None) if t is not None: result = getattr(t, translation_function)(eol_message) else: if _default is None: from django.conf import settings _default = translation(settings.LANGUAGE_CODE) result = getattr(_default, translation_function)(eol_message) if isinstance(message, SafeData): return mark_safe(result) return result
Example #12
Source File: i18n.py From python with Apache License 2.0 | 6 votes |
def render(self, context): self.filter_expression.var.translate = not self.noop if self.message_context: self.filter_expression.var.message_context = ( self.message_context.resolve(context)) output = self.filter_expression.resolve(context) value = render_value_in_context(output, context) # Restore percent signs. Percent signs in template text are doubled # so they are not interpreted as string format flags. is_safe = isinstance(value, SafeData) value = value.replace('%%', '%') value = mark_safe(value) if is_safe else value if self.asvar: context[self.asvar] = value return '' else: return value
Example #13
Source File: debug.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def render(self, context): try: output = self.filter_expression.resolve(context) output = template_localtime(output, use_tz=context.use_tz) output = localize(output, use_l10n=context.use_l10n) output = force_text(output) except UnicodeDecodeError: return '' except Exception as e: if not hasattr(e, 'django_template_source'): e.django_template_source = self.source raise if (context.autoescape and not isinstance(output, SafeData)) or isinstance(output, EscapeData): return conditional_escape(output) else: return output
Example #14
Source File: defaultfilters.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def linebreaksbr(value, autoescape=None): """ Converts all newlines in a piece of plain text to HTML line breaks (``<br />``). """ autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) if autoescape: value = escape(value) return mark_safe(value.replace('\n', '<br />'))
Example #15
Source File: defaultfilters.py From openhgsenti with Apache License 2.0 | 5 votes |
def linenumbers(value, autoescape=True): """Displays text with line numbers.""" lines = value.split('\n') # Find the maximum width of the line count, for use with zero padding # string format command width = six.text_type(len(six.text_type(len(lines)))) if not autoescape or isinstance(value, SafeData): for i, line in enumerate(lines): lines[i] = ("%0" + width + "d. %s") % (i + 1, line) else: for i, line in enumerate(lines): lines[i] = ("%0" + width + "d. %s") % (i + 1, escape(line)) return mark_safe('\n'.join(lines))
Example #16
Source File: defaultfilters.py From openhgsenti with Apache License 2.0 | 5 votes |
def linebreaks_filter(value, autoescape=True): """ Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (``<br />``) and a new line followed by a blank line becomes a paragraph break (``</p>``). """ autoescape = autoescape and not isinstance(value, SafeData) return mark_safe(linebreaks(value, autoescape))
Example #17
Source File: tests.py From django-admin-easy with MIT License | 5 votes |
def test_multidecorator(self): @easy.utils('safestring.mark_safe') @easy.filter('date', 'django', 'y-m-d') def field(self, obj): return datetime(2016, 6, 25) v = field(1, 1) self.assertEquals(v, '16-06-25') self.assertIsInstance(v, SafeData)
Example #18
Source File: test_widgets.py From django-antispam with MIT License | 5 votes |
def test_render_safe_html(self): html = self.widget.render('honeypot_field', '') self.assertIsInstance(html, SafeData)
Example #19
Source File: defaultfilters.py From openhgsenti with Apache License 2.0 | 5 votes |
def stringfilter(func): """ Decorator for filters which should only receive unicode objects. The object passed as the first positional argument will be converted to a unicode object. """ def _dec(*args, **kwargs): if args: args = list(args) args[0] = force_text(args[0]) if (isinstance(args[0], SafeData) and getattr(_dec._decorated_function, 'is_safe', False)): return mark_safe(func(*args, **kwargs)) return func(*args, **kwargs) # Include a reference to the real function (used to check original # arguments by the template parser, and to bear the 'is_safe' attribute # when multiple decorators are applied). _dec._decorated_function = getattr(func, '_decorated_function', func) return wraps(func)(_dec) ################### # STRINGS # ###################
Example #20
Source File: test_widgets.py From django-antispam with MIT License | 5 votes |
def test_render_return_html(self): html = self.widget.render('captcha', '1234') self.assertIsInstance(html, SafeData)
Example #21
Source File: trans_real.py From openhgsenti with Apache License 2.0 | 5 votes |
def do_translate(message, translation_function): """ Translates 'message' using the given 'translation_function' name -- which will be either gettext or ugettext. It uses the current thread to find the translation object to use. If no current translation is activated, the message will be run through the default translation object. """ global _default # str() is allowing a bytestring message to remain bytestring on Python 2 eol_message = message.replace(str('\r\n'), str('\n')).replace(str('\r'), str('\n')) if len(eol_message) == 0: # Returns an empty value of the corresponding type if an empty message # is given, instead of metadata, which is the default gettext behavior. result = type(message)("") else: _default = _default or translation(settings.LANGUAGE_CODE) translation_object = getattr(_active, "value", _default) result = getattr(translation_object, translation_function)(eol_message) if isinstance(message, SafeData): return mark_safe(result) return result
Example #22
Source File: html.py From openhgsenti with Apache License 2.0 | 5 votes |
def conditional_escape(text): """ Similar to escape(), except that it doesn't operate on pre-escaped strings. This function relies on the __html__ convention used both by Django's SafeData class and by third-party libraries like markupsafe. """ if hasattr(text, '__html__'): return text.__html__() else: return escape(text)
Example #23
Source File: defaultfilters.py From python with Apache License 2.0 | 5 votes |
def linebreaks_filter(value, autoescape=True): """ Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (``<br />``) and a new line followed by a blank line becomes a paragraph break (``</p>``). """ autoescape = autoescape and not isinstance(value, SafeData) return mark_safe(linebreaks(value, autoescape))
Example #24
Source File: defaultfilters.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def linebreaks_filter(value, autoescape=None): """ Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (``<br />``) and a new line followed by a blank line becomes a paragraph break (``</p>``). """ autoescape = autoescape and not isinstance(value, SafeData) return mark_safe(linebreaks(value, autoescape))
Example #25
Source File: defaultfilters.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def cut(value, arg): """ Removes all values of arg from the given string. """ safe = isinstance(value, SafeData) value = value.replace(arg, '') if safe and arg != ';': return mark_safe(value) return value ################### # HTML STRINGS # ###################
Example #26
Source File: defaultfilters.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def stringfilter(func): """ Decorator for filters which should only receive unicode objects. The object passed as the first positional argument will be converted to a unicode object. """ def _dec(*args, **kwargs): if args: args = list(args) args[0] = force_text(args[0]) if (isinstance(args[0], SafeData) and getattr(_dec._decorated_function, 'is_safe', False)): return mark_safe(func(*args, **kwargs)) return func(*args, **kwargs) # Include a reference to the real function (used to check original # arguments by the template parser, and to bear the 'is_safe' attribute # when multiple decorators are applied). _dec._decorated_function = getattr(func, '_decorated_function', func) for attr in ('is_safe', 'needs_autoescape'): if hasattr(func, attr): import warnings warnings.warn("Setting the %s attribute of a template filter " "function is deprecated; use @register.filter(%s=%s) " "instead" % (attr, attr, getattr(func, attr)), DeprecationWarning) setattr(_dec, attr, getattr(func, attr)) return wraps(func)(_dec) ################### # STRINGS # ###################
Example #27
Source File: trans_null.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def gettext(message): result = TECHNICAL_ID_MAP.get(message, message) if isinstance(message, SafeData): return mark_safe(result) return result
Example #28
Source File: html.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def conditional_escape(text): """ Similar to escape(), except that it doesn't operate on pre-escaped strings. """ if isinstance(text, SafeData): return text else: return escape(text)
Example #29
Source File: defaultfilters.py From python with Apache License 2.0 | 5 votes |
def linebreaksbr(value, autoescape=True): """ Converts all newlines in a piece of plain text to HTML line breaks (``<br />``). """ autoescape = autoescape and not isinstance(value, SafeData) value = normalize_newlines(value) if autoescape: value = escape(value) return mark_safe(value.replace('\n', '<br />'))
Example #30
Source File: test_force_escape.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_escape(self): escaped = force_escape('<some html & special characters > here') self.assertEqual(escaped, '<some html & special characters > here') self.assertIsInstance(escaped, SafeData)