Exemple #1
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 ''
Exemple #2
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 ''
Exemple #3
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
Exemple #4
0
    def __init__(self, required=True, widget=None, label=None, initial=None,
                 help_text=None, error_messages=None, show_hidden_initial=False,
                 validators=[]):
        # 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    字段label信息
        #          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.   字段 help_text 信息
        # 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                        用于字段验证的验证器集合
        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()

        # Hook into self.widget_attrs() for any Field-specific HTML attributes.
        extra_attrs = self.widget_attrs(widget)     #给控件添加额外的html标签属性
        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
Exemple #5
0
def encodeValue(value):
    if value is None:
        return u''
    string_org = value
    try:
        value = encoding.smart_unicode(value)
    except (UnicodeEncodeError, encoding.DjangoUnicodeDecodeError):
        value = encoding.smart_str(value)
    except:
        value = string_org
    return value.strip()
Exemple #6
0
def slugify_name(name):
    """
    >>> slugify_name("Ben Dover")
    'ben-dover'
    >>> slugify_name("? ? MAKE ME   SANDWICH ! ! !")
    'make-me-sandwich'
    """
    slug = "another-name"
    name = smart_unicode(name)
    
    # Normalize unicode.
    try:
        slug = unicodedata.normalize("NFKD", name).encode("ascii", "ignore")
    except Exception, e:
        pass
Exemple #7
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
Exemple #8
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
Exemple #9
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)
Exemple #10
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]
Exemple #11
0
 def to_python(self, value):
     "Returns a Unicode object."
     if value in validators.EMPTY_VALUES:
         return u''
     return smart_unicode(value)
Exemple #12
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)