Exemplo n.º 1
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)))
Exemplo n.º 2
0
Arquivo: http.py Projeto: 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)))
Exemplo n.º 3
0
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
    then encoded as per normal.
    """
    if hasattr(query, 'items'):
        query = query.items()
    return urllib.urlencode(
        [(smart_str(k),
          isinstance(v, (list, tuple)) and [smart_str(i)
                                            for i in v] or smart_str(v))
         for k, v in query], doseq)
Exemplo n.º 4
0
Arquivo: http.py Projeto: letolab/airy
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
    then encoded as per normal.
    """
    if hasattr(query, 'items'):
        query = query.items()
    return urllib.urlencode(
        [(smart_str(k),
         isinstance(v, (list,tuple)) and [smart_str(i) for i in v] or smart_str(v))
            for k, v in query],
        doseq)
Exemplo n.º 5
0
def get_format(format_type, lang=None, use_l10n=None):
    """
    For a specific format type, returns the format for the current
    language (locale), defaults to the format in the settings.
    format_type is the name of the format, e.g. 'DATE_FORMAT'

    If use_l10n is provided and is not None, that will force the value to
    be localized (or not), overriding the value of settings.USE_L10N.
    """
    format_type = smart_str(format_type)
    if use_l10n or (use_l10n is None and settings.USE_L10N):
        if lang is None:
            lang = get_language()
        cache_key = (format_type, lang)
        try:
            return _format_cache[cache_key] or getattr(settings, format_type)
        except KeyError:
            for module in get_format_modules():
                try:
                    val = getattr(module, format_type)
                    _format_cache[cache_key] = val
                    return val
                except AttributeError:
                    pass
            _format_cache[cache_key] = None
    return getattr(settings, format_type)
Exemplo n.º 6
0
def localize_input(value, default=None):
    """
    Checks if an input value is a localizable type and returns it
    formatted with the appropriate formatting string of the current locale.
    """
    if isinstance(value, (decimal.Decimal, float, int, long)):
        return number_format(value)
    elif isinstance(value, datetime.datetime):
        value = datetime_safe.new_datetime(value)
        format = smart_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
        return value.strftime(format)
    elif isinstance(value, datetime.date):
        value = datetime_safe.new_date(value)
        format = smart_str(default or get_format('DATE_INPUT_FORMATS')[0])
        return value.strftime(format)
    elif isinstance(value, datetime.time):
        format = smart_str(default or get_format('TIME_INPUT_FORMATS')[0])
        return value.strftime(format)
    return value
Exemplo n.º 7
0
 def _create_mime_attachment(self, content, mimetype):
     """
     Converts the content, mimetype pair into a MIME attachment object.
     """
     basetype, subtype = mimetype.split('/', 1)
     if basetype == 'text':
         encoding = self.encoding or settings.DEFAULT_CHARSET
         attachment = SafeMIMEText(smart_str(content, encoding), subtype,
                                   encoding)
     else:
         # Encode non-text attachments with base64.
         attachment = MIMEBase(basetype, subtype)
         attachment.set_payload(content)
         Encoders.encode_base64(attachment)
     return attachment
Exemplo n.º 8
0
 def to_python(self, value):
     """
     Validates that the input is a decimal number. Returns a Decimal
     instance. Returns None for empty values. Ensures that there are no more
     than max_digits in the number, and no more than decimal_places digits
     after the decimal point.
     """
     if value in validators.EMPTY_VALUES:
         return None
     if self.localize:
         value = formats.sanitize_separators(value)
     value = smart_str(value).strip()
     try:
         value = Decimal(value)
     except DecimalException:
         raise ValidationError(self.error_messages['invalid'])
     return value
Exemplo n.º 9
0
 def to_python(self, value):
     """
     Validates that the input is a decimal number. Returns a Decimal
     instance. Returns None for empty values. Ensures that there are no more
     than max_digits in the number, and no more than decimal_places digits
     after the decimal point.
     """
     if value in validators.EMPTY_VALUES:
         return None
     if self.localize:
         value = formats.sanitize_separators(value)
     value = smart_str(value).strip()
     try:
         value = Decimal(value)
     except DecimalException:
         raise ValidationError(self.error_messages['invalid'])
     return value
Exemplo n.º 10
0
    def message(self):
        encoding = self.encoding or settings.DEFAULT_CHARSET
        msg = SafeMIMEText(smart_str(self.body, encoding),
                           self.content_subtype, encoding)
        msg = self._create_message(msg)
        msg['Subject'] = self.subject
        msg['From'] = self.extra_headers.get('From', self.from_email)
        msg['To'] = self.extra_headers.get('To', ', '.join(self.to))
        if self.cc:
            msg['Cc'] = ', '.join(self.cc)

        # Email header names are case-insensitive (RFC 2045), so we have to
        # accommodate that when doing comparisons.
        header_names = [key.lower() for key in self.extra_headers]
        if 'date' not in header_names:
            msg['Date'] = formatdate()
        if 'message-id' not in header_names:
            msg['Message-ID'] = make_msgid()
        for name, value in self.extra_headers.items():
            if name.lower() in ('from',
                                'to'):  # From and To are already handled
                continue
            msg[name] = value
        return msg
Exemplo n.º 11
0
 def __repr__(self):
     return "<%s: %s (%s)>" % (
         self.__class__.__name__, smart_str(self.name), self.content_type)
Exemplo n.º 12
0
 def dictvalue(t):
     if t[1] is True:
         return t[0]
     else:
         return t[0] + "=" + smart_str(t[1])
Exemplo n.º 13
0
Arquivo: base.py Projeto: letolab/airy
 def __str__(self):
     return smart_str(self.name or '')
Exemplo n.º 14
0
 def __str__(self):
     return smart_str(self.name or '')
Exemplo n.º 15
0
 def dictvalue(t):
     if t[1] is True:
         return t[0]
     else:
         return t[0] + '=' + smart_str(t[1])
Exemplo n.º 16
0
 def __repr__(self):
     return smart_str(self._tzname)
Exemplo n.º 17
0
 def __repr__(self):
     return smart_str(self._tzname)
Exemplo n.º 18
0
 def __repr__(self):
     return "<%s: %s (%s)>" % (self.__class__.__name__, smart_str(
         self.name), self.content_type)