示例#1
0
文件: fields.py 项目: letolab/airy
    def __init__(self, required=True, widget=None, label=None, initial=None,
                 help_text=None, error_messages=None, show_hidden_initial=False,
                 validators=[], localize=False):
        # required -- Boolean that specifies whether the field is required.
        #             True by default.
        # widget -- A Widget class, or instance of a Widget class, that should
        #           be used for this Field when displaying it. Each Field has a
        #           default Widget that it'll use if you don't specify this. In
        #           most cases, the default widget is TextInput.
        # label -- A verbose name for this field, for use in displaying this
        #          field in a form. By default, Django will use a "pretty"
        #          version of the form field name, if the Field is part of a
        #          Form.
        # initial -- A value to use in this Field's initial display. This value
        #            is *not* used as a fallback if data isn't given.
        # help_text -- An optional string to use as "help text" for this Field.
        # error_messages -- An optional dictionary to override the default
        #                   messages that the field will raise.
        # show_hidden_initial -- Boolean that specifies if it is needed to render a
        #                        hidden widget with initial value after widget.
        # validators -- List of addtional validators to use
        # localize -- Boolean that specifies if the field should be localized.
        if label is not None:
            label = smart_unicode(label)
        self.required, self.label, self.initial = required, label, initial
        self.show_hidden_initial = show_hidden_initial
        if help_text is None:
            self.help_text = u''
        else:
            self.help_text = smart_unicode(help_text)
        widget = widget or self.widget
        if isinstance(widget, type):
            widget = widget()

        # Trigger the localization machinery if needed.
        self.localize = localize
        if self.localize:
            widget.is_localized = True

        # Let the widget know whether it should display as required.
        widget.is_required = self.required

        # Hook into self.widget_attrs() for any Field-specific HTML attributes.
        extra_attrs = self.widget_attrs(widget)
        if extra_attrs:
            widget.attrs.update(extra_attrs)

        self.widget = widget

        # Increase the creation counter, and save our local copy.
        self.creation_counter = Field.creation_counter
        Field.creation_counter += 1

        messages = {}
        for c in reversed(self.__class__.__mro__):
            messages.update(getattr(c, 'default_error_messages', {}))
        messages.update(error_messages or {})
        self.error_messages = messages

        self.validators = self.default_validators + validators
示例#2
0
文件: forms.py 项目: sebastibe/airy
 def _auto_id(self):
     """
     Calculates and returns the ID attribute for this BoundField, if the
     associated Form has specified auto_id. Returns an empty string otherwise.
     """
     auto_id = self.form.auto_id
     if auto_id and "%s" in smart_unicode(auto_id):
         return smart_unicode(auto_id) % self.html_name
     elif auto_id:
         return self.html_name
     return ""
示例#3
0
 def _auto_id(self):
     """
     Calculates and returns the ID attribute for this BoundField, if the
     associated Form has specified auto_id. Returns an empty string otherwise.
     """
     auto_id = self.form.auto_id
     if auto_id and '%s' in smart_unicode(auto_id):
         return smart_unicode(auto_id) % self.html_name
     elif auto_id:
         return self.html_name
     return ''
示例#4
0
 def valid_value(self, value):
     "Check to see if the provided value is a valid choice"
     for k, v in self.choices:
         if isinstance(v, (list, tuple)):
             # This is an optgroup, so look inside the group for options
             for k2, v2 in v:
                 if value == smart_unicode(k2):
                     return True
         else:
             if value == smart_unicode(k):
                 return True
     return False
示例#5
0
文件: fields.py 项目: letolab/airy
 def valid_value(self, value):
     "Check to see if the provided value is a valid choice"
     for k, v in self.choices:
         if isinstance(v, (list, tuple)):
             # This is an optgroup, so look inside the group for options
             for k2, v2 in v:
                 if value == smart_unicode(k2):
                     return True
         else:
             if value == smart_unicode(k):
                 return True
     return False
