示例#1
0
 def add_item(self, title, link, description, author_email=None,
     author_name=None, author_link=None, pubdate=None, comments=None,
     unique_id=None, enclosure=None, categories=(), item_copyright=None,
     ttl=None, **kwargs):
     """
     Adds an item to the feed. All args are expected to be Python Unicode
     objects except pubdate, which is a datetime.datetime object, and
     enclosure, which is an instance of the Enclosure class.
     """
     to_unicode = lambda s: force_unicode(s, strings_only=True)
     if categories:
         categories = [to_unicode(c) for c in categories]
     if ttl is not None:
         # Force ints to unicode
         ttl = force_unicode(ttl)
     item = {
         'title': to_unicode(title),
         'link': iri_to_uri(link),
         'description': to_unicode(description),
         'author_email': to_unicode(author_email),
         'author_name': to_unicode(author_name),
         'author_link': iri_to_uri(author_link),
         'pubdate': pubdate,
         'comments': to_unicode(comments),
         'unique_id': to_unicode(unique_id),
         'enclosure': enclosure,
         'categories': categories or (),
         'item_copyright': to_unicode(item_copyright),
         'ttl': ttl,
     }
     item.update(kwargs)
     self.items.append(item)
示例#2
0
 def render_option(self, selected_choices, option_value, option_label):
     option_value = force_unicode(option_value)
     selected_html = (option_value
                      in selected_choices) and u' selected="selected"' or ''
     return u'<option value="%s"%s>%s</option>' % (
         escape(option_value), selected_html,
         conditional_escape(force_unicode(option_label)))
示例#3
0
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and 'id' in attrs
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<ul>']
        # Normalize to strings
        str_values = set([force_unicode(v) for v in value])
        for i, (option_value,
                option_label) in enumerate(chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            if has_id:
                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                label_for = u' for="%s"' % final_attrs['id']
            else:
                label_for = ''

            cb = CheckboxInput(final_attrs,
                               check_test=lambda value: value in str_values)
            option_value = force_unicode(option_value)
            rendered_cb = cb.render(name, option_value)
            option_label = conditional_escape(force_unicode(option_label))
            output.append(u'<li><label%s>%s %s</label></li>' %
                          (label_for, rendered_cb, option_label))
        output.append(u'</ul>')
        return mark_safe(u'\n'.join(output))
示例#4
0
 def __init__(self, title, link, description, language=None, author_email=None,
         author_name=None, author_link=None, subtitle=None, categories=None,
         feed_url=None, feed_copyright=None, feed_guid=None, ttl=None, **kwargs):
     to_unicode = lambda s: force_unicode(s, strings_only=True)
     if categories:
         categories = [force_unicode(c) for c in categories]
     if ttl is not None:
         # Force ints to unicode
         ttl = force_unicode(ttl)
     self.feed = {
         'title': to_unicode(title),
         'link': iri_to_uri(link),
         'description': to_unicode(description),
         'language': to_unicode(language),
         'author_email': to_unicode(author_email),
         'author_name': to_unicode(author_name),
         'author_link': iri_to_uri(author_link),
         'subtitle': to_unicode(subtitle),
         'categories': categories or (),
         'feed_url': iri_to_uri(feed_url),
         'feed_copyright': to_unicode(feed_copyright),
         'id': feed_guid or link,
         'ttl': ttl,
     }
     self.feed.update(kwargs)
     self.items = []
示例#5
0
 def format(self, formatstr):
     pieces = []
     for i, piece in enumerate(re_formatchars.split(force_unicode(formatstr))):
         if i % 2:
             pieces.append(force_unicode(getattr(self, piece)()))
         elif piece:
             pieces.append(re_escaped.sub(r'\1', piece))
     return u''.join(pieces)
示例#6
0
 def format(self, formatstr):
     pieces = []
     for i, piece in enumerate(
             re_formatchars.split(force_unicode(formatstr))):
         if i % 2:
             pieces.append(force_unicode(getattr(self, piece)()))
         elif piece:
             pieces.append(re_escaped.sub(r'\1', piece))
     return u''.join(pieces)
示例#7
0
文件: widgets.py 项目: letolab/airy
 def _has_changed(self, initial, data):
     if initial is None:
         initial = []
     if data is None:
         data = []
     if len(initial) != len(data):
         return True
     initial_set = set([force_unicode(value) for value in initial])
     data_set = set([force_unicode(value) for value in data])
     return data_set != initial_set
示例#8
0
 def _has_changed(self, initial, data):
     if initial is None:
         initial = []
     if data is None:
         data = []
     if len(initial) != len(data):
         return True
     initial_set = set([force_unicode(value) for value in initial])
     data_set = set([force_unicode(value) for value in data])
     return data_set != initial_set
示例#9
0
文件: widgets.py 项目: letolab/airy
 def render_options(self, choices, selected_choices):
     # Normalize to strings.
     selected_choices = set([force_unicode(v) for v in selected_choices])
     output = []
     for option_value, option_label in chain(self.choices, choices):
         if isinstance(option_label, (list, tuple)):
             output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
             for option in option_label:
                 output.append(self.render_option(selected_choices, *option))
             output.append(u'</optgroup>')
         else:
             output.append(self.render_option(selected_choices, option_value, option_label))
     return u'\n'.join(output)
示例#10
0
 def get_renderer(self, name, value, attrs=None, choices=()):
     """Returns an instance of the renderer."""
     if value is None: value = ''
     str_value = force_unicode(value)  # Normalize to string.
     final_attrs = self.build_attrs(attrs)
     choices = list(chain(self.choices, choices))
     return self.renderer(name, str_value, final_attrs, choices)
示例#11
0
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])
            else:
                yield ' '
                if len(lines) > 1:
                    pos = len(lines[-1])
            yield word
    return u''.join(_generator())
示例#12
0
文件: html.py 项目: letolab/airy
def clean_html(text):
    """
    Clean the given HTML.  Specifically, do the following:
        * Convert <b> and <i> to <strong> and <em>.
        * Encode all ampersands correctly.
        * Remove all "target" attributes from <a> tags.
        * Remove extraneous HTML, such as presentational tags that open and
          immediately close and <br clear="all">.
        * Convert hard-coded bullets into HTML unordered lists.
        * Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the
          bottom of the text.
    """
    from airy.utils.text import normalize_newlines
    text = normalize_newlines(force_unicode(text))
    text = re.sub(r'<(/?)\s*b\s*>', '<\\1strong>', text)
    text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text)
    text = fix_ampersands(text)
    # Remove all target="" attributes from <a> tags.
    text = link_target_attribute_re.sub('\\1', text)
    # Trim stupid HTML such as <br clear="all">.
    text = html_gunk_re.sub('', text)
    # Convert hard-coded bullets into HTML unordered lists.
    def replace_p_tags(match):
        s = match.group().replace('</p>', '</li>')
        for d in DOTS:
            s = s.replace('<p>%s' % d, '<li>')
        return u'<ul>\n%s\n</ul>' % s
    text = hard_coded_bullets_re.sub(replace_p_tags, text)
    # Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the bottom
    # of the text.
    text = trailing_empty_content_re.sub('', text)
    return text
示例#13
0
def escape(html):
    """
    Returns the given HTML with ampersands, quotes and angle brackets encoded.
    """
    return mark_safe(
        force_unicode(html).replace('&', '&amp;').replace('<', '&lt;').replace(
            '>', '&gt;').replace('"', '&quot;').replace("'", '&#39;'))
示例#14
0
文件: widgets.py 项目: letolab/airy
 def get_renderer(self, name, value, attrs=None, choices=()):
     """Returns an instance of the renderer."""
     if value is None: value = ''
     str_value = force_unicode(value) # Normalize to string.
     final_attrs = self.build_attrs(attrs)
     choices = list(chain(self.choices, choices))
     return self.renderer(name, str_value, final_attrs, choices)
示例#15
0
def clean_html(text):
    """
    Clean the given HTML.  Specifically, do the following:
        * Convert <b> and <i> to <strong> and <em>.
        * Encode all ampersands correctly.
        * Remove all "target" attributes from <a> tags.
        * Remove extraneous HTML, such as presentational tags that open and
          immediately close and <br clear="all">.
        * Convert hard-coded bullets into HTML unordered lists.
        * Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the
          bottom of the text.
    """
    from airy.utils.text import normalize_newlines
    text = normalize_newlines(force_unicode(text))
    text = re.sub(r'<(/?)\s*b\s*>', '<\\1strong>', text)
    text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text)
    text = fix_ampersands(text)
    # Remove all target="" attributes from <a> tags.
    text = link_target_attribute_re.sub('\\1', text)
    # Trim stupid HTML such as <br clear="all">.
    text = html_gunk_re.sub('', text)

    # Convert hard-coded bullets into HTML unordered lists.
    def replace_p_tags(match):
        s = match.group().replace('</p>', '</li>')
        for d in DOTS:
            s = s.replace('<p>%s' % d, '<li>')
        return u'<ul>\n%s\n</ul>' % s

    text = hard_coded_bullets_re.sub(replace_p_tags, text)
    # Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the bottom
    # of the text.
    text = trailing_empty_content_re.sub('', text)
    return text
示例#16
0
文件: widgets.py 项目: letolab/airy
 def __unicode__(self):
     if 'id' in self.attrs:
         label_for = ' for="%s_%s"' % (self.attrs['id'], self.index)
     else:
         label_for = ''
     choice_label = conditional_escape(force_unicode(self.choice_label))
     return mark_safe(u'<label%s>%s %s</label>' % (label_for, self.tag(), choice_label))
示例#17
0
    def render(self, name, value, attrs=None):
        substitutions = {
            'initial_text': self.initial_text,
            'input_text': self.input_text,
            'clear_template': '',
            'clear_checkbox_label': self.clear_checkbox_label,
        }
        template = u'%(input)s'
        substitutions['input'] = super(ClearableFileInput,
                                       self).render(name, value, attrs)

        if value and hasattr(value, "url"):
            template = self.template_with_initial
            substitutions['initial'] = (
                u'<a href="%s">%s</a>' %
                (escape(value.url), escape(force_unicode(value))))
            if not self.is_required:
                checkbox_name = self.clear_checkbox_name(name)
                checkbox_id = self.clear_checkbox_id(checkbox_name)
                substitutions['clear_checkbox_name'] = conditional_escape(
                    checkbox_name)
                substitutions['clear_checkbox_id'] = conditional_escape(
                    checkbox_id)
                substitutions['clear'] = CheckboxInput().render(
                    checkbox_name, False, attrs={'id': checkbox_id})
                substitutions[
                    'clear_template'] = self.template_with_clear % substitutions

        return mark_safe(template % substitutions)
示例#18
0
def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
    """
    Converts any URLs in text into clickable links.

    Works on http://, https://, www. links and links ending in .org, .net or
    .com. Links can have trailing punctuation (periods, commas, close-parens)
    and leading punctuation (opening parens) and it'll still do the right
    thing.

    If trim_url_limit is not None, the URLs in link text longer than this limit
    will truncated to trim_url_limit-3 characters and appended with an elipsis.

    If nofollow is True, the URLs in link text will get a rel="nofollow"
    attribute.

    If autoescape is True, the link text and URLs will get autoescaped.
    """
    trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(
        x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x
    safe_input = isinstance(text, SafeData)
    words = word_split_re.split(force_unicode(text))
    nofollow_attr = nofollow and ' rel="nofollow"' or ''
    for i, word in enumerate(words):
        match = None
        if '.' in word or '@' in word or ':' in word:
            match = punctuation_re.match(word)
        if match:
            lead, middle, trail = match.groups()
            # Make URL we want to point to.
            url = None
            if middle.startswith('http://') or middle.startswith('https://'):
                url = urlquote(middle, safe='/&=:;#?+*')
            elif middle.startswith('www.') or ('@' not in middle and \
                    middle and middle[0] in string.ascii_letters + string.digits and \
                    (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)
                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)
示例#19
0
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)))
示例#20
0
 def render_options(self, choices, selected_choices):
     # Normalize to strings.
     selected_choices = set([force_unicode(v) for v in selected_choices])
     output = []
     for option_value, option_label in chain(self.choices, choices):
         if isinstance(option_label, (list, tuple)):
             output.append(u'<optgroup label="%s">' %
                           escape(force_unicode(option_value)))
             for option in option_label:
                 output.append(self.render_option(selected_choices,
                                                  *option))
             output.append(u'</optgroup>')
         else:
             output.append(
                 self.render_option(selected_choices, option_value,
                                    option_label))
     return u'\n'.join(output)
示例#21
0
文件: http.py 项目: letolab/airy
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)))
示例#22
0
文件: widgets.py 项目: letolab/airy
 def render(self, name, value, attrs=None):
     if value is None:
         value = ''
     final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
     if value != '':
         # Only add the 'value' attribute if a value is non-empty.
         final_attrs['value'] = force_unicode(self._format_value(value))
     return mark_safe(u'<input%s />' % flatatt(final_attrs))
示例#23
0
 def render(self, name, value, attrs=None):
     if value is None:
         value = ''
     final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
     if value != '':
         # Only add the 'value' attribute if a value is non-empty.
         final_attrs['value'] = force_unicode(self._format_value(value))
     return mark_safe(u'<input%s />' % flatatt(final_attrs))
示例#24
0
 def __unicode__(self):
     if 'id' in self.attrs:
         label_for = ' for="%s_%s"' % (self.attrs['id'], self.index)
     else:
         label_for = ''
     choice_label = conditional_escape(force_unicode(self.choice_label))
     return mark_safe(u'<label%s>%s %s</label>' %
                      (label_for, self.tag(), choice_label))
示例#25
0
文件: html.py 项目: letolab/airy
def linebreaks(value, autoescape=False):
    """Converts newlines into <p> and <br />s."""
    value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines
    paras = re.split('\n{2,}', value)
    if autoescape:
        paras = [u'<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras]
    else:
        paras = [u'<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
    return u'\n\n'.join(paras)
示例#26
0
 def _has_changed(self, initial, data):
     """
     Return True if data differs from initial.
     """
     # For purposes of seeing whether something has changed, None is
     # the same as an empty string, if the data or inital value we get
     # is None, replace it w/ u''.
     if data is None:
         data_value = u''
     else:
         data_value = data
     if initial is None:
         initial_value = u''
     else:
         initial_value = initial
     if force_unicode(initial_value) != force_unicode(data_value):
         return True
     return False
示例#27
0
文件: widgets.py 项目: letolab/airy
 def _has_changed(self, initial, data):
     """
     Return True if data differs from initial.
     """
     # For purposes of seeing whether something has changed, None is
     # the same as an empty string, if the data or inital value we get
     # is None, replace it w/ u''.
     if data is None:
         data_value = u''
     else:
         data_value = data
     if initial is None:
         initial_value = u''
     else:
         initial_value = initial
     if force_unicode(initial_value) != force_unicode(data_value):
         return True
     return False
示例#28
0
文件: html.py 项目: letolab/airy
def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
    """
    Converts any URLs in text into clickable links.

    Works on http://, https://, www. links and links ending in .org, .net or
    .com. Links can have trailing punctuation (periods, commas, close-parens)
    and leading punctuation (opening parens) and it'll still do the right
    thing.

    If trim_url_limit is not None, the URLs in link text longer than this limit
    will truncated to trim_url_limit-3 characters and appended with an elipsis.

    If nofollow is True, the URLs in link text will get a rel="nofollow"
    attribute.

    If autoescape is True, the link text and URLs will get autoescaped.
    """
    trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x
    safe_input = isinstance(text, SafeData)
    words = word_split_re.split(force_unicode(text))
    nofollow_attr = nofollow and ' rel="nofollow"' or ''
    for i, word in enumerate(words):
        match = None
        if '.' in word or '@' in word or ':' in word:
            match = punctuation_re.match(word)
        if match:
            lead, middle, trail = match.groups()
            # Make URL we want to point to.
            url = None
            if middle.startswith('http://') or middle.startswith('https://'):
                url = urlquote(middle, safe='/&=:;#?+*')
            elif middle.startswith('www.') or ('@' not in middle and \
                    middle and middle[0] in string.ascii_letters + string.digits and \
                    (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)
                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)
示例#29
0
def get_text_list(list_, last_word=ugettext_lazy(u'or')):
    """
    >>> get_text_list(['a', 'b', 'c', 'd'])
    u'a, b, c or d'
    >>> get_text_list(['a', 'b', 'c'], 'and')
    u'a, b and c'
    >>> get_text_list(['a', 'b'], 'and')
    u'a and b'
    >>> get_text_list(['a'])
    u'a'
    >>> get_text_list([])
    u''
    """
    if len(list_) == 0: return u''
    if len(list_) == 1: return force_unicode(list_[0])
    return u'%s %s %s' % (
        # Translators: This string is used as a separator between list elements
        _(', ').join([force_unicode(i) for i in list_][:-1]),
        force_unicode(last_word), force_unicode(list_[-1]))
示例#30
0
 def add_item(self,
              title,
              link,
              description,
              author_email=None,
              author_name=None,
              author_link=None,
              pubdate=None,
              comments=None,
              unique_id=None,
              enclosure=None,
              categories=(),
              item_copyright=None,
              ttl=None,
              **kwargs):
     """
     Adds an item to the feed. All args are expected to be Python Unicode
     objects except pubdate, which is a datetime.datetime object, and
     enclosure, which is an instance of the Enclosure class.
     """
     to_unicode = lambda s: force_unicode(s, strings_only=True)
     if categories:
         categories = [to_unicode(c) for c in categories]
     if ttl is not None:
         # Force ints to unicode
         ttl = force_unicode(ttl)
     item = {
         'title': to_unicode(title),
         'link': iri_to_uri(link),
         'description': to_unicode(description),
         'author_email': to_unicode(author_email),
         'author_name': to_unicode(author_name),
         'author_link': iri_to_uri(author_link),
         'pubdate': pubdate,
         'comments': to_unicode(comments),
         'unique_id': to_unicode(unique_id),
         'enclosure': enclosure,
         'categories': categories or (),
         'item_copyright': to_unicode(item_copyright),
         'ttl': ttl,
     }
     item.update(kwargs)
     self.items.append(item)
示例#31
0
    def __init__(self, message, code=None, params=None):
        import operator
        from airy.utils.encoding import force_unicode
        """
        ValidationError can be passed any object that can be printed (usually
        a string), a list of objects or a dictionary.
        """
        if isinstance(message, dict):
            self.message_dict = message
            # Reduce each list of messages into a single list.
            message = reduce(operator.add, message.values())

        if isinstance(message, list):
            self.messages = [force_unicode(msg) for msg in message]
        else:
            self.code = code
            self.params = params
            message = force_unicode(message)
            self.messages = [message]
示例#32
0
    def __init__(self, message, code=None, params=None):
        import operator
        from airy.utils.encoding import force_unicode
        """
        ValidationError can be passed any object that can be printed (usually
        a string), a list of objects or a dictionary.
        """
        if isinstance(message, dict):
            self.message_dict = message
            # Reduce each list of messages into a single list.
            message = reduce(operator.add, message.values())

        if isinstance(message, list):
            self.messages = [force_unicode(msg) for msg in message]
        else:
            self.code = code
            self.params = params
            message = force_unicode(message)
            self.messages = [message]
示例#33
0
def get_valid_filename(s):
    """
    Returns the given string converted to a string that can be used for a clean
    filename. Specifically, leading and trailing spaces are removed; other
    spaces are converted to underscores; and anything that is not a unicode
    alphanumeric, dash, underscore, or dot, is removed.
    >>> get_valid_filename("john's portrait in 2004.jpg")
    u'johns_portrait_in_2004.jpg'
    """
    s = force_unicode(s).strip().replace(' ', '_')
    return re.sub(r'(?u)[^-\w.]', '', s)
示例#34
0
文件: widgets.py 项目: letolab/airy
 def render(self, name, value, attrs=None):
     final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
     try:
         result = self.check_test(value)
     except: # Silently catch exceptions
         result = False
     if result:
         final_attrs['checked'] = 'checked'
     if value not in ('', True, False, None):
         # Only add the 'value' attribute if a value is non-empty.
         final_attrs['value'] = force_unicode(value)
     return mark_safe(u'<input%s />' % flatatt(final_attrs))
示例#35
0
 def render(self, name, value, attrs=None):
     final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
     try:
         result = self.check_test(value)
     except:  # Silently catch exceptions
         result = False
     if result:
         final_attrs['checked'] = 'checked'
     if value not in ('', True, False, None):
         # Only add the 'value' attribute if a value is non-empty.
         final_attrs['value'] = force_unicode(value)
     return mark_safe(u'<input%s />' % flatatt(final_attrs))
示例#36
0
def linebreaks(value, autoescape=False):
    """Converts newlines into <p> and <br />s."""
    value = re.sub(r'\r\n|\r|\n', '\n',
                   force_unicode(value))  # normalize newlines
    paras = re.split('\n{2,}', value)
    if autoescape:
        paras = [
            u'<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras
        ]
    else:
        paras = [u'<p>%s</p>' % p.replace('\n', '<br />') for p in paras]
    return u'\n\n'.join(paras)
示例#37
0
 def render(self, name, value, attrs=None, choices=()):
     if value is None: value = []
     final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
     id_ = final_attrs.get('id', None)
     inputs = []
     for i, v in enumerate(value):
         input_attrs = dict(value=force_unicode(v), **final_attrs)
         if id_:
             # An ID attribute was given. Add a numeric index as a suffix
             # so that the inputs don't all have the same ID attribute.
             input_attrs['id'] = '%s_%s' % (id_, i)
         inputs.append(u'<input%s />' % flatatt(input_attrs))
     return mark_safe(u'\n'.join(inputs))
示例#38
0
文件: widgets.py 项目: letolab/airy
 def render(self, name, value, attrs=None, choices=()):
     if value is None: value = []
     final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
     id_ = final_attrs.get('id', None)
     inputs = []
     for i, v in enumerate(value):
         input_attrs = dict(value=force_unicode(v), **final_attrs)
         if id_:
             # An ID attribute was given. Add a numeric index as a suffix
             # so that the inputs don't all have the same ID attribute.
             input_attrs['id'] = '%s_%s' % (id_, i)
         inputs.append(u'<input%s />' % flatatt(input_attrs))
     return mark_safe(u'\n'.join(inputs))
示例#39
0
 def __init__(self,
              title,
              link,
              description,
              language=None,
              author_email=None,
              author_name=None,
              author_link=None,
              subtitle=None,
              categories=None,
              feed_url=None,
              feed_copyright=None,
              feed_guid=None,
              ttl=None,
              **kwargs):
     to_unicode = lambda s: force_unicode(s, strings_only=True)
     if categories:
         categories = [force_unicode(c) for c in categories]
     if ttl is not None:
         # Force ints to unicode
         ttl = force_unicode(ttl)
     self.feed = {
         'title': to_unicode(title),
         'link': iri_to_uri(link),
         'description': to_unicode(description),
         'language': to_unicode(language),
         'author_email': to_unicode(author_email),
         'author_name': to_unicode(author_name),
         'author_link': iri_to_uri(author_link),
         'subtitle': to_unicode(subtitle),
         'categories': categories or (),
         'feed_url': iri_to_uri(feed_url),
         'feed_copyright': to_unicode(feed_copyright),
         'id': feed_guid or link,
         'ttl': ttl,
     }
     self.feed.update(kwargs)
     self.items = []
示例#40
0
文件: widgets.py 项目: letolab/airy
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and 'id' in attrs
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<ul>']
        # Normalize to strings
        str_values = set([force_unicode(v) for v in value])
        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            if has_id:
                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                label_for = u' for="%s"' % final_attrs['id']
            else:
                label_for = ''

            cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
            option_value = force_unicode(option_value)
            rendered_cb = cb.render(name, option_value)
            option_label = conditional_escape(force_unicode(option_label))
            output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label))
        output.append(u'</ul>')
        return mark_safe(u'\n'.join(output))
示例#41
0
def safe_join(base, *paths):
    """
    Joins one or more path components to the base path component intelligently.
    Returns a normalized, absolute version of the final path.

    The final path must be located inside of the base path component (otherwise
    a ValueError is raised).
    """
    # We need to use normcase to ensure we don't false-negative on case
    # insensitive operating systems (like Windows).
    base = force_unicode(base)
    paths = [force_unicode(p) for p in paths]
    final_path = normcase(abspathu(join(base, *paths)))
    base_path = normcase(abspathu(base))
    base_path_len = len(base_path)
    # Ensure final_path starts with base_path and that the next character after
    # the final path is os.sep (or nothing, in which case final_path must be
    # equal to base_path).
    if not final_path.startswith(base_path) \
       or final_path[base_path_len:base_path_len+1] not in ('', sep):
        raise ValueError('The joined path (%s) is located outside of the base '
                         'path component (%s)' % (final_path, base_path))
    return final_path
示例#42
0
    def save(self, name, content):
        """
        Saves new content to the file specified by name. The content should be a
        proper File object, ready to be read from the beginning.
        """
        # Get the proper name for the file, as it will actually be saved.
        if name is None:
            name = content.name

        name = self.get_available_name(name)
        name = self._save(name, content)

        # Store filenames with forward slashes, even on Windows
        return force_unicode(name.replace('\\', '/'))
示例#43
0
def truncate_words(s, num, end_text='...'):
    """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 (...)

    Newlines in the string will be stripped.
    """
    s = force_unicode(s)
    length = int(num)
    words = s.split()
    if len(words) > length:
        words = words[:length]
        if not words[-1].endswith(end_text):
            words.append(end_text)
    return u' '.join(words)
示例#44
0
def sanitize_address(addr, encoding):
    if isinstance(addr, basestring):
        addr = parseaddr(force_unicode(addr))
    nm, addr = addr
    nm = str(Header(nm, encoding))
    try:
        addr = addr.encode('ascii')
    except UnicodeEncodeError:  # IDN
        if u'@' in addr:
            localpart, domain = addr.split(u'@', 1)
            localpart = str(Header(localpart, encoding))
            domain = domain.encode('idna')
            addr = '@'.join([localpart, domain])
        else:
            addr = str(Header(addr, encoding))
    return formataddr((nm, addr))
示例#45
0
def smart_split(text):
    r"""
    Generator that splits a string by spaces, leaving quoted phrases together.
    Supports both single and double quotes, and supports escaping quotes with
    backslashes. In the output, strings will keep their initial and trailing
    quote marks and escaped quotes will remain escaped (the results can then
    be further processed with unescape_string_literal()).

    >>> list(smart_split(r'This is "a person\'s" test.'))
    [u'This', u'is', u'"a person\\\'s"', u'test.']
    >>> list(smart_split(r"Another 'person\'s' test."))
    [u'Another', u"'person\\'s'", u'test.']
    >>> list(smart_split(r'A "\"funky\" style" test.'))
    [u'A', u'"\\"funky\\" style"', u'test.']
    """
    text = force_unicode(text)
    for bit in smart_split_re.finditer(text):
        yield bit.group(0)
示例#46
0
def forbid_multi_line_headers(name, val, encoding):
    """Forbids multi-line headers, to prevent header injection."""
    encoding = encoding or settings.DEFAULT_CHARSET
    val = force_unicode(val)
    if '\n' in val or '\r' in val:
        raise BadHeaderError(
            "Header values can't contain newlines (got %r for header %r)" %
            (val, name))
    try:
        val = val.encode('ascii')
    except UnicodeEncodeError:
        if name.lower() in ADDRESS_HEADERS:
            val = ', '.join(
                sanitize_address(addr, encoding)
                for addr in getaddresses((val, )))
        else:
            val = str(Header(val, encoding))
    else:
        if name.lower() == 'subject':
            val = Header(val)
    return name, val
示例#47
0
文件: widgets.py 项目: letolab/airy
    def render(self, name, value, attrs=None):
        substitutions = {
            'initial_text': self.initial_text,
            'input_text': self.input_text,
            'clear_template': '',
            'clear_checkbox_label': self.clear_checkbox_label,
        }
        template = u'%(input)s'
        substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs)

        if value and hasattr(value, "url"):
            template = self.template_with_initial
            substitutions['initial'] = (u'<a href="%s">%s</a>'
                                        % (escape(value.url),
                                           escape(force_unicode(value))))
            if not self.is_required:
                checkbox_name = self.clear_checkbox_name(name)
                checkbox_id = self.clear_checkbox_id(checkbox_name)
                substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name)
                substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id)
                substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
                substitutions['clear_template'] = self.template_with_clear % substitutions

        return mark_safe(template % substitutions)
示例#48
0
def ugettext(message):
    return force_unicode(gettext(message))
示例#49
0
文件: html.py 项目: letolab/airy
def fix_ampersands(value):
    """Returns the given HTML with all unencoded ampersands encoded correctly."""
    return unencoded_ampersands_re.sub('&amp;', force_unicode(value))
示例#50
0
文件: html.py 项目: letolab/airy
def strip_entities(value):
    """Returns the given HTML with all entities (&something;) stripped."""
    return re.sub(r'&(?:\w+|#\d+);', '', force_unicode(value))
示例#51
0
文件: html.py 项目: letolab/airy
def strip_spaces_between_tags(value):
    """Returns the given HTML with spaces between tags removed."""
    return re.sub(r'>\s+<', '><', force_unicode(value))
示例#52
0
文件: html.py 项目: letolab/airy
def strip_tags(value):
    """Returns the given HTML with all tags stripped."""
    return re.sub(r'<[^>]*?>', '', force_unicode(value))
示例#53
0
文件: html.py 项目: letolab/airy
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))
    return value