From 234a2e0b6bcad5536d5d6cbb398eb6b7cc4eb67e Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 25 Dec 2014 13:30:37 +0100 Subject: [PATCH] Fixed #23866 -- Harmonized refs to Django documentation from code --- django/contrib/gis/db/models/__init__.py | 3 ++- django/core/management/commands/squashmigrations.py | 5 +++-- django/core/management/sql.py | 9 ++++++--- django/core/management/templates.py | 7 ++----- django/db/migrations/state.py | 5 +++-- django/db/migrations/writer.py | 9 +++++---- django/utils/deconstruct.py | 6 ++++-- django/utils/version.py | 8 ++++++++ django/views/csrf.py | 6 ++++-- 9 files changed, 37 insertions(+), 21 deletions(-) diff --git a/django/contrib/gis/db/models/__init__.py b/django/contrib/gis/db/models/__init__.py index 9be4a18e03..0ab75a1f05 100644 --- a/django/contrib/gis/db/models/__init__.py +++ b/django/contrib/gis/db/models/__init__.py @@ -2,13 +2,14 @@ from django.core.exceptions import ImproperlyConfigured # Want to get everything from the 'normal' models package. from django.db.models import * # NOQA +from django.utils.version import get_docs_version from django.contrib.gis.geos import HAS_GEOS if not HAS_GEOS: raise ImproperlyConfigured( "GEOS is required and has not been detected. Are you sure it is installed? " - "See also https://docs.djangoproject.com/en/stable/ref/contrib/gis/install/geolibs/") + "See also https://docs.djangoproject.com/en/%s/ref/contrib/gis/install/geolibs/" % get_docs_version()) # Geographic aggregate functions from django.contrib.gis.db.models.aggregates import * # NOQA diff --git a/django/core/management/commands/squashmigrations.py b/django/core/management/commands/squashmigrations.py index 6f598757b6..13dcb6da67 100644 --- a/django/core/management/commands/squashmigrations.py +++ b/django/core/management/commands/squashmigrations.py @@ -1,5 +1,4 @@ from django.core.management.base import BaseCommand, CommandError -from django.utils import six from django.conf import settings from django.db import connections, DEFAULT_DB_ALIAS, migrations from django.db.migrations.loader import AmbiguityError @@ -7,6 +6,8 @@ from django.db.migrations.executor import MigrationExecutor from django.db.migrations.writer import MigrationWriter from django.db.migrations.optimizer import MigrationOptimizer from django.db.migrations.migration import SwappableTuple +from django.utils import six +from django.utils.version import get_docs_version class Command(BaseCommand): @@ -85,7 +86,7 @@ class Command(BaseCommand): raise CommandError( "You cannot squash squashed migrations! Please transition " "it to a normal migration first: " - "https://docs.djangoproject.com/en/1.7/topics/migrations/#squashing-migrations" + "https://docs.djangoproject.com/en/%s/topics/migrations/#squashing-migrations" % get_docs_version() ) operations.extend(smigration.operations) for dependency in smigration.dependencies: diff --git a/django/core/management/sql.py b/django/core/management/sql.py index 955d8391b3..a1897054de 100644 --- a/django/core/management/sql.py +++ b/django/core/management/sql.py @@ -10,6 +10,7 @@ from django.conf import settings from django.core.management.base import CommandError from django.db import models, router from django.utils.deprecation import RemovedInDjango19Warning +from django.utils.version import get_docs_version def check_for_migrations(app_config, connection): @@ -31,9 +32,11 @@ def sql_create(app_config, style, connection): if connection.settings_dict['ENGINE'] == 'django.db.backends.dummy': # This must be the "dummy" database backend, which means the user # hasn't set ENGINE for the database. - raise CommandError("Django doesn't know which syntax to use for your SQL statements,\n" + - "because you haven't properly specified the ENGINE setting for the database.\n" + - "see: https://docs.djangoproject.com/en/dev/ref/settings/#databases") + raise CommandError( + "Django doesn't know which syntax to use for your SQL statements,\n" + "because you haven't properly specified the ENGINE setting for the database.\n" + "see: https://docs.djangoproject.com/en/%s/ref/settings/#databases" % get_docs_version() + ) # Get installed models, so we generate REFERENCES right. # We trim models from the current app so that the sqlreset command does not diff --git a/django/core/management/templates.py b/django/core/management/templates.py index b67a1b1fb2..b0bd7a7117 100644 --- a/django/core/management/templates.py +++ b/django/core/management/templates.py @@ -16,6 +16,7 @@ from django.template import Template, Context from django.utils import archive from django.utils.six.moves.urllib.request import urlretrieve from django.utils._os import rmtree_errorhandler +from django.utils.version import get_docs_version from django.core.management.base import BaseCommand, CommandError from django.core.management.utils import handle_extensions @@ -100,15 +101,11 @@ class TemplateCommand(BaseCommand): base_name = '%s_name' % app_or_project base_subdir = '%s_template' % app_or_project base_directory = '%s_directory' % app_or_project - if django.VERSION[-2] != 'final': - docs_version = 'dev' - else: - docs_version = '%d.%d' % django.VERSION[:2] context = Context(dict(options, **{ base_name: name, base_directory: top_dir, - 'docs_version': docs_version, + 'docs_version': get_docs_version(), 'django_version': django.__version__, }), autoescape=False) diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index 797afef722..871e9920fa 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -10,6 +10,7 @@ from django.conf import settings from django.utils import six from django.utils.encoding import force_text, smart_text from django.utils.module_loading import import_string +from django.utils.version import get_docs_version class InvalidBasesError(ValueError): @@ -71,8 +72,8 @@ class ProjectState(object): raise InvalidBasesError( "Cannot resolve bases for %r\nThis can happen if you are inheriting models from an " "app with migrations (e.g. contrib.auth)\n in an app with no migrations; see " - "https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies " - "for more" % new_unrendered_models + "https://docs.djangoproject.com/en/%s/topics/migrations/#dependencies " + "for more" % (new_unrendered_models, get_docs_version()) ) unrendered_models = new_unrendered_models # make sure apps has no dangling references diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 75089ae2e2..3c4bbb0427 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -18,6 +18,7 @@ from django.utils import datetime_safe, six from django.utils.encoding import force_text from django.utils.functional import Promise from django.utils.timezone import utc +from django.utils.version import get_docs_version COMPILED_REGEX_TYPE = type(re.compile('')) @@ -389,8 +390,8 @@ class MigrationWriter(object): "declared and used in the same class body). Please move " "the function into the main module body to use migrations.\n" "For more information, see " - "https://docs.djangoproject.com/en/dev/topics/migrations/#serializing-values" - % (value.__name__, module_name)) + "https://docs.djangoproject.com/en/%s/topics/migrations/#serializing-values" + % (value.__name__, module_name, get_docs_version())) return "%s.%s" % (module_name, value.__name__), {"import %s" % module_name} # Other iterables elif isinstance(value, collections.Iterable): @@ -419,8 +420,8 @@ class MigrationWriter(object): else: raise ValueError( "Cannot serialize: %r\nThere are some values Django cannot serialize into " - "migration files.\nFor more, see https://docs.djangoproject.com/en/dev/" - "topics/migrations/#migration-serializing" % value + "migration files.\nFor more, see https://docs.djangoproject.com/en/%s/" + "topics/migrations/#migration-serializing" % (value, get_docs_version()) ) diff --git a/django/utils/deconstruct.py b/django/utils/deconstruct.py index a51a16961d..e0713a6d61 100644 --- a/django/utils/deconstruct.py +++ b/django/utils/deconstruct.py @@ -1,6 +1,8 @@ from __future__ import absolute_import # Avoid importing `importlib` from this package. from importlib import import_module +from django.utils.version import get_docs_version + def deconstructible(*args, **kwargs): """ @@ -38,8 +40,8 @@ def deconstructible(*args, **kwargs): "classes. Please move the object into the main module " "body to use migrations.\n" "For more information, see " - "https://docs.djangoproject.com/en/dev/topics/migrations/#serializing-values" - % (name, module_name)) + "https://docs.djangoproject.com/en/%s/topics/migrations/#serializing-values" + % (name, module_name, get_docs_version())) return ( path or '%s.%s' % (obj.__class__.__module__, name), obj._constructor_args[0], diff --git a/django/utils/version.py b/django/utils/version.py index 0d55808a8d..f73568bb9a 100644 --- a/django/utils/version.py +++ b/django/utils/version.py @@ -52,6 +52,14 @@ def get_complete_version(version=None): return version +def get_docs_version(version=None): + version = get_complete_version(version) + if version[3] != 'final': + return 'dev' + else: + return '%d.%d' % version[:2] + + @lru_cache() def get_git_changeset(): """Returns a numeric identifier of the latest git changeset. diff --git a/django/views/csrf.py b/django/views/csrf.py index b8de4851e7..9018bd981a 100644 --- a/django/views/csrf.py +++ b/django/views/csrf.py @@ -2,6 +2,7 @@ from django.conf import settings from django.http import HttpResponseForbidden from django.template import Context, Template from django.utils.translation import ugettext as _ +from django.utils.version import get_docs_version # We include the template inline since we need to be able to reliably display # this error message, especially for the sake of developers, and there isn't any @@ -58,7 +59,7 @@ CSRF_FAILURE_TEMPLATE = """

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's + href='https://docs.djangoproject.com/en/{{ docs_version }}/ref/contrib/csrf/#ref-contrib-csrf'>Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:

@@ -66,7 +67,7 @@ CSRF_FAILURE_TEMPLATE = """
  • Your browser is accepting cookies.
  • The view function uses RequestContext + href='https://docs.djangoproject.com/en/{{ docs_version }}/ref/templates/api/#subclassing-context-requestcontext'>RequestContext for the template, instead of Context.
  • In the template, there is a {% templatetag openblock %} csrf_token @@ -126,6 +127,7 @@ def csrf_failure(request, reason=""): "re-enable them, at least for this site, or for 'same-origin' " "requests."), 'DEBUG': settings.DEBUG, + 'docs_version': get_docs_version(), 'more': _("More information is available with DEBUG=True."), }) return HttpResponseForbidden(t.render(c), content_type='text/html')