def check_password(password, encoded, setter=None, preferred='default'):
    """
    Returns a boolean of whether the raw password matches the three
    part encoded digest.

    If setter is specified, it'll be called when you need to
    regenerate the password.
    """
    if not password or not is_password_usable(encoded):
        return False

    preferred = get_hasher(preferred)
    raw_password = password
    password = smart_str(password)
    encoded = smart_str(encoded)

    if len(encoded) == 32 and '$' not in encoded:
        hasher = get_hasher('unsalted_md5')
    else:
        algorithm = encoded.split('$', 1)[0]
        hasher = get_hasher(algorithm)

    must_update = hasher.algorithm != preferred.algorithm
    is_correct = hasher.verify(password, encoded)
    if setter and is_correct and must_update:
        setter(raw_password)
    return is_correct
示例#2
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)))
 def handle_label(self, path, **options):
     verbosity = int(options.get('verbosity', 1))
     result = finders.find(path, all=options['all'])
     path = smart_unicode(path)
     if result:
         if not isinstance(result, (list, tuple)):
             result = [result]
         output = u'\n  '.join(
             (smart_unicode(os.path.realpath(path)) for path in result))
         self.stdout.write(
             smart_str(u"Found '%s' here:\n  %s\n" % (path, output)))
     else:
         if verbosity >= 1:
             self.stderr.write(
                 smart_str("No matching file found for '%s'.\n" % path))
示例#4
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 isinstance(query, MultiValueDict):
        query = query.lists()
    elif 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)
示例#5
0
    def execute(self, *args, **options):
        """
        Try to execute this command, performing model validation if
        needed (as controlled by the attribute
        ``self.requires_model_validation``). If the command raises a
        ``CommandError``, intercept it and print it sensibly to
        stderr.
        """
        show_traceback = options.get('traceback', False)

        # Switch to English, because django-admin.py creates database content
        # like permissions, and those shouldn't contain any translations.
        # But only do this if we can assume we have a working settings file,
        # because django.utils.translation requires settings.
        saved_lang = None
        if self.can_import_settings:
            try:
                from my_django.utils import translation
                saved_lang = translation.get_language()
                translation.activate('en-us')
            except ImportError, e:
                # If settings should be available, but aren't,
                # raise the error and quit.
                if show_traceback:
                    traceback.print_exc()
                else:
                    sys.stderr.write(
                        smart_str(self.style.ERROR('Error: %s\n' % e)))
                sys.exit(1)
def sitemap(request, sitemaps, section=None):
    """
    This view generates a sitemap with additional geographic
    elements defined by Google.
    """
    maps, urls = [], []
    if section is not None:
        if section not in sitemaps:
            raise Http404(_(u"No sitemap available for section: %r") % section)
        maps.append(sitemaps[section])
    else:
        maps = sitemaps.values()

    page = request.GET.get("p", 1)
    current_site = get_current_site(request)
    for site in maps:
        try:
            if callable(site):
                urls.extend(site().get_urls(page=page, site=current_site))
            else:
                urls.extend(site.get_urls(page=page, site=current_site))
        except EmptyPage:
            raise Http404(_(u"Page %s empty") % page)
        except PageNotAnInteger:
            raise Http404(_(u"No page '%s'") % page)
    xml = smart_str(
        loader.render_to_string('gis/sitemaps/geo_sitemap.xml',
                                {'urlset': urls}))
    return HttpResponse(xml, content_type='application/xml')
    def assertNotContains(self,
                          response,
                          text,
                          status_code=200,
                          msg_prefix='',
                          html=False):
        """
        Asserts that a response indicates that some content was retrieved
        successfully, (i.e., the HTTP status code was as expected), and that
        ``text`` doesn't occurs in the content of the response.
        """

        # If the response supports deferred rendering and hasn't been rendered
        # yet, then ensure that it does get rendered before proceeding further.
        if (hasattr(response, 'render') and callable(response.render)
                and not response.is_rendered):
            response.render()

        if msg_prefix:
            msg_prefix += ": "

        self.assertEqual(
            response.status_code, status_code,
            msg_prefix + "Couldn't retrieve content: Response code was %d"
            " (expected %d)" % (response.status_code, status_code))
        text = smart_str(text, response._charset)
        content = response.content
        if html:
            content = assert_and_parse_html(
                self, content, None, u'Response\'s content is not valid HTML:')
            text = assert_and_parse_html(
                self, text, None, u'Second argument is not valid HTML:')
        self.assertEqual(
            content.count(text), 0,
            msg_prefix + "Response should not contain '%s'" % text)
    def render(self, name, value, attrs):
        encoded = value

        if not is_password_usable(encoded):
            return "None"

        final_attrs = self.build_attrs(attrs)

        encoded = smart_str(encoded)

        if len(encoded) == 32 and '$' not in encoded:
            algorithm = 'unsalted_md5'
        else:
            algorithm = encoded.split('$', 1)[0]

        try:
            hasher = get_hasher(algorithm)
        except ValueError:
            summary = "<strong>Invalid password format or unknown hashing algorithm.</strong>"
        else:
            summary = ""
            for key, value in hasher.safe_summary(encoded).iteritems():
                summary += "<strong>%(key)s</strong>: %(value)s " % {"key": ugettext(key), "value": value}

        return mark_safe("<div%(attrs)s>%(summary)s</div>" % {"attrs": flatatt(final_attrs), "summary": summary})
