diff options
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/_os.py | 46 | ||||
| -rw-r--r-- | django/utils/datastructures.py | 13 | ||||
| -rw-r--r-- | django/utils/text.py | 3 |
3 files changed, 38 insertions, 24 deletions
diff --git a/django/utils/_os.py b/django/utils/_os.py index 56ef0f2014..30d7cbd444 100644 --- a/django/utils/_os.py +++ b/django/utils/_os.py @@ -1,23 +1,23 @@ -from os.path import join, normcase, abspath, sep
-
-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).
- final_path = normcase(abspath(join(base, *paths)))
- base_path = normcase(abspath(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 is located outside of the base path'
- ' component')
- return final_path
+from os.path import join, normcase, abspath, sep + +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). + final_path = normcase(abspath(join(base, *paths))) + base_path = normcase(abspath(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 is located outside of the base path' + ' component') + return final_path diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 60bc0051a2..4b60d1d194 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -267,3 +267,16 @@ class DotExpandedDict(dict): current[bits[-1]] = v except TypeError: # Special-case if current isn't a dict. current = {bits[-1] : v} + +class FileDict(dict): + """ + A dictionary used to hold uploaded file contents. The only special feature + here is that repr() of this object won't dump the entire contents of the + file to the output. A handy safeguard for a large file upload. + """ + def __repr__(self): + if 'content' in self: + d = dict(self, content='<omitted>') + return dict.__repr__(d) + return dict.__repr__(self) + diff --git a/django/utils/text.py b/django/utils/text.py index c41c35151b..4670ab47fa 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -2,6 +2,7 @@ import re from django.conf import settings from django.utils.encoding import force_unicode from django.utils.functional import allow_lazy +from django.utils.translation import ugettext_lazy # Capitalizes the first letter of a string. capfirst = lambda x: x and force_unicode(x)[0].upper() + force_unicode(x)[1:] @@ -123,7 +124,7 @@ def get_valid_filename(s): return re.sub(r'[^-A-Za-z0-9_.]', '', s) get_valid_filename = allow_lazy(get_valid_filename, unicode) -def get_text_list(list_, last_word=u'or'): +def get_text_list(list_, last_word=ugettext_lazy(u'or')): """ >>> get_text_list(['a', 'b', 'c', 'd']) 'a, b, c or d' |
