diff options
| author | Justin Bronn <jbronn@gmail.com> | 2007-10-26 20:47:20 +0000 |
|---|---|---|
| committer | Justin Bronn <jbronn@gmail.com> | 2007-10-26 20:47:20 +0000 |
| commit | 4ffbddf92d89c3b31cef90043721184a501cd454 (patch) | |
| tree | db8131d40b0a5437270a6b1e8d579113ab3508e8 /django/core | |
| parent | f66ee9d0065838a0f6c9c76203a775a78446cdf7 (diff) | |
gis: Merged revisions 6525-6613 via svnmerge from [repos:django/trunk trunk].
git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6615 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core')
| -rw-r--r-- | django/core/cache/backends/base.py | 8 | ||||
| -rw-r--r-- | django/core/cache/backends/db.py | 8 | ||||
| -rw-r--r-- | django/core/cache/backends/dummy.py | 3 | ||||
| -rw-r--r-- | django/core/cache/backends/filebased.py | 20 | ||||
| -rw-r--r-- | django/core/cache/backends/locmem.py | 7 | ||||
| -rw-r--r-- | django/core/cache/backends/memcached.py | 3 | ||||
| -rw-r--r-- | django/core/cache/backends/simple.py | 9 | ||||
| -rw-r--r-- | django/core/handlers/modpython.py | 19 | ||||
| -rw-r--r-- | django/core/handlers/wsgi.py | 22 | ||||
| -rw-r--r-- | django/core/mail.py | 4 | ||||
| -rw-r--r-- | django/core/management/__init__.py | 111 | ||||
| -rw-r--r-- | django/core/management/validation.py | 2 | ||||
| -rw-r--r-- | django/core/urlresolvers.py | 5 |
13 files changed, 151 insertions, 70 deletions
diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py index bb67399f3b..cd0d7bd103 100644 --- a/django/core/cache/backends/base.py +++ b/django/core/cache/backends/base.py @@ -14,6 +14,14 @@ class BaseCache(object): timeout = 300 self.default_timeout = timeout + def add(self, key, value, timeout=None): + """ + Set a value in the cache if the key does not already exist. If + timeout is given, that timeout will be used for the key; otherwise + the default cache timeout will be used. + """ + raise NotImplementedError + def get(self, key, default=None): """ Fetch a given key from the cache. If the key does not exist, return diff --git a/django/core/cache/backends/db.py b/django/core/cache/backends/db.py index 4a0d44a44e..d54677057f 100644 --- a/django/core/cache/backends/db.py +++ b/django/core/cache/backends/db.py @@ -38,6 +38,12 @@ class CacheClass(BaseCache): return pickle.loads(base64.decodestring(row[1])) def set(self, key, value, timeout=None): + return self._base_set('set', key, value, timeout) + + def add(self, key, value, timeout=None): + return self._base_set('add', key, value, timeout) + + def _base_set(self, mode, key, value, timeout=None): if timeout is None: timeout = self.default_timeout cursor = connection.cursor() @@ -50,7 +56,7 @@ class CacheClass(BaseCache): encoded = base64.encodestring(pickle.dumps(value, 2)).strip() cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) try: - if cursor.fetchone(): + if mode == 'set' and cursor.fetchone(): cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key]) else: cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)]) diff --git a/django/core/cache/backends/dummy.py b/django/core/cache/backends/dummy.py index 4c64161538..3ff7e1c1b9 100644 --- a/django/core/cache/backends/dummy.py +++ b/django/core/cache/backends/dummy.py @@ -6,6 +6,9 @@ class CacheClass(BaseCache): def __init__(self, *args, **kwargs): pass + def add(self, *args, **kwargs): + pass + def get(self, key, default=None): return default diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py index d5415c8ace..690193ac81 100644 --- a/django/core/cache/backends/filebased.py +++ b/django/core/cache/backends/filebased.py @@ -17,6 +17,26 @@ class CacheClass(SimpleCacheClass): del self._cache del self._expire_info + def add(self, key, value, timeout=None): + fname = self._key_to_file(key) + if timeout is None: + timeout = self.default_timeout + try: + filelist = os.listdir(self._dir) + except (IOError, OSError): + self._createdir() + filelist = [] + if len(filelist) > self._max_entries: + self._cull(filelist) + if os.path.basename(fname) not in filelist: + try: + f = open(fname, 'wb') + now = time.time() + pickle.dump(now + timeout, f, 2) + pickle.dump(value, f, 2) + except (IOError, OSError): + pass + def get(self, key, default=None): fname = self._key_to_file(key) try: diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py index 4c48c571b7..5998f7bfd5 100644 --- a/django/core/cache/backends/locmem.py +++ b/django/core/cache/backends/locmem.py @@ -14,6 +14,13 @@ class CacheClass(SimpleCacheClass): SimpleCacheClass.__init__(self, host, params) self._lock = RWLock() + def add(self, key, value, timeout=None): + self._lock.writer_enters() + try: + SimpleCacheClass.add(self, key, value, timeout) + finally: + self._lock.writer_leaves() + def get(self, key, default=None): should_delete = False self._lock.reader_enters() diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py index 52610daef1..096cec0ee0 100644 --- a/django/core/cache/backends/memcached.py +++ b/django/core/cache/backends/memcached.py @@ -16,6 +16,9 @@ class CacheClass(BaseCache): BaseCache.__init__(self, params) self._cache = memcache.Client(server.split(';')) + def add(self, key, value, timeout=0): + self._cache.add(key.encode('ascii', 'ignore'), value, timeout or self.default_timeout) + def get(self, key, default=None): val = self._cache.get(smart_str(key)) if val is None: diff --git a/django/core/cache/backends/simple.py b/django/core/cache/backends/simple.py index 3fcad8c7ad..ff60d49066 100644 --- a/django/core/cache/backends/simple.py +++ b/django/core/cache/backends/simple.py @@ -21,6 +21,15 @@ class CacheClass(BaseCache): except (ValueError, TypeError): self._cull_frequency = 3 + def add(self, key, value, timeout=None): + if len(self._cache) >= self._max_entries: + self._cull() + if timeout is None: + timeout = self.default_timeout + if key not in self._cache.keys(): + self._cache[key] = value + self._expire_info[key] = time.time() + timeout + def get(self, key, default=None): now = time.time() exp = self._expire_info.get(key) diff --git a/django/core/handlers/modpython.py b/django/core/handlers/modpython.py index 52419d9e6c..2a3e03f3dd 100644 --- a/django/core/handlers/modpython.py +++ b/django/core/handlers/modpython.py @@ -14,7 +14,7 @@ import os class ModPythonRequest(http.HttpRequest): def __init__(self, req): self._req = req - self.path = force_unicode(req.uri, errors='ignore') + self.path = force_unicode(req.uri) def __repr__(self): # Since this is called as part of error handling, we need to be very @@ -136,6 +136,8 @@ class ModPythonRequest(http.HttpRequest): method = property(_get_method) class ModPythonHandler(BaseHandler): + request_class = ModPythonRequest + def __call__(self, req): # mod_python fakes the environ, and thus doesn't process SetEnv. This fixes that os.environ.update(req.subprocess_env) @@ -150,13 +152,16 @@ class ModPythonHandler(BaseHandler): dispatcher.send(signal=signals.request_started) try: - request = ModPythonRequest(req) - response = self.get_response(request) - - # Apply response middleware - for middleware_method in self._response_middleware: - response = middleware_method(request, response) + try: + request = self.request_class(req) + except UnicodeDecodeError: + response = http.HttpResponseBadRequest() + else: + response = self.get_response(request) + # Apply response middleware + for middleware_method in self._response_middleware: + response = middleware_method(request, response) finally: dispatcher.send(signal=signals.request_finished) diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index 95c5f9cbda..d06eee73f2 100644 --- a/django/core/handlers/wsgi.py +++ b/django/core/handlers/wsgi.py @@ -75,7 +75,7 @@ def safe_copyfileobj(fsrc, fdst, length=16*1024, size=0): class WSGIRequest(http.HttpRequest): def __init__(self, environ): self.environ = environ - self.path = force_unicode(environ['PATH_INFO'], errors='ignore') + self.path = force_unicode(environ['PATH_INFO']) self.META = environ self.method = environ['REQUEST_METHOD'].upper() @@ -165,7 +165,9 @@ class WSGIRequest(http.HttpRequest): content_length = int(self.environ.get('CONTENT_LENGTH', 0)) except ValueError: # if CONTENT_LENGTH was empty string or not an integer content_length = 0 - safe_copyfileobj(self.environ['wsgi.input'], buf, size=content_length) + if content_length > 0: + safe_copyfileobj(self.environ['wsgi.input'], buf, + size=content_length) self._raw_post_data = buf.getvalue() buf.close() return self._raw_post_data @@ -179,6 +181,7 @@ class WSGIRequest(http.HttpRequest): class WSGIHandler(BaseHandler): initLock = Lock() + request_class = WSGIRequest def __call__(self, environ, start_response): from django.conf import settings @@ -194,13 +197,16 @@ class WSGIHandler(BaseHandler): dispatcher.send(signal=signals.request_started) try: - request = WSGIRequest(environ) - response = self.get_response(request) - - # Apply response middleware - for middleware_method in self._response_middleware: - response = middleware_method(request, response) + try: + request = self.request_class(environ) + except UnicodeDecodeError: + response = http.HttpResponseBadRequest() + else: + response = self.get_response(request) + # Apply response middleware + for middleware_method in self._response_middleware: + response = middleware_method(request, response) finally: dispatcher.send(signal=signals.request_finished) diff --git a/django/core/mail.py b/django/core/mail.py index b90752e649..77ab8007e6 100644 --- a/django/core/mail.py +++ b/django/core/mail.py @@ -73,7 +73,7 @@ class SafeMIMEText(MIMEText): if '\n' in val or '\r' in val: raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) try: - val = str(force_unicode(val)) + val = force_unicode(val).encode('ascii') except UnicodeEncodeError: if name.lower() in ('to', 'from', 'cc'): result = [] @@ -92,7 +92,7 @@ class SafeMIMEMultipart(MIMEMultipart): if '\n' in val or '\r' in val: raise BadHeaderError, "Header values can't contain newlines (got %r for header %r)" % (val, name) try: - val = str(force_unicode(val)) + val = force_unicode(val).encode('ascii') except UnicodeEncodeError: if name.lower() in ('to', 'from', 'cc'): result = [] diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index cd90fd6109..f706fa3c7e 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -1,31 +1,35 @@ -import django -from django.core.management.base import BaseCommand, CommandError, handle_default_options -from optparse import OptionParser import os import sys +from optparse import OptionParser from imp import find_module +import django +from django.core.management.base import BaseCommand, CommandError, handle_default_options + # For backwards compatibility: get_version() used to be in this module. get_version = django.get_version -# A cache of loaded commands, so that call_command +# A cache of loaded commands, so that call_command # doesn't have to reload every time it is called _commands = None def find_commands(management_dir): """ - Given a path to a management directory, return a list of all the command names - that are available. Returns an empty list if no commands are defined. + Given a path to a management directory, returns a list of all the command + names that are available. + + Returns an empty list if no commands are defined. """ - command_dir = os.path.join(management_dir,'commands') + command_dir = os.path.join(management_dir, 'commands') try: - return [f[:-3] for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py')] + return [f[:-3] for f in os.listdir(command_dir) + if not f.startswith('_') and f.endswith('.py')] except OSError: return [] def find_management_module(app_name): """ - Determine the path to the management module for the application named, + Determines the path to the management module for the application named, without acutally importing the application or the management module. Raises ImportError if the management module cannot be found for any reason. @@ -36,23 +40,23 @@ def find_management_module(app_name): path = None while parts: part = parts.pop() - f,path,descr = find_module(part, path and [path] or None) + f, path, descr = find_module(part, path and [path] or None) return path - + def load_command_class(app_name, name): """ - Given a command name and an application name, returns the Command + Given a command name and an application name, returns the Command class instance. All errors raised by the importation process (ImportError, AttributeError) are allowed to propagate. """ - return getattr(__import__('%s.management.commands.%s' % (app_name, name), + return getattr(__import__('%s.management.commands.%s' % (app_name, name), {}, {}, ['Command']), 'Command')() def get_commands(load_user_commands=True, project_directory=None): """ Returns a dictionary of commands against the application in which - those commands can be found. This works by looking for a - management.commands package in django.core, and in each installed + those commands can be found. This works by looking for a + management.commands package in django.core, and in each installed application -- if a commands package exists, all commands in that package are registered. @@ -62,38 +66,38 @@ def get_commands(load_user_commands=True, project_directory=None): startapp command will be modified to use that directory. The dictionary is in the format {command_name: app_name}. Key-value - pairs from this dictionary can then be used in calls to + pairs from this dictionary can then be used in calls to load_command_class(app_name, command_name) - + If a specific version of a command must be loaded (e.g., with the startapp command), the instantiated module can be placed in the dictionary in place of the application name. - + The dictionary is cached on the first call, and reused on subsequent calls. """ global _commands if _commands is None: - _commands = dict([(name, 'django.core') + _commands = dict([(name, 'django.core') for name in find_commands(__path__[0])]) if load_user_commands: - # Get commands from all installed apps + # Get commands from all installed apps. from django.conf import settings for app_name in settings.INSTALLED_APPS: try: path = find_management_module(app_name) - _commands.update(dict([(name, app_name) + _commands.update(dict([(name, app_name) for name in find_commands(path)])) except ImportError: pass # No management module - ignore this app - + if project_directory: # Remove the "startproject" command from self.commands, because # that's a django-admin.py command, not a manage.py command. del _commands['startproject'] # Override the startapp command so that it always uses the - # project_directory, not the current working directory + # project_directory, not the current working directory # (which is default). from django.core.management.commands.startapp import ProjectCommand _commands['startapp'] = ProjectCommand(project_directory) @@ -113,7 +117,7 @@ def call_command(name, *args, **options): """ try: app_name = get_commands()[name] - if isinstance(app_name, BaseCommand): + if isinstance(app_name, BaseCommand): # If the command is already loaded, use it directly. klass = app_name else: @@ -121,16 +125,16 @@ def call_command(name, *args, **options): except KeyError: raise CommandError, "Unknown command: %r" % name return klass.execute(*args, **options) - -class LaxOptionParser(OptionParser): + +class LaxOptionParser(OptionParser): """ An option parser that doesn't raise any errors on unknown options. - + This is needed because the --settings and --pythonpath options affect - the commands (and thus the options) that are available to the user. + the commands (and thus the options) that are available to the user. """ - def error(self, msg): - pass + def error(self, msg): + pass class ManagementUtility(object): """ @@ -144,16 +148,19 @@ class ManagementUtility(object): self.prog_name = os.path.basename(self.argv[0]) self.project_directory = None self.user_commands = False - + def main_help_text(self): """ Returns the script's main help text, as a string. """ usage = ['%s <subcommand> [options] [args]' % self.prog_name] - usage.append('Django command line tool, version %s' % django.get_version()) - usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name) + usage.append('Django command line tool,' + ' version %s' % django.get_version()) + usage.append("Type '%s help <subcommand>' for help on a specific" + " subcommand." % self.prog_name) usage.append('Available subcommands:') - commands = get_commands(self.user_commands, self.project_directory).keys() + commands = get_commands(self.user_commands, + self.project_directory).keys() commands.sort() for cmd in commands: usage.append(' %s' % cmd) @@ -166,33 +173,35 @@ class ManagementUtility(object): django-admin.py or manage.py) if it can't be found. """ try: - app_name = get_commands(self.user_commands, self.project_directory)[subcommand] - if isinstance(app_name, BaseCommand): + app_name = get_commands(self.user_commands, + self.project_directory)[subcommand] + if isinstance(app_name, BaseCommand): # If the command is already loaded, use it directly. klass = app_name else: klass = load_command_class(app_name, subcommand) except KeyError: - sys.stderr.write("Unknown command: %r\nType '%s help' for usage.\n" % (subcommand, self.prog_name)) + sys.stderr.write("Unknown command: %r\nType '%s help' for" + " usage.\n" % (subcommand, self.prog_name)) sys.exit(1) return klass - + def execute(self): """ Given the command-line arguments, this figures out which subcommand is being run, creates a parser appropriate to that command, and runs it. """ - # Preprocess options to extract --settings and --pythonpath. These options - # could affect the commands that are available, so they must be processed - # early - parser = LaxOptionParser(version=get_version(), - option_list=BaseCommand.option_list) + # Preprocess options to extract --settings and --pythonpath. + # These options could affect the commands that are available, so they + # must be processed early. + parser = LaxOptionParser(version=get_version(), + option_list=BaseCommand.option_list) try: - options, args = parser.parse_args(self.argv) + options, args = parser.parse_args(self.argv) handle_default_options(options) - except: + except: pass # Ignore any option errors at this point. - + try: subcommand = self.argv[1] except IndexError: @@ -208,7 +217,8 @@ class ManagementUtility(object): # Special-cases: We want 'django-admin.py --version' and # 'django-admin.py --help' to work, for backwards compatibility. elif self.argv[1:] == ['--version']: - print django.get_version() + # LaxOptionParser already takes care of printing the version. + pass elif self.argv[1:] == ['--help']: sys.stderr.write(self.main_help_text() + '\n') else: @@ -227,10 +237,10 @@ class ProjectManagementUtility(ManagementUtility): super(ProjectManagementUtility, self).__init__(argv) self.project_directory = project_directory self.user_commands = True - + def setup_environ(settings_mod): """ - Configure the runtime environment. This can also be used by external + Configures the runtime environment. This can also be used by external scripts wanting to set up a similar environment to manage.py. """ # Add this project to sys.path so that it's importable in the conventional @@ -244,7 +254,8 @@ def setup_environ(settings_mod): sys.path.pop() # Set DJANGO_SETTINGS_MODULE appropriately. - os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name) + os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, + settings_name) return project_directory def execute_from_command_line(argv=None): diff --git a/django/core/management/validation.py b/django/core/management/validation.py index 9e004d3d25..c433b7fa96 100644 --- a/django/core/management/validation.py +++ b/django/core/management/validation.py @@ -35,6 +35,8 @@ def get_validation_errors(outfile, app=None): for f in opts.fields: if f.name == 'id' and not f.primary_key and opts.pk.name == 'id': e.add(opts, '"%s": You can\'t use "id" as a field name, because each model automatically gets an "id" field if none of the fields have primary_key=True. You need to either remove/rename your "id" field or add primary_key=True to a field.' % f.name) + if f.name.endswith('_'): + e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name) if isinstance(f, models.CharField) and f.max_length in (None, 0): e.add(opts, '"%s": CharFields require a "max_length" attribute.' % f.name) if isinstance(f, models.DecimalField): diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index c512c37b96..2ef3d6f169 100644 --- a/django/core/urlresolvers.py +++ b/django/core/urlresolvers.py @@ -249,8 +249,9 @@ class RegexURLResolver(object): except AttributeError: try: self._urlconf_module = __import__(self.urlconf_name, {}, {}, ['']) - except ValueError, e: - # Invalid urlconf_name, such as "foo.bar." (note trailing period) + except Exception, e: + # Either an invalid urlconf_name, such as "foo.bar.", or some + # kind of problem during the actual import. raise ImproperlyConfigured, "Error while importing URLconf %r: %s" % (self.urlconf_name, e) return self._urlconf_module urlconf_module = property(_get_urlconf_module) |