示例#9
0
def technical_404_response(request, exception):
    "Create a technical 404 error response. The exception should be the Http404."
    try:
        tried = exception.args[0]['tried']
    except (IndexError, TypeError, KeyError):
        tried = []
    else:
        if not tried:
            # tried exists but is an empty list. The URLconf must've been empty.
            return empty_urlconf(request)

    urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
    if isinstance(urlconf, types.ModuleType):
        urlconf = urlconf.__name__

    t = Template(TECHNICAL_404_TEMPLATE, name='Technical 404 template')
    c = Context({
        'urlconf': urlconf,
        'root_urlconf': settings.ROOT_URLCONF,
        'request_path': request.path_info[1:],  # Trim leading slash
        'urlpatterns': tried,
        'reason': smart_str(exception, errors='replace'),
        'request': request,
        'settings': get_safe_settings(),
    })
    return HttpResponseNotFound(t.render(c), mimetype='text/html')
示例#10
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:
            cached = _format_cache[cache_key]
            if cached is not None:
                return cached
            else:
                # Return the general setting by default
                return getattr(settings, format_type)
        except KeyError:
            for module in get_format_modules(lang):
                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)
示例#11
0
 def unsign(self, signed_value):
     signed_value = smart_str(signed_value)
     if not self.sep in signed_value:
         raise BadSignature('No "%s" found in value' % self.sep)
     value, sig = signed_value.rsplit(self.sep, 1)
     if constant_time_compare(sig, self.signature(value)):
         return force_unicode(value)
     raise BadSignature('Signature "%s" does not match' % sig)
def default_key_func(key, key_prefix, version):
    """
    Default function to generate keys.

    Constructs the key used by all other methods. By default it prepends
    the `key_prefix'. KEY_FUNCTION can be used to specify an alternate
    function with custom key making behavior.
    """
    return ':'.join([key_prefix, str(version), smart_str(key)])
 def log(self, msg, level=2):
     """
     Small log helper
     """
     msg = smart_str(msg)
     if not msg.endswith("\n"):
         msg += "\n"
     if self.verbosity >= level:
         self.stdout.write(msg)
 def urls(self, plugin_name, easy_instance_field):
     if easy_instance_field.field in self.field_dict(
             easy_instance_field.model.model).values():
         field_value = smart_str(easy_instance_field.raw_value)
         return [
             mark_safe(u'%s%s/%s/%s/' %
                       (easy_instance_field.model.url(), plugin_name,
                        easy_instance_field.field.name,
                        urllib.quote(field_value, safe='')))
         ]
