class SafeText(six.text_type, SafeData): """ A unicode (Python 2) / str (Python 3) subclass that has been specifically marked as "safe" for HTML output purposes. """ __new__ = allow_lazy(six.text_type.__new__, six.text_type) def __add__(self, rhs): """ Concatenating a safe unicode string with another safe byte string or safe unicode string is safe. Otherwise, the result is no longer safe. """ t = super(SafeText, self).__add__(rhs) if isinstance(rhs, SafeData): return SafeText(t) return t def _proxy_method(self, *args, **kwargs): """ Wrap a call to a normal unicode method up so that we return safe results. The method that is being wrapped is passed in the 'method' argument. """ method = kwargs.pop('method') data = method(self, *args, **kwargs) if isinstance(data, bytes): return SafeBytes(data) else: return SafeText(data) encode = curry(_proxy_method, method=six.text_type.encode)
def test_deprecated_allow_lazy(self): with self.assertRaises(RemovedInDjango20Warning): def noop_text(text): return force_text(text) noop_text = allow_lazy(noop_text, six.text_type) rendered = noop_text(ugettext_lazy("I am a text")) self.assertEqual(type(rendered), six.text_type) self.assertEqual(rendered, "I am a text")
class Truncator(object): "A simplified version of django.utils.text.Truncator" def __init__(self, text): self.text = text def words(self, num): s = truncate_words(self.text, num) if s.endswith(' ...'): s = s.replace(' ...', '...') return s words = allow_lazy(words)
class ZealousSpacelessNode(template.Node): def __init__(self, nodelist): self.nodelist = nodelist def render(self, context): return self.strip_spaces_between_tags( self.nodelist.render(context).strip()) def strip_spaces_between_tags(self, value): value = re.sub(r'\s+<', '<', force_unicode(value)) value = re.sub(r'>\s+', '>', force_unicode(value)) return value strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, unicode)
r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join([re.escape(x) for x in DOTS]), re.DOTALL) trailing_empty_content_re = re.compile( r'(?:<p>(?: |\s|<br \/>)*?</p>\s*)+\Z') def escape(text): """ Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML. """ return mark_safe( force_text(text).replace('&', '&').replace('<', '<').replace( '>', '>').replace('"', '"').replace("'", ''')) escape = allow_lazy(escape, six.text_type) _js_escapes = { ord('\\'): '\\u005C', ord('\''): '\\u0027', ord('"'): '\\u0022', ord('>'): '\\u003E', ord('<'): '\\u003C', ord('&'): '\\u0026', ord('='): '\\u003D', ord('-'): '\\u002D', ord(';'): '\\u003B', ord('\u2028'): '\\u2028', ord('\u2029'): '\\u2029' }
(middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))): url = urlquote('http://%s' % middle, safe='/&=:;#?+*') elif '@' in middle and not ':' in middle and simple_email_re.match(middle): url = 'mailto:%s' % middle nofollow_attr = '' # Make link. if url: trimmed = trim_url(middle) if autoescape and not safe_input: lead, trail = escape(lead), escape(trail) url, trimmed = escape(url), escape(trimmed) if is_image(url): middle = '<a href="%s"><img class="image-inside-review" src="%s" alt="" /></a>' % (url, url) else: middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed) words[i] = mark_safe('%s%s%s' % (lead, middle, trail)) else: if safe_input: words[i] = mark_safe(word) elif autoescape: words[i] = escape(word) elif safe_input: words[i] = mark_safe(word) elif autoescape: words[i] = escape(word) return u''.join(words) urlimagize = allow_lazy(urlimagize, unicode) register.filter(urlimagize)
import urllib.request, urllib.parse, urllib.error from email.Utils import formatdate from django.utils.encoding import smart_str, force_unicode from django.utils.functional import allow_lazy def urlquote(url, safe='/'): """ A version of Python's urllib.quote() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_unicode(urllib.parse.quote(smart_str(url), safe)) urlquote = allow_lazy(urlquote, str) def urlquote_plus(url, safe=''): """ A version of Python's urllib.quote_plus() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_unicode(urllib.parse.quote_plus(smart_str(url), safe)) urlquote_plus = allow_lazy(urlquote_plus, str) def urlencode(query, doseq=0): """ A version of Python's urllib.urlencode() function that can operate on unicode strings. The parameters are first case to UTF-8 encoded strings and
from django.utils.encoding import force_text from django.utils.functional import allow_lazy, SimpleLazyObject from django.utils import six from django.utils.six.moves import html_entities from django.utils.translation import ugettext_lazy, ugettext as _, pgettext from django.utils.safestring import SafeText, mark_safe if six.PY2: # Import force_unicode even though this module doesn't use it, because some # people rely on it being here. from django.utils.encoding import force_unicode # NOQA # Capitalizes the first letter of a string. capfirst = lambda x: x and force_text(x)[0].upper() + force_text(x)[1:] capfirst = allow_lazy(capfirst, six.text_type) # Set up regular expressions re_words = re.compile(r'<.*?>|((?:\w[-\w]*|&.*?;)+)', re.U | re.S) re_chars = re.compile(r'<.*?>|(.)', re.U | re.S) re_tag = re.compile(r'<(/)?([^ ]+?)(?:(\s*/)| .*?)?>', re.S) re_newlines = re.compile(r'\r\n|\r') # Used in normalize_newlines re_camel_case = re.compile(r'(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))') def wrap(text, width): """ A word-wrap function that preserves existing line breaks. Expects that existing line breaks are posix newlines. All white space is preserved except added line breaks consume the space on
from cStringIO import StringIO # PYTHON3: from io import BytesIO from django import forms from django.forms import widgets from django.conf import settings from django.utils.translation import ugettext_lazy as _ try: from django.utils.functional import keep_lazy allow_lazy = lambda func, *resultclasses: keep_lazy(*resultclasses)(func) except: from django.utils.functional import allow_lazy from django.core.files.uploadhandler import FileUploadHandler from django.core.files.uploadedfile import UploadedFile format_lazy = allow_lazy(lambda x, y: x % y, unicode) class PhotographUploadHandler(FileUploadHandler): """ File upload handler to stream photograph uploads into memory. """ def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None): # Check the content-length header to see if we should keep the data. if content_length > settings.CMBARTER_MAX_IMAGE_SIZE + 10000: self.__keep = False else: self.__keep = True def new_file(self, field_name, file_name, content_type, *args, **kwargs):
walker = treewalkers.getTreeWalker("dom") stream = walker(dom_tree) s = serializer.HTMLSerializer(omit_optional_tags=False, quote_attr_values=True) output_generator = s.serialize(stream) return u''.join(output_generator) def unescape(text): """ Returns the given text with ampersands, quotes and angle brackets decoded for use in URLs. This function undoes what django.utils.html.escape() does """ return text.replace(''', "'").replace('"', '"').replace( '>', '>').replace('<', '<').replace('&', '&') def remove_tags(html, tags): """Returns the given HTML sanitized, and with the given tags removed.""" allowed = set(acceptable_elements) - set([t.lower() for t in tags]) return bleach.clean(html, tags=allowed) remove_tags = allow_lazy(remove_tags, six.text_type) def clean_html(html): return bleach.clean(html)
import re from django.conf import settings from django.utils.encoding import force_unicode from django.utils.functional import allow_lazy from django.utils.translation import ugettext_lazy from htmlentitydefs import name2codepoint # Capitalizes the first letter of a string. capfirst = lambda x: x and force_unicode(x)[0].upper() + force_unicode(x)[1:] capfirst = allow_lazy(capfirst, unicode) def wrap(text, width): """ A word-wrap function that preserves existing line breaks and most spaces in the text. Expects that existing line breaks are posix newlines. """ text = force_unicode(text) def _generator(): it = iter(text.split(' ')) word = it.next() yield word pos = len(word) - word.rfind('\n') - 1 for word in it: if "\n" in word: lines = word.split('\n') else: lines = (word, ) pos += len(lines[0]) + 1 if pos > width:
from django import template from django.utils import six from django.utils.encoding import force_text from django.utils.functional import allow_lazy from django.utils.safestring import SafeText, mark_safe from django.template.defaultfilters import stringfilter register = template.Library() _slack_escapes = { ord('&'): u'&', ord('<'): u'<', ord('>'): u'>', } @register.filter(is_safe=True) @stringfilter def escapeslack(value): """ Returns the given text with ampersands and angle brackets encoded for use in the Slack API, per the Slack API documentation: <https://api.slack.com/docs/formatting#how_to_escape_characters> This is based on django.template.defaultfilters.escapejs. """ return mark_safe(force_text(value).translate(_slack_escapes)) escapeslack = allow_lazy(escapeslack, six.text_type, SafeText)
>>> print(cut_string('<p>Zlatý valoun</p>', 8)) Zlatý... """ string = force_unicode(string) from django.utils.html import strip_tags text = decode_entities(strip_tags(string)) if len(text) > length: final_length = length - len(end_text) try: text = '%s%s' % (text[: text.rindex(' ', 0, final_length)], end_text) except ValueError: text = '%s%s' % (text[:final_length], end_text) return text cut_string = allow_lazy(cut_string, unicode) def decode_entities(string): """ Převede html entity na unicode znaky. Pro převod používá knihovnu BeautifulSoup Použití: >>> print(decode_entities('ý')) ý """ from BeautifulSoup import BeautifulStoneSoup text = unicode(BeautifulStoneSoup(string,convertEntities=BeautifulStoneSoup.HTML_ENTITIES)) return text decode_entities = allow_lazy(decode_entities, unicode)
from django.utils.encoding import force_unicode from django.utils.functional import allow_lazy def truncate_letters(s, num): """ truncates a string to a number of letters, similar to truncate_words """ s = force_unicode(s) length = int(num) if len(s)>length: s = s[:length] if not s.endswith('...'): s += '...' return s truncate_letters = allow_lazy(truncate_letters, unicode)
return [l[0] for l in settings.LANGUAGES] def get_default_language(): return settings.LANGUAGE_CODE[:2].lower() def json_dumps(data): return json.dumps(data, cls=DjangoJSONEncoder, ensure_ascii=False) def slugify(value): return _slugify(force_text(value)) slugify = allow_lazy(slugify, six.text_type) # @pytils_translit.returns(str) def safe_translify(in_string): """ Translify russian text. When string doesn't translify completely, ascii string with non-ascii chars ignored is returned. @param in_string: input string @type in_string: C{unicode} @return: transliterated string @rtype: C{str} """
from __future__ import unicode_literals from __future__ import absolute_import import re import unicodedata from django.template.defaultfilters import register, stringfilter from django.utils import six from django.utils.functional import allow_lazy from django.utils.safestring import mark_safe def slugify_symbol(value): value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') value = re.sub('[^$`\w\s-]', '', value).strip().lower() return mark_safe(re.sub('[-\s`]+', '-', value)) slugify_symbol = allow_lazy(slugify_symbol, six.text_type) @register.filter(is_safe=True) @stringfilter def slugify(value): """ Converts to lowercase, removes non-word characters apart from '$', and converts spaces to hyphens. Also strips leading and trailing whitespace. Based on the Django version, but modified to preserve '$'. """ return slugify_symbol(value)
## managing trading partners. ## from cStringIO import StringIO # PYTHON3: from io import BytesIO from django import forms from django.forms import widgets from django.conf import settings from django.utils.translation import ugettext_lazy as _ try: from django.utils.functional import keep_lazy allow_lazy = lambda func, *resultclasses: keep_lazy(*resultclasses)(func) except: from django.utils.functional import allow_lazy from django.core.files.uploadhandler import FileUploadHandler from django.core.files.uploadedfile import UploadedFile format_lazy = allow_lazy(lambda x, y: x % y, unicode) class PhotographUploadHandler(FileUploadHandler): """ File upload handler to stream photograph uploads into memory. """ def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None): # Check the content-length header to see if we should keep the data. if content_length > settings.CMBARTER_MAX_IMAGE_SIZE + 10000: self.__keep = False
from django import forms from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext from django.utils.functional import lazy, allow_lazy from .models import MembershipApplication from django.conf import settings def rules_accepted_proxy(msg): return msg % settings.APPLICATION_RULES_URL rules_accepted_proxy = allow_lazy(rules_accepted_proxy, str) class ApplicationForm(forms.ModelForm): rules_accepted = forms.BooleanField( required=True, label=rules_accepted_proxy( _("I have read and accept <a href=\"%s\" target=\"_blank\">the rules</a>" ))) required_css_class = 'required' class Meta: model = MembershipApplication fields = [ 'fname', 'lname', 'city', 'email', 'phone',
RFC1123_DATE = re.compile(r'^\w{3}, %s %s %s %s GMT$' % (__D, __M, __Y, __T)) RFC850_DATE = re.compile(r'^\w{6,9}, %s-%s-%s %s GMT$' % (__D, __M, __Y2, __T)) ASCTIME_DATE = re.compile(r'^\w{3} %s %s %s %s$' % (__M, __D2, __T, __Y)) def urlquote(url, safe='/'): """ A version of Python's urllib.quote() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_text(urllib_parse.quote(force_str(url), force_str(safe))) urlquote = allow_lazy(urlquote, six.text_type) def urlquote_plus(url, safe=''): """ A version of Python's urllib.quote_plus() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_text(urllib_parse.quote_plus(force_str(url), force_str(safe))) urlquote_plus = allow_lazy(urlquote_plus, six.text_type)
def escape(text): """ Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML. """ return mark_safe( force_unicode(text) .replace("&", "&") .replace("<", "<") .replace(">", ">") .replace('"', """) .replace("'", "'") ) escape = allow_lazy(escape, six.text_type) _base_js_escapes = ( ("\\", "\\u005C"), ("'", "\\u0027"), ('"', "\\u0022"), (">", "\\u003E"), ("<", "\\u003C"), ("&", "\\u0026"), ("=", "\\u003D"), ("-", "\\u002D"), (";", "\\u003B"), ("\u2028", "\\u2028"), ("\u2029", "\\u2029"), )
RE_FIND_PRE = re.compile(r'<pre(?:\s[^>]*)?>(?:.*?)</pre>', flags=re.S | re.M | re.I) RE_RESTORE_PRE = re.compile(r'%preplaceholder_(\d+)%') RE_SPACES = re.compile(r'>\s+<') def strip_spaces_between_tags_except_pre(value): matches = [] def replacement(match): matches.append(match.group(0)[1:-1]) # save the whole match without leading "<" and trailing ">" return '<%%preplaceholder_%d%%>' % (len(matches) - 1) # add "<" and ">" to preserve space stripping value = RE_FIND_PRE.sub(replacement, force_text(value)) value = RE_SPACES.sub('><', force_text(value)) return RE_RESTORE_PRE.sub(lambda match: matches[int(match.group(1))], value) strip_spaces_between_tags_except_pre = allow_lazy(strip_spaces_between_tags_except_pre, six.text_type) class SpacelessExceptPreNode(Node): def __init__(self, nodelist): self.nodelist = nodelist def render(self, context): return strip_spaces_between_tags_except_pre(self.nodelist.render(context).strip()) @register.tag def spaceless_except_pre(parser, token): """Remove whitespace between HTML tags, including tab and newline characters except content between <pre>""" nodelist = parser.parse(('endspaceless_except_pre',)) parser.delete_first_token()
""" Account constants """ from django.utils.translation import ugettext_lazy as _ # In Django 1.11, there's django.utils.text.format_lazy. from django.utils.functional import allow_lazy format_lazy = allow_lazy(lambda s, *a, **kw: s.format(*a, **kw), unicode) # The minimum and maximum length for the name ("full name") account field NAME_MIN_LENGTH = 2 NAME_MAX_LENGTH = 255 # The minimum and maximum length for the username account field USERNAME_MIN_LENGTH = 2 USERNAME_MAX_LENGTH = 30 # The minimum and maximum length for the email account field EMAIL_MIN_LENGTH = 3 EMAIL_MAX_LENGTH = 254 # Limit per RFCs is 254 ACCOUNT_VISIBILITY_PREF_KEY = 'account_privacy' # Indicates the user's preference that all users can view the shareable fields in their account information. ALL_USERS_VISIBILITY = 'all_users' # Indicates the user's preference that all their account information be private. PRIVATE_VISIBILITY = 'private'
Usage: .. code-block:: python :linenos: >>> snakify('polls-report May 1, 2016') u'polls_report_may_1_2016' """ value = force_text(value) value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')\ .decode('ascii') value = re.sub('[^\w\s-]', '', value).strip().lower() return mark_safe(re.sub('[-\s]+', '_', value)) snakify = allow_lazy(snakify, six.text_type, SafeText) def get_object_or_none(klass, *args, **kwargs): """ Method returns the queried object or None if the object does not exist Usage: .. code-block:: python :linenos: >>> get_object_or_none(Poll, pk=1) None >>> get_object_or_none(Poll, {'pk': 1, 'user': 1}) None >>> get_object_or_none(Poll, {'pk': 2})
__Y = r'(?P<year>\d{4})' __Y2 = r'(?P<year>\d{2})' __T = r'(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})' RFC1123_DATE = re.compile(r'^\w{3}, %s %s %s %s GMT$' % (__D, __M, __Y, __T)) RFC850_DATE = re.compile(r'^\w{6,9}, %s-%s-%s %s GMT$' % (__D, __M, __Y2, __T)) ASCTIME_DATE = re.compile(r'^\w{3} %s %s %s %s$' % (__M, __D2, __T, __Y)) def urlquote(url, safe='/'): """ A version of Python's urllib.quote() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_text(urllib_parse.quote(force_str(url), force_str(safe))) urlquote = allow_lazy(urlquote, six.text_type) def urlquote_plus(url, safe=''): """ A version of Python's urllib.quote_plus() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_text(urllib_parse.quote_plus(force_str(url), force_str(safe))) urlquote_plus = allow_lazy(urlquote_plus, six.text_type) def urlunquote(quoted_url): """ A wrapper for Python's urllib.unquote() function that can operate on the result of django.utils.http.urlquote().
word_split_re = re.compile(r'(\s+)') simple_url_re = re.compile(r'^https?://\[?\w', re.IGNORECASE) simple_url_2_re = re.compile(r'^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net|org)$', re.IGNORECASE) simple_email_re = re.compile(r'^\S+@\S+\.\S+$') link_target_attribute_re = re.compile(r'(<a [^>]*?)target=[^\s>]+') html_gunk_re = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)', re.IGNORECASE) hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join(re.escape(x) for x in DOTS), re.DOTALL) trailing_empty_content_re = re.compile(r'(?:<p>(?: |\s|<br \/>)*?</p>\s*)+\Z') def escape(text): """ Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML. """ return mark_safe(force_text(text).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')) escape = allow_lazy(escape, six.text_type) _js_escapes = { ord('\\'): '\\u005C', ord('\''): '\\u0027', ord('"'): '\\u0022', ord('>'): '\\u003E', ord('<'): '\\u003C', ord('&'): '\\u0026', ord('='): '\\u003D', ord('-'): '\\u002D', ord(';'): '\\u003B', ord('\u2028'): '\\u2028', ord('\u2029'): '\\u2029' }
'|'.join([re.escape(x) for x in DOTS]), re.DOTALL) trailing_empty_content_re = re.compile( r'(?:<p>(?: |\s|<br \/>)*?</p>\s*)+\Z') del x # Temporary variable def escape(html): """ Returns the given HTML with ampersands, quotes and angle brackets encoded. """ return mark_safe( force_unicode(html).replace('&', '&').replace('<', '<').replace( '>', '>').replace('"', '"').replace("'", ''')) escape = allow_lazy(escape, unicode) _base_js_escapes = (('\\', '\\u005C'), ('\'', '\\u0027'), ('"', '\\u0022'), ('>', '\\u003E'), ('<', '\\u003C'), ('&', '\\u0026'), ('=', '\\u003D'), ('-', '\\u002D'), (';', '\\u003B'), ('\u2028', '\\u2028'), ('\u2029', '\\u2029')) # Escape every ASCII character with a value less than 32. _js_escapes = (_base_js_escapes + tuple([('%c' % z, '\\u%04X' % z) for z in range(32)])) def escapejs(value): """Hex encodes characters for use in JavaScript strings.""" for bad, good in _js_escapes: value = mark_safe(force_unicode(value).replace(bad, good))
import re from django.conf import settings from django.utils.encoding import force_unicode from django.utils.functional import allow_lazy from django.utils.translation import ugettext_lazy from htmlentitydefs import name2codepoint # Capitalizes the first letter of a string. capfirst = lambda x: x and force_unicode(x)[0].upper() + force_unicode(x)[1:] capfirst = allow_lazy(capfirst, unicode) def wrap(text, width): """ A word-wrap function that preserves existing line breaks and most spaces in the text. Expects that existing line breaks are posix newlines. """ text = force_unicode(text) def _generator(): it = iter(text.split(' ')) word = it.next() yield word pos = len(word) - word.rfind('\n') - 1 for word in it: if "\n" in word: lines = word.split('\n') else: lines = (word,) pos += len(lines[0]) + 1 if pos > width: yield '\n' pos = len(lines[-1])
fully_decorated = cache_page(60*15)(fully_decorated) fully_decorated = cache_control(private=True)(fully_decorated) fully_decorated = never_cache(fully_decorated) # django.contrib.auth.decorators # Apply user_passes_test twice to check #9474 fully_decorated = user_passes_test(lambda u:True)(fully_decorated) fully_decorated = login_required(fully_decorated) fully_decorated = permission_required('change_world')(fully_decorated) # django.contrib.admin.views.decorators fully_decorated = staff_member_required(fully_decorated) # django.utils.functional fully_decorated = memoize(fully_decorated, {}, 1) fully_decorated = allow_lazy(fully_decorated) fully_decorated = lazy(fully_decorated) class DecoratorsTest(TestCase): def test_attributes(self): """ Tests that django decorators set certain attributes of the wrapped function. """ # Only check __name__ on Python 2.4 or later since __name__ can't be # assigned to in earlier Python versions. if version_info[0] >= 2 and version_info[1] >= 4: self.assertEquals(fully_decorated.__name__, 'fully_decorated') self.assertEquals(fully_decorated.__doc__, 'Expected __doc__')
import re from django import template from django.template.base import Node from django.utils.functional import allow_lazy register = template.Library() def strip_empty_lines(value): """Return the given HTML with empty and all-whitespace lines removed.""" return re.sub(r'\n[ \t]*(?=\n)', '', value) strip_empty_lines = allow_lazy(strip_empty_lines, str) @register.tag def gapless(parser, token): """ Remove blank lines between `{% gapless %}` and `{% endgapless %}` """ nodelist = parser.parse(('endgapless',)) parser.delete_first_token() return GaplessNode(nodelist) class GaplessNode(Node): """ Closely modeled after: https://djangosnippets.org/snippets/569/ """ def __init__(self, nodelist): self.nodelist = nodelist
from django.utils.encoding import force_text from django.utils.functional import allow_lazy, SimpleLazyObject from django.utils import six from django.utils.six.moves import html_entities from django.utils.translation import ugettext_lazy, ugettext as _, pgettext from django.utils.safestring import mark_safe if not six.PY3: # Import force_unicode even though this module doesn't use it, because some # people rely on it being here. from django.utils.encoding import force_unicode # Capitalizes the first letter of a string. capfirst = lambda x: x and force_text(x)[0].upper() + force_text(x)[1:] capfirst = allow_lazy(capfirst, six.text_type) # Set up regular expressions re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U|re.S) re_tag = re.compile(r'<(/)?([^ ]+?)(?: (/)| .*?)?>', re.S) def wrap(text, width): """ A word-wrap function that preserves existing line breaks and most spaces in the text. Expects that existing line breaks are posix newlines. """ text = force_text(text) def _generator(): it = iter(text.split(' ')) word = next(it)
paras = re.split('\n{2,}', value) newlist = [] for p in paras: p = p.replace('\n', '<br />') if not needle_in_haystack("<h2>", p): p = u'<p>%s</p>' % p newlist.append(p) # OLD CODE from linebreaks: #paras = [u'<p>%s</p>' % p.replace('\n', '<br />') for p in paras] return u'\n\n'.join(newlist) smartbreaks = allow_lazy(smartbreaks, unicode) def smartlinebreaks(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(smartbreaks(value, autoescape)) smartlinebreaks.is_safe = True smartlinebreaks.needs_autoescape = True #smartlinebreaks = stringfilter(smartlinebreaks)
import requests, re, unicodedata from django.conf import settings from django.utils import six from django.utils.functional import allow_lazy from django.utils.safestring import mark_safe def get_place_details(place_id): property_search_prefix = "https://maps.googleapis.com/maps/api/place/details/" data_type = "json" # build and execute property search api call url = property_search_prefix + data_type + '?placeid=' + place_id + '&key=' + settings.GOOGLE_API_KEY data = requests.get(url).json() return data def slugify_no_hyphen(value): """ Converts to lowercase, removes non-word characters (alphanumerics and underscores) and removes spaces. Also strips leading and trailing whitespace. Copied from django.utils.text and modified hyphen replace """ value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') value = re.sub('[^\w\s-]', '', value).strip().lower() return mark_safe(re.sub('[-\s]+', '', value)) slugify_no_hyphen = allow_lazy(slugify_no_hyphen, six.text_type)
value = force_text(unicode_func(value)) value = unicodedata.normalize('NFKC', value.strip()) value = re.sub(to_und_rgx, '_', value) value = re.sub(slugify_rgx, '-', value) value = re.sub(multi_dash_rgx, '-', value) value = re.sub(dash_und_rgx, '_', value) value = re.sub(und_dash_rgx, '_', value) value = re.sub(starting_chars_rgx, '', value) value = re.sub(ending_chars_rgx, '', value) return mark_safe(value) if DJANGO_VERSION < (1, 10): from django.utils.functional import allow_lazy default_slugify = allow_lazy(default_slugify, six.text_type, SafeText) else: from django.utils.functional import keep_lazy default_slugify = keep_lazy(six.text_type, SafeText)(default_slugify) # DJANGO BACKWARDS-COMPATIBLE PATTERNS def patterns(prefix, *args): if DJANGO_VERSION < (1, 9): from django.conf.urls import patterns as django_patterns return django_patterns(prefix, *args) elif prefix != '': raise Exception("You need to update your URLConf to be a list of URL " "objects") else:
class Truncator(SimpleLazyObject): """ An object used to truncate text, either by characters or words. """ def __init__(self, text): super(Truncator, self).__init__(lambda: force_text(text)) def add_truncation_text(self, text, truncate=None): if truncate is None: truncate = pgettext( 'String to return when truncating text', '%(truncated_text)s...') truncate = force_text(truncate) if '%(truncated_text)s' in truncate: return truncate % {'truncated_text': text} # The truncation text didn't contain the %(truncated_text)s string # replacement argument so just append it to the text. if text.endswith(truncate): # But don't append the truncation text if the current text already # ends in this. return text return '%s%s' % (text, truncate) def chars(self, num, truncate=None, html=False): """ Returns the text truncated to be no longer than the specified number of characters. Takes an optional argument of what should be used to notify that the string has been truncated, defaulting to a translatable string of an ellipsis (...). """ length = int(num) text = unicodedata.normalize('NFC', self._wrapped) # Calculate the length to truncate to (max length - end_text length) truncate_len = length for char in self.add_truncation_text('', truncate): if not unicodedata.combining(char): truncate_len -= 1 if truncate_len == 0: break if html: return self._truncate_html(length, truncate, text, truncate_len, False) return self._text_chars(length, truncate, text, truncate_len) chars = allow_lazy(chars) def _text_chars(self, length, truncate, text, truncate_len): """ Truncates a string after a certain number of chars. """ s_len = 0 end_index = None for i, char in enumerate(text): if unicodedata.combining(char): # Don't consider combining characters # as adding to the string length continue s_len += 1 if end_index is None and s_len > truncate_len: end_index = i if s_len > length: # Return the truncated string return self.add_truncation_text(text[:end_index or 0], truncate) # Return the original string since no truncation was necessary return text def words(self, num, truncate=None, html=False): """ Truncates a string after a certain number of words. Takes an optional argument of what should be used to notify that the string has been truncated, defaulting to ellipsis (...). """ length = int(num) if html: return self._truncate_html(length, truncate, self._wrapped, length, True) return self._text_words(length, truncate) words = allow_lazy(words) def _text_words(self, length, truncate): """ Truncates a string after a certain number of words. Newlines in the string will be stripped. """ words = self._wrapped.split() if len(words) > length: words = words[:length] return self.add_truncation_text(' '.join(words), truncate) return ' '.join(words) def _truncate_html(self, length, truncate, text, truncate_len, words): """ Truncates HTML to a certain number of chars (not counting tags and comments), or, if words is True, then to a certain number of words. Closes opened tags if they were correctly closed in the given HTML. Newlines in the HTML are preserved. """ if words and length <= 0: return '' html4_singlets = ( 'br', 'col', 'link', 'base', 'img', 'param', 'area', 'hr', 'input' ) # Count non-HTML chars/words and keep note of open tags pos = 0 end_text_pos = 0 current_len = 0 open_tags = [] regex = re_words if words else re_chars while current_len <= length: m = regex.search(text, pos) if not m: # Checked through whole string break pos = m.end(0) if m.group(1): # It's an actual non-HTML word or char current_len += 1 if current_len == truncate_len: end_text_pos = pos continue # Check for tag tag = re_tag.match(m.group(0)) if not tag or current_len >= truncate_len: # Don't worry about non tags or tags after our truncate point continue closing_tag, tagname, self_closing = tag.groups() # Element names are always case-insensitive tagname = tagname.lower() if self_closing or tagname in html4_singlets: pass elif closing_tag: # Check for match in open tags list try: i = open_tags.index(tagname) except ValueError: pass else: # SGML: An end tag closes, back to the matching start tag, # all unclosed intervening start tags with omitted end tags open_tags = open_tags[i + 1:] else: # Add it to the start of the open tags list open_tags.insert(0, tagname) if current_len <= length: return text out = text[:end_text_pos] truncate_text = self.add_truncation_text('', truncate) if truncate_text: out += truncate_text # Close any tags still open for tag in open_tags: out += '</%s>' % tag # Return string return out
import string import json from itertools import chain from django import forms from django.core.exceptions import ValidationError from django.core.validators import RegexValidator from django.utils.translation import ugettext_lazy as _, pgettext_lazy from django.utils.functional import allow_lazy from django.template.defaultfilters import capfirst from .decorators import to_list from . import app_settings capfirst = allow_lazy(capfirst, unicode) DEFAULT_TITLE_CHOICES = ( (pgettext_lazy('title', 'Mrs'), pgettext_lazy('title', 'Mrs')), (pgettext_lazy('title', 'Mr'), pgettext_lazy('title', 'Mr')), ) def get_title_choices(): return app_settings.A2_ATTRIBUTE_KIND_TITLE_CHOICES or DEFAULT_TITLE_CHOICES DEFAULT_ATTRIBUTE_KINDS = [ { 'label': _('string'), 'name': 'string', 'field_class': forms.CharField, },
def order_by(queryset, args): args = [x.strip() for x in args.split(',')] return queryset.order_by(*args) @register.filter def classpathlinebreaks(value): """ Replaces separator in python class path a single newline becomes an HTML line break (``<br />``). """ value = re.sub(r'\[|\]|\'', ',', force_unicode(value)) # normalize newlines paras = re.split(',', value) paras = [u'<p>%s</p>' % p.strip().replace(',', '<br />') for p in paras] return u'\n\n'.join(paras) classpathlinebreaks = allow_lazy(classpathlinebreaks, unicode) @register.filter def short_name(value): """ Replaces separator in python class path a single newline becomes an HTML line break (``<br />``). """ l = len(force_unicode(value)) if l > 65: name = value[:35] + "..." + value[l - 25:] else: name = value return unicode(name) short_name = allow_lazy(short_name, unicode)
# -*- coding: utf-8 -*- import re from django import template from django.utils.encoding import force_unicode from django.utils.functional import allow_lazy register = template.Library() def selectively_remove_spaces_between_tags(value): html = re.sub(r'>\s+<', '><', force_unicode(value)) html = re.sub(r'</button><', '</button> <', force_unicode(html)) return re.sub(r'(<input[^>]+>)<', r'\1 <', force_unicode(html)) selectively_remove_spaces_between_tags = allow_lazy( selectively_remove_spaces_between_tags, unicode) class SpecialSpacelessNode(template.Node): def __init__(self, nodelist): self.nodelist = nodelist def render(self, context): return selectively_remove_spaces_between_tags( self.nodelist.render(context).strip() ) @register.tag def specialspaceless(parser, token):
url = smart_urlquote('http://%s' % middle) elif not ':' in middle and simple_email_re.match(middle): local, domain = middle.rsplit('@', 1) try: domain = domain.encode('idna') except UnicodeError: continue url = 'mailto:%s@%s' % (local, domain) nofollow_attr = '' # Make link. if url: if autoescape and not safe_input: lead, trail = escape(lead), escape(trail) url, replaceText = escape(url), escape(replaceText) middle = '<a href="%(url)s" title="%(url)s"%(attr)s>%(text)s</a>' % {'url': url, 'attr': nofollow_attr, 'text': replaceText} words[i] = mark_safe('%s%s%s' % (lead, middle, trail)) else: if safe_input: words[i] = mark_safe(word) elif autoescape: words[i] = escape(word) elif safe_input: words[i] = mark_safe(word) elif autoescape: words[i] = escape(word) return u''.join(words) urlize = allow_lazy(urlize, unicode)
""" Middleware that strips whitespace between html tags copied from David Cramer's blog http://www.davidcramer.net/code/369/spaceless-html-in-django.html """ import re from django.utils.functional import allow_lazy from django.utils.encoding import force_unicode def reduce_spaces_between_tags(value): """Returns the given HTML with all spaces between tags removed. ,but one. One space is left so that consecutive links and other things do not appear glued together slight mod of django.utils.html import strip_spaces_between_tags """ return re.sub(r'>\s+<', '> <', force_unicode(value)) reduce_spaces_between_tags = allow_lazy(reduce_spaces_between_tags, unicode) class SpacelessMiddleware(object): def process_response(self, request, response): """strips whitespace from all documents whose content type is text/html """ if 'Content-Type' in response and 'text/html' in response['Content-Type']: response.content = reduce_spaces_between_tags(response.content) response['Content-Length'] = str(len(response.content)) return response
__Y = r'(?P<year>\d{4})' __Y2 = r'(?P<year>\d{2})' __T = r'(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})' RFC1123_DATE = re.compile(r'^\w{3}, %s %s %s %s GMT$' % (__D, __M, __Y, __T)) RFC850_DATE = re.compile(r'^\w{6,9}, %s-%s-%s %s GMT$' % (__D, __M, __Y2, __T)) ASCTIME_DATE = re.compile(r'^\w{3} %s %s %s %s$' % (__M, __D2, __T, __Y)) def urlquote(url, safe='/'): """ A version of Python's urllib.quote() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_unicode(urllib.quote(smart_str(url), smart_str(safe))) urlquote = allow_lazy(urlquote, unicode) def urlquote_plus(url, safe=''): """ A version of Python's urllib.quote_plus() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_unicode(urllib.quote_plus(smart_str(url), smart_str(safe))) urlquote_plus = allow_lazy(urlquote_plus, unicode) def urlunquote(quoted_url): """ A wrapper for Python's urllib.unquote() function that can operate on the result of django.utils.http.urlquote().
import re from django.utils.encoding import force_unicode from django.utils.functional import allow_lazy def strip_spaces_between_tags(value): """Returns the given HTML with spaces between tags removed.""" value = re.sub(r'^\s+', '', force_unicode(value)) # Replace all lines with spaces only at the beginning of document value = re.sub(r'\n\s+', '\n', force_unicode(value)) # Replace all leading spaces at the beginning of a line return re.sub(r'>\s+<', '><', force_unicode(value)) strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, unicode)
**kwargs) def slugify(value, *args, **kwargs): """Add a DNS safe option to slugify :param dns_safe: Remove underscores from slug as well """ dns_safe = kwargs.pop('dns_safe', True) value = slugify_base(value, *args, **kwargs) if dns_safe: value = mark_safe(re.sub('[-_]+', '-', value)) return value slugify = allow_lazy(slugify, six.text_type, SafeText) def safe_makedirs(directory_name): """ Safely create a directory. Makedirs has an issue where it has a race condition around checking for a directory and then creating it. This catches the exception in the case where the dir already exists. """ try: os.makedirs(directory_name) except OSError as e: if e.errno == errno.EEXIST: pass
from __future__ import unicode_literals import base64 import calendar import datetime import re import sys import unicodedata from binascii import Error as BinasciiError from email.utils import formatdate from django.utils import six from django.utils.datastructures import MultiValueDict from django.utils.encoding import force_bytes, force_str, force_text from django.utils.functional import allow_lazy from django.utils.six.moves.urllib.parse import ( quote, quote_plus, unquote, unquote_plus, urlencode as original_urlencode, urlparse, ) ETAG_MATCH = re.compile(r'(?:W/)?"((?:\\.|[^"])*)"') MONTHS = 'jan feb mar apr may jun jul aug sep oct nov dec'.split() __D = r'(?P<day>\d{2})' __D2 = r'(?P<day>[ \d]\d)' __M = r'(?P<mon>\w{3})' __Y = r'(?P<year>\d{4})' __Y2 = r'(?P<year>\d{2})' __T = r'(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})' RFC1123_DATE = re.compile(r'^\w{3}, %s %s %s %s GMT$' % (__D, __M, __Y, __T)) RFC850_DATE = re.compile(r'^\w{6,9}, %s-%s-%s %s GMT$' % (__D, __M, __Y2, __T)) ASCTIME_DATE = re.compile(r'^\w{3} %s %s %s %s$' % (__M, __D2, __T, __Y)) RFC3986_GENDELIMS = str(":/?#[]@") RFC3986_SUBDELIMS = str("!$&'()*+,;=")
# -*- coding: utf-8 -*- try: from django.utils.text import truncate_words except ImportError: # django >=1.5 from django.utils.text import Truncator from django.utils.functional import allow_lazy def truncate_words(s, num, end_text='...'): truncate = end_text and ' %s' % end_text or '' return Truncator(s).words(num, truncate=truncate) truncate_words = allow_lazy(truncate_words, unicode)
def slugify(value, *args, **kwargs): """ Add a DNS safe option to slugify. :param dns_safe: Remove underscores from slug as well """ dns_safe = kwargs.pop('dns_safe', True) value = slugify_base(value, *args, **kwargs) if dns_safe: value = mark_safe(re.sub('[-_]+', '-', value)) return value slugify = allow_lazy(slugify, str, SafeText) def safe_makedirs(directory_name): """ Safely create a directory. Makedirs has an issue where it has a race condition around checking for a directory and then creating it. This catches the exception in the case where the dir already exists. """ try: os.makedirs(directory_name) except OSError as e: if e.errno != errno.EEXIST: # 17, FileExistsError raise
from .models import UserProfile as Profile md5 = lambda x: _md5(force_bytes(x, errors='replace')) sha1 = lambda x: _sha1(force_bytes(x, errors='replace')) try: from django.utils.text import truncate_words except ImportError: from django.utils.text import Truncator from django.utils.functional import allow_lazy def truncate_words(s, num, end_text='...'): truncate = end_text and ' %s' % end_text or '' return Truncator(s).words(num, truncate=truncate) truncate_words = allow_lazy(truncate_words, text_type) def upload_to_mugshot(instance, filename): """ Uploads a mugshot for a user to the ``USERENA_MUGSHOT_PATH`` and saving it under unique hash for the image. This is for privacy reasons so others can't just browse through the mugshot directory. """ extension = filename.split('.')[-1].lower() salt, hash = generate_sha1(instance.pk) path = settings.USERENA_MUGSHOT_PATH % { 'date_now': get_datetime_now().date(),
from django.utils import six import re import bleach def linebreaks(value, autoescape=False): """Converts newlines into <p> and <br />s.""" value = normalize_newlines(value) paras = re.split('\n{2,}', value) if autoescape: paras = ['<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras] else: paras = ['<p>%s</p>' % p for p in paras] return '\n\n'.join(paras) linebreaks = allow_lazy(linebreaks, six.text_type) def run(): error_count = 0 created_count = 0 db = sqlsoup.SQLSoup('mysql://root@localhost/rhizome') discussions = db.execute('select A.id, A.created, B.user_id, B.comment, C.title, B.id as comment_id from `discuss_discussionthread` A left join django_comments B on A.object_pk = B.id left join threadedcomments_comment C on B.id = C.comment_ptr_id where A.content_type_id = 112 and A.is_public = 1 and A.is_removed = 0 and B.is_public = 1 and B.is_removed = 0 and A.id <> 1523;') # top level discussions... for discussion in discussions.fetchall():
def escape(text): """ Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML. This function always escapes its input, even if it's already escaped and marked as such. This may result in double-escaping. If this is a concern, use conditional_escape() instead. """ return mark_safe( force_text(text).replace('&', '&').replace('<', '<').replace( '>', '>').replace('"', '"').replace("'", ''')) escape = allow_lazy(escape, six.text_type, SafeText) _js_escapes = { ord('\\'): '\\u005C', ord('\''): '\\u0027', ord('"'): '\\u0022', ord('>'): '\\u003E', ord('<'): '\\u003C', ord('&'): '\\u0026', ord('='): '\\u003D', ord('-'): '\\u002D', ord(';'): '\\u003B', ord('\u2028'): '\\u2028', ord('\u2029'): '\\u2029' }
def wrap(text, width): """ A word-wrap function that preserves existing line breaks and most spaces in the text. Expects that existing line breaks are posix newlines. """ text = force_unicode(text) def _lines(text): for index in range(0, len(text), width): yield text[index : index + width] + "\n" return u"".join(_lines(text)) wrap = allow_lazy(wrap, unicode) def letterwrap(value, arg): """ Wraps words at specified line length. Argument: number of characters to wrap the text at. """ return wrap(value, int(arg)) letterwrap = stringfilter(letterwrap) register.filter("letterwrap", letterwrap, is_safe=True) def first_item(value, separator):
from django.utils.functional import allow_lazy ETAG_MATCH = re.compile(r'(?:W/)?"((?:\\.|[^"])*)"') def urlquote(url, safe='/'): """ A version of Python's urllib.quote() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_unicode(urllib.quote(smart_str(url), safe)) urlquote = allow_lazy(urlquote, unicode) def urlquote_plus(url, safe=''): """ A version of Python's urllib.quote_plus() function that can operate on unicode strings. The url is first UTF-8 encoded before quoting. The returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ return force_unicode(urllib.quote_plus(smart_str(url), safe)) urlquote_plus = allow_lazy(urlquote_plus, unicode)
from django.core.urlresolvers import reverse from django.contrib.auth.models import User from django.utils.text import Truncator from django.utils.functional import allow_lazy from django.utils import six from creoleparser import text2html from .utils import slugify, creole_slugify, remove_links from .rendering import render_creole def truncate_words(s, num, end_text='...'): truncate = end_text and ' %s' % end_text or '' return Truncator(s).words(num, truncate=truncate) truncate_words = allow_lazy(truncate_words, six.text_type) class PageManager(models.Manager): def get_queryset(self): qs = super(PageManager, self).get_queryset() return qs.filter(deleted=False) def all_urls(self): urls = set() for page in self.all(): # Since we can link to both slugs and original names, we # consider both. urls.add(reverse('view_page', args=[page.name])) urls.add(reverse('view_page', args=[page.name_slug]))
for arg in string.split(','): arg = arg.strip() if arg == '': continue args.append(arg) return args def truncate_chars(s, num): s = force_unicode(s) length = int(num) if len(s) > length: length = length - 3 s = s[:length].strip() s += '...' return s truncate_chars = allow_lazy(truncate_chars, unicode) @register.filter def truncatechars(value, arg): """ Truncates a string after a certain number of characters, but respects word boundaries. Argument: Number of characters to truncate after. """ try: length = int(arg) except ValueError: # If the argument is not a valid integer. return value # Fail silently. return truncate_chars(value, length) truncatechars.is_safe = True
def decorator(func): return allow_lazy(func, *args)
from django.utils.hashcompat import sha_constructor, md5_constructor from userena import settings as userena_settings import urllib, random, datetime try: from django.utils.text import truncate_words except ImportError: # Django >=1.5 from django.utils.text import Truncator from django.utils.functional import allow_lazy def truncate_words(s, num, end_text='...'): truncate = end_text and ' %s' % end_text or '' return Truncator(s).words(num, truncate=truncate) truncate_words = allow_lazy(truncate_words, unicode) def get_gravatar(email, size=80, default='identicon'): """ Get's a Gravatar for a email address. :param size: The size in pixels of one side of the Gravatar's square image. Optional, if not supplied will default to ``80``. :param default: Defines what should be displayed if no image is found for this user. Optional argument which defaults to ``identicon``. The argument can be a URI to an image or one of the following options: ``404`` Do not load any image if none is associated with the email
respecting word boundaries. """ s = force_unicode(s) length = int(num) if len(s) > length: length = length - 3 if s[length - 1] == ' ' or s[length] == ' ': s = s[:length].strip() else: words = s[:length].split() if len(words) > 1: del words[-1] s = u' '.join(words) s += '...' return s truncate_chars = allow_lazy(truncate_chars, unicode) def truncatechars(value, arg): """ Truncates a string after a certain number of characters, but respects word boundaries. Argument: Number of characters to truncate after. """ try: length = int(arg) except ValueError: # Fail silently if the argument is not a valid integer. return value return truncate_chars(value, length)