示例#6
0
文件: web.py 项目: letolab/airy
 def post(self, *args, **kwargs):
     args = self.get_flat_arguments()
     args['files'] = {}
     for file in self.request.files:
         if len(self.request.files[file]) > 1:
             for content in self.request.files[file]:
                 args['files'][file] = {
                     'content': '%s;%s,%s' % (content['content_type'], 'base64', base64.b64encode(content['body'])),
                     'name': smart_unicode(content['filename'])
                 }
         else:
             content = self.request.files[file][0]
             args['files'][file] = {
                 'content': '%s;%s,%s' % (content['content_type'], 'base64', base64.b64encode(content['body'])),
                 'name': smart_unicode(content['filename'])
             }
     self.write(json_encode(args))
     self.finish()
示例#7
0
文件: web.py 项目: letolab/airy
def utf8(value):
    """Converts a string argument to a byte string.

    If the argument is already a byte string or None, it is returned unchanged.
    Otherwise it must be a unicode string and is encoded as utf8.
    """
    if isinstance(value, _UTF8_TYPES):
        return value
    assert isinstance(value, unicode)
    return smart_unicode(value, encoding="utf-8", errors="ignore")
示例#8
0
def utf8(value):
    """Converts a string argument to a byte string.

    If the argument is already a byte string or None, it is returned unchanged.
    Otherwise it must be a unicode string and is encoded as utf8.
    """
    if isinstance(value, _UTF8_TYPES):
        return value
    assert isinstance(value, unicode)
    return smart_unicode(value, encoding="utf-8", errors="ignore")
示例#9
0
 def post(self, *args, **kwargs):
     args = self.get_flat_arguments()
     args['files'] = {}
     for file in self.request.files:
         if len(self.request.files[file]) > 1:
             for content in self.request.files[file]:
                 args['files'][file] = {
                     'content':
                     '%s;%s,%s' % (content['content_type'], 'base64',
                                   base64.b64encode(content['body'])),
                     'name':
                     smart_unicode(content['filename'])
                 }
         else:
             content = self.request.files[file][0]
             args['files'][file] = {
                 'content':
                 '%s;%s,%s' % (content['content_type'], 'base64',
                               base64.b64encode(content['body'])),
                 'name':
                 smart_unicode(content['filename'])
             }
     self.write(json_encode(args))
     self.finish()
示例#10
0
 def __call__(self, value):
     try:
         super(URLValidator, self).__call__(value)
     except ValidationError, e:
         # Trivial case failed. Try for possible IDN domain
         if value:
             value = smart_unicode(value)
             scheme, netloc, path, query, fragment = urlparse.urlsplit(value)
             try:
                 netloc = netloc.encode('idna') # IDN -> ACE
             except UnicodeError: # invalid domain part
                 raise e
             url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
             super(URLValidator, self).__call__(url)
         else:
             raise
示例#11
0
文件: markup.py 项目: letolab/airy
def sanitize(text):
    """
    Cleans up user input from potentially dangerous things,
    such as <script>, <img src="javascript:..." />, <a href="javascript:.."> etc.

    Please note, this function is **not 100% safe**.

    If want to ensure the input is safe it's best to just escape all HTML.

    If you decide to use ``markup,sanitize()`` make sure you have html5lib available on your system/project.
    """
    try:
        from airy.core import sanitizer
        return smart_unicode(sanitizer.clean_html(text))
    except ImportError:
        logging.error("You need html5lib in order to use sanitize")
        return "ERROR: You need html5lib in order to use sanitize"
示例#12
0
def sanitize(text):
    """
    Cleans up user input from potentially dangerous things,
    such as <script>, <img src="javascript:..." />, <a href="javascript:.."> etc.

    Please note, this function is **not 100% safe**.

    If want to ensure the input is safe it's best to just escape all HTML.

    If you decide to use ``markup,sanitize()`` make sure you have html5lib available on your system/project.
    """
    try:
        from airy.core import sanitizer
        return smart_unicode(sanitizer.clean_html(text))
    except ImportError:
        logging.error("You need html5lib in order to use sanitize")
        return "ERROR: You need html5lib in order to use sanitize"
示例#13
0
 def __call__(self, value):
     try:
         super(URLValidator, self).__call__(value)
     except ValidationError, e:
         # Trivial case failed. Try for possible IDN domain
         if value:
             value = smart_unicode(value)
             scheme, netloc, path, query, fragment = urlparse.urlsplit(
                 value)
             try:
                 netloc = netloc.encode('idna')  # IDN -> ACE
             except UnicodeError:  # invalid domain part
                 raise e
             url = urlparse.urlunsplit(
                 (scheme, netloc, path, query, fragment))
             super(URLValidator, self).__call__(url)
         else:
             raise
示例#14
0
 def tzname(self, dt):
     try:
         return smart_unicode(time.tzname[self._isdst(dt)],
                              DEFAULT_LOCALE_ENCODING)
     except UnicodeDecodeError:
         return None
示例#15
0
文件: tzinfo.py 项目: sebastibe/airy
 def tzname(self, dt):
     try:
         return smart_unicode(time.tzname[self._isdst(dt)], DEFAULT_LOCALE_ENCODING)
     except UnicodeDecodeError:
         return None
示例#16
0
文件: fields.py 项目: letolab/airy
 def to_python(self, value):
     if not value:
         return []
     elif not isinstance(value, (list, tuple)):
         raise ValidationError(self.error_messages['invalid_list'])
     return [smart_unicode(val) for val in value]
示例#17
0
 def __call__(self, value):
     """
     Validates that the input matches the regular expression.
     """
     if not self.regex.search(smart_unicode(value)):
         raise ValidationError(self.message, code=self.code)
示例#18
0
文件: fields.py 项目: letolab/airy
 def to_python(self, value):
     "Returns a Unicode object."
     if value in validators.EMPTY_VALUES:
         return u''
     return smart_unicode(value)
示例#19
0
 def __call__(self, value):
     """
     Validates that the input matches the regular expression.
     """
     if not self.regex.search(smart_unicode(value)):
         raise ValidationError(self.message, code=self.code)
示例#20
0
 def to_python(self, value):
     if not value:
         return []
     elif not isinstance(value, (list, tuple)):
         raise ValidationError(self.error_messages['invalid_list'])
     return [smart_unicode(val) for val in value]
示例#21
0
 def __unicode__(self):
     return smart_unicode(self.name or u'')
示例#22
0
 def to_python(self, value):
     "Returns a Unicode object."
     if value in validators.EMPTY_VALUES:
         return u''
     return smart_unicode(value)
示例#23
0
文件: markup.py 项目: letolab/airy
def linebreaks(text):
    """
    Turns every new-line ("\n") into a "<br />" HTML tag.
    """
    return smart_unicode(text).replace('\n', '<br />')
示例#24
0
    def __init__(self,
                 required=True,
                 widget=None,
                 label=None,
                 initial=None,
                 help_text=None,
                 error_messages=None,
                 show_hidden_initial=False,
                 validators=[],
                 localize=False):
        # required -- Boolean that specifies whether the field is required.
        #             True by default.
        # widget -- A Widget class, or instance of a Widget class, that should
        #           be used for this Field when displaying it. Each Field has a
        #           default Widget that it'll use if you don't specify this. In
        #           most cases, the default widget is TextInput.
        # label -- A verbose name for this field, for use in displaying this
        #          field in a form. By default, Django will use a "pretty"
        #          version of the form field name, if the Field is part of a
        #          Form.
        # initial -- A value to use in this Field's initial display. This value
        #            is *not* used as a fallback if data isn't given.
        # help_text -- An optional string to use as "help text" for this Field.
        # error_messages -- An optional dictionary to override the default
        #                   messages that the field will raise.
        # show_hidden_initial -- Boolean that specifies if it is needed to render a
        #                        hidden widget with initial value after widget.
        # validators -- List of addtional validators to use
        # localize -- Boolean that specifies if the field should be localized.
        if label is not None:
            label = smart_unicode(label)
        self.required, self.label, self.initial = required, label, initial
        self.show_hidden_initial = show_hidden_initial
        if help_text is None:
            self.help_text = u''
        else:
            self.help_text = smart_unicode(help_text)
        widget = widget or self.widget
        if isinstance(widget, type):
            widget = widget()

        # Trigger the localization machinery if needed.
        self.localize = localize
        if self.localize:
            widget.is_localized = True

        # Let the widget know whether it should display as required.
        widget.is_required = self.required

        # Hook into self.widget_attrs() for any Field-specific HTML attributes.
        extra_attrs = self.widget_attrs(widget)
        if extra_attrs:
            widget.attrs.update(extra_attrs)

        self.widget = widget

        # Increase the creation counter, and save our local copy.
        self.creation_counter = Field.creation_counter
        Field.creation_counter += 1

        messages = {}
        for c in reversed(self.__class__.__mro__):
            messages.update(getattr(c, 'default_error_messages', {}))
        messages.update(error_messages or {})
        self.error_messages = messages

        self.validators = self.default_validators + validators
示例#25
0
文件: base.py 项目: letolab/airy
 def __unicode__(self):
     return smart_unicode(self.name or u'')
示例#26
0
文件: markup.py 项目: letolab/airy
def linkify(text, shorten=False, extra_params={"target": "_blank", "rel": "nofollow"},
            require_protocol=False, permitted_protocols=["http", "https"]):
    """Converts plain text into HTML with links.

    For example: ``linkify("Hello http://tornadoweb.org!")`` would return
    ``Hello <a href="http://tornadoweb.org">http://tornadoweb.org</a>!``

    Parameters:

    shorten: Long urls will be shortened for display.

    extra_params: Extra text to include in the link tag,
        e.g. linkify(text, extra_params={'rel': "nofollow", 'class': "external"})

    require_protocol: Only linkify urls which include a protocol. If this is
        False, urls such as www.facebook.com will also be linkified.

    permitted_protocols: List (or set) of protocols which should be linkified,
        e.g. linkify(text, permitted_protocols=["http", "ftp", "mailto"]).
        It is very unsafe to include protocols such as "javascript".
    """

    def make_link(m):
        url = m.group(1)
        proto = m.group(2)
        if require_protocol and not proto:
            return url  # not protocol, no linkify

        if proto and proto not in permitted_protocols:
            return url  # bad protocol, no linkify

        href = m.group(1)
        if not proto:
            href = "http://" + href   # no proto specified, use http

        params = ' '
        if extra_params:
            params += ' '.join(['%s="%s"' % (key, value) for key, value in extra_params.iteritems()])

        # clip long urls. max_len is just an approximation
        max_len = 30
        if shorten and len(url) > max_len:
            before_clip = url
            if proto:
                proto_len = len(proto) + 1 + len(m.group(3) or "")  # +1 for :
            else:
                proto_len = 0

            parts = url[proto_len:].split("/")
            if len(parts) > 1:
                # Grab the whole host part plus the first bit of the path
                # The path is usually not that interesting once shortened
                # (no more slug, etc), so it really just provides a little
                # extra indication of shortening.
                url = url[:proto_len] + parts[0] + "/" +\
                      parts[1][:8].split('?')[0].split('.')[0]

            if len(url) > max_len * 1.5:  # still too long
                url = url[:max_len]

            if url != before_clip:
                amp = url.rfind('&')
                # avoid splitting html char entities
                if amp > max_len - 5:
                    url = url[:amp]
                url += "..."

                if len(url) >= len(before_clip):
                    url = before_clip
                else:
                    # full url is visible on mouse-over (for those who don't
                    # have a status bar, such as Safari by default)
                    params += ' title="%s"' % href

        return u'<a href="%s"%s>%s</a>' % (href, params, url)

    try:
        text = text.replace('http://www.', 'www.').replace('www.', 'http://www.')
        return _autolink_html(text, _link_regexes, extra_params=extra_params)
    except ImportError:
        pass

    splitted_text = re.split("""(<a.*?>.*?</a>)""", text)
    for i in range(0, len(splitted_text), 2):
        splitted_text[i] = _URL_RE.sub(make_link, splitted_text[i])

    # The regex is modified to avoid character entites other than &amp; so
    # that we won't pick up &quot;, etc.
#    return _URL_RE.sub(make_link, text)
    return smart_unicode(''.join(splitted_text))
示例#27
0
def linkify(text,
            shorten=False,
            extra_params={
                "target": "_blank",
                "rel": "nofollow"
            },
            require_protocol=False,
            permitted_protocols=["http", "https"]):
    """Converts plain text into HTML with links.

    For example: ``linkify("Hello http://tornadoweb.org!")`` would return
    ``Hello <a href="http://tornadoweb.org">http://tornadoweb.org</a>!``

    Parameters:

    shorten: Long urls will be shortened for display.

    extra_params: Extra text to include in the link tag,
        e.g. linkify(text, extra_params={'rel': "nofollow", 'class': "external"})

    require_protocol: Only linkify urls which include a protocol. If this is
        False, urls such as www.facebook.com will also be linkified.

    permitted_protocols: List (or set) of protocols which should be linkified,
        e.g. linkify(text, permitted_protocols=["http", "ftp", "mailto"]).
        It is very unsafe to include protocols such as "javascript".
    """
    def make_link(m):
        url = m.group(1)
        proto = m.group(2)
        if require_protocol and not proto:
            return url  # not protocol, no linkify

        if proto and proto not in permitted_protocols:
            return url  # bad protocol, no linkify

        href = m.group(1)
        if not proto:
            href = "http://" + href  # no proto specified, use http

        params = ' '
        if extra_params:
            params += ' '.join([
                '%s="%s"' % (key, value)
                for key, value in extra_params.iteritems()
            ])

        # clip long urls. max_len is just an approximation
        max_len = 30
        if shorten and len(url) > max_len:
            before_clip = url
            if proto:
                proto_len = len(proto) + 1 + len(m.group(3) or "")  # +1 for :
            else:
                proto_len = 0

            parts = url[proto_len:].split("/")
            if len(parts) > 1:
                # Grab the whole host part plus the first bit of the path
                # The path is usually not that interesting once shortened
                # (no more slug, etc), so it really just provides a little
                # extra indication of shortening.
                url = url[:proto_len] + parts[0] + "/" +\
                      parts[1][:8].split('?')[0].split('.')[0]

            if len(url) > max_len * 1.5:  # still too long
                url = url[:max_len]

            if url != before_clip:
                amp = url.rfind('&')
                # avoid splitting html char entities
                if amp > max_len - 5:
                    url = url[:amp]
                url += "..."

                if len(url) >= len(before_clip):
                    url = before_clip
                else:
                    # full url is visible on mouse-over (for those who don't
                    # have a status bar, such as Safari by default)
                    params += ' title="%s"' % href

        return u'<a href="%s"%s>%s</a>' % (href, params, url)

    try:
        text = text.replace('http://www.',
                            'www.').replace('www.', 'http://www.')
        return _autolink_html(text, _link_regexes, extra_params=extra_params)
    except ImportError:
        pass

    splitted_text = re.split("""(<a.*?>.*?</a>)""", text)
    for i in range(0, len(splitted_text), 2):
        splitted_text[i] = _URL_RE.sub(make_link, splitted_text[i])

    # The regex is modified to avoid character entites other than &amp; so
    # that we won't pick up &quot;, etc.


#    return _URL_RE.sub(make_link, text)
    return smart_unicode(''.join(splitted_text))
示例#28
0
def linebreaks(text):
    """
    Turns every new-line ("\n") into a "<br />" HTML tag.
    """
    return smart_unicode(text).replace('\n', '<br />')