示例#15
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
def make_password(password, salt=None, hasher='default'):
    """
    Turn a plain-text password into a hash for database storage

    Same as encode() but generates a new random salt.  If
    password is None or blank then UNUSABLE_PASSWORD will be
    returned which disallows logins.
    """
    if not password:
        return UNUSABLE_PASSWORD

    hasher = get_hasher(hasher)
    password = smart_str(password)

    if not salt:
        salt = hasher.salt()
    salt = smart_str(salt)

    return hasher.encode(password, salt)
 def get_context_object_name(self, obj):
     """
     Get the name to use for the object.
     """
     if self.context_object_name:
         return self.context_object_name
     elif hasattr(obj, '_meta'):
         return smart_str(obj._meta.object_name.lower())
     else:
         return None
 def get_context_object_name(self, object_list):
     """
     Get the name of the item to be used in the context.
     """
     if self.context_object_name:
         return self.context_object_name
     elif hasattr(object_list, 'model'):
         return smart_str('%s_list' %
                          object_list.model._meta.object_name.lower())
     else:
         return None
    def handle_noargs(self, **options):
        self.set_options(**options)
        # Warn before doing anything more.
        if (isinstance(self.storage, FileSystemStorage)
                and self.storage.location):
            destination_path = self.storage.location
            destination_display = ':\n\n    %s' % destination_path
        else:
            destination_path = None
            destination_display = '.'

        if self.clear:
            clear_display = 'This will DELETE EXISTING FILES!'
        else:
            clear_display = 'This will overwrite existing files!'

        if self.interactive:
            confirm = raw_input(u"""
You have requested to collect static files at the destination
location as specified in your settings%s

%s
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: """ %
                                (destination_display, clear_display))
            if confirm != 'yes':
                raise CommandError("Collecting static files cancelled.")

        collected = self.collect()
        modified_count = len(collected['modified'])
        unmodified_count = len(collected['unmodified'])
        post_processed_count = len(collected['post_processed'])

        if self.verbosity >= 1:
            template = ("\n%(modified_count)s %(identifier)s %(action)s"
                        "%(destination)s%(unmodified)s%(post_processed)s.\n")
            summary = template % {
                'modified_count':
                modified_count,
                'identifier':
                'static file' + (modified_count != 1 and 's' or ''),
                'action':
                self.symlink and 'symlinked' or 'copied',
                'destination':
                (destination_path and " to '%s'" % destination_path or ''),
                'unmodified': (collected['unmodified']
                               and ', %s unmodified' % unmodified_count or ''),
                'post_processed':
                (collected['post_processed']
                 and ', %s post-processed' % post_processed_count or ''),
            }
            self.stdout.write(smart_str(summary))
 def resolve(self, path):
     tried = []
     match = self.regex.search(path)
     if match:
         new_path = path[match.end():]
         for pattern in self.url_patterns:
             try:
                 sub_match = pattern.resolve(new_path)
             except Resolver404, e:
                 sub_tried = e.args[0].get('tried')
                 if sub_tried is not None:
                     tried.extend([[pattern] + t for t in sub_tried])
                 else:
                     tried.append([pattern])
             else:
                 if sub_match:
                     sub_match_dict = dict([(smart_str(k), v) for k, v in match.groupdict().items()])
                     sub_match_dict.update(self.default_kwargs)
                     for k, v in sub_match.kwargs.iteritems():
                         sub_match_dict[smart_str(k)] = v
                     return ResolverMatch(sub_match.func, sub_match.args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, [self.namespace] + sub_match.namespaces)
                 tried.append([pattern])
         raise Resolver404({'tried': tried, 'path': new_path})
    def get_step_files(self, step):
        wizard_files = self.data[self.step_files_key].get(step, {})

        if wizard_files and not self.file_storage:
            raise NoFileStorageConfigured

        files = {}
        for field, field_dict in wizard_files.iteritems():
            field_dict = dict(
                (smart_str(k), v) for k, v in field_dict.iteritems())
            tmp_name = field_dict.pop('tmp_name')
            files[field] = UploadedFile(file=self.file_storage.open(tmp_name),
                                        **field_dict)
        return files or None
示例#22
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
def textile(value):
    try:
        import textile
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError(
                "Error in 'textile' filter: The Python textile library isn't installed."
            )
        return force_unicode(value)
    else:
        return mark_safe(
            force_unicode(
                textile.textile(smart_str(value),
                                encoding='utf-8',
                                output='utf-8')))
def label_for_field(name, model, model_admin=None, return_attr=False):
    """
    Returns a sensible label for a field name. The name can be a callable or the
    name of an object attributes, as well as a genuine fields. If return_attr is
    True, the resolved attribute (which could be a callable) is also returned.
    This will be None if (and only if) the name refers to a field.
    """
    attr = None
    try:
        field = model._meta.get_field_by_name(name)[0]
        if isinstance(field, RelatedObject):
            label = field.opts.verbose_name
        else:
            label = field.verbose_name
    except models.FieldDoesNotExist:
        if name == "__unicode__":
            label = force_unicode(model._meta.verbose_name)
            attr = unicode
        elif name == "__str__":
            label = smart_str(model._meta.verbose_name)
            attr = str
        else:
            if callable(name):
                attr = name
            elif model_admin is not None and hasattr(model_admin, name):
                attr = getattr(model_admin, name)
            elif hasattr(model, name):
                attr = getattr(model, name)
            else:
                message = "Unable to lookup '%s' on %s" % (
                    name, model._meta.object_name)
                if model_admin:
                    message += " or %s" % (model_admin.__class__.__name__, )
                raise AttributeError(message)

            if hasattr(attr, "short_description"):
                label = attr.short_description
            elif callable(attr):
                if attr.__name__ == "<lambda>":
                    label = "--"
                else:
                    label = pretty_name(attr.__name__)
            else:
                label = pretty_name(name)
    if return_attr:
        return (label, attr)
    else:
        return label
def restructuredtext(value):
    try:
        from docutils.core import publish_parts
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError(
                "Error in 'restructuredtext' filter: The Python docutils library isn't installed."
            )
        return force_unicode(value)
    else:
        docutils_settings = getattr(settings,
                                    "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
        parts = publish_parts(source=smart_str(value),
                              writer_name="html4css1",
                              settings_overrides=docutils_settings)
        return mark_safe(force_unicode(parts["fragment"]))
 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
示例#27
0
 def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
     if not self.pk:
         raise ValueError(
             "get_next/get_previous cannot be used on unsaved objects.")
     op = is_next and 'gt' or 'lt'
     order = not is_next and '-' or ''
     param = smart_str(getattr(self, field.attname))
     q = Q(**{'%s__%s' % (field.name, op): param})
     q = q | Q(**{field.name: param, 'pk__%s' % op: self.pk})
     qs = self.__class__._default_manager.using(self._state.db).filter(
         **kwargs).filter(q).order_by('%s%s' % (order, field.name),
                                      '%spk' % order)
     try:
         return qs[0]
     except IndexError:
         raise self.DoesNotExist("%s matching query does not exist." %
                                 self.__class__._meta.object_name)
    def assertContains(self,
                       response,
                       text,
                       count=None,
                       status_code=200,
                       msg_prefix='',
                       html=False):
        """
        Asserts that a response indicates that some content was retrieved
        successfully, (i.e., the HTTP status code was as expected), and that
        ``text`` occurs ``count`` times in the content of the response.
        If ``count`` is None, the count doesn't matter - the assertion is true
        if the text occurs at least once in the response.
        """

        # If the response supports deferred rendering and hasn't been rendered
        # yet, then ensure that it does get rendered before proceeding further.
        if (hasattr(response, 'render') and callable(response.render)
                and not response.is_rendered):
            response.render()

        if msg_prefix:
            msg_prefix += ": "

        self.assertEqual(
            response.status_code, status_code,
            msg_prefix + "Couldn't retrieve content: Response code was %d"
            " (expected %d)" % (response.status_code, status_code))
        text = smart_str(text, response._charset)
        content = response.content
        if html:
            content = assert_and_parse_html(
                self, content, None, u"Response's content is not valid HTML:")
            text = assert_and_parse_html(
                self, text, None, u"Second argument is not valid HTML:")
        real_count = content.count(text)
        if count is not None:
            self.assertEqual(
                real_count, count,
                msg_prefix + "Found %d instances of '%s' in response"
                " (expected %d)" % (real_count, text, count))
        else:
            self.assertTrue(
                real_count != 0,
                msg_prefix + "Couldn't find '%s' in response" % text)
示例#29
0
def parse_rst(text, default_reference_context, thing_being_parsed=None):
    """
    Convert the string from reST to an XHTML fragment.
    """
    overrides = {
        'doctitle_xform': True,
        'inital_header_level': 3,
        "default_reference_context": default_reference_context,
        "link_base": reverse('django-admindocs-docroot').rstrip('/')
    }
    if thing_being_parsed:
        thing_being_parsed = smart_str("<%s>" % thing_being_parsed)
    parts = docutils.core.publish_parts(text,
                                        source_path=thing_being_parsed,
                                        destination_path=None,
                                        writer_name='html',
                                        settings_overrides=overrides)
    return mark_safe(parts['fragment'])
def smart_urlquote(url):
    "Quotes a URL if it isn't already quoted."
    # Handle IDN before quoting.
    scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
    try:
        netloc = netloc.encode('idna') # IDN -> ACE
    except UnicodeError: # invalid domain part
        pass
    else:
        url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))

    # An URL is considered unquoted if it contains no % characters or
    # contains a % not followed by two hexadecimal digits. See #9655.
    if '%' not in url or unquoted_percents_re.search(url):
        # See http://bugs.python.org/issue2637
        url = urllib.quote(smart_str(url), safe='!*\'();:@&=+$,/?#[]~')

    return force_unicode(url)