Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 101 additions & 4 deletions django/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,44 @@
DEFAULT_STORAGE_ALIAS = "default"
STATICFILES_STORAGE_ALIAS = "staticfiles"

# RemovedInDjango70Warning.
USE_BLANK_CHOICE_DASH_DEPRECATED_MSG = (
"The USE_BLANK_CHOICE_DASH setting is deprecated. If you wish to define "
"your own default blank choice label, override "
"django.db.models.fields.BLANK_CHOICE_LABEL in your app's ready() method."
)

# RemovedInDjango70Warning.
DEPRECATED_EMAIL_SETTINGS = {
"EMAIL_BACKEND",
"EMAIL_FILE_PATH",
"EMAIL_HOST",
"EMAIL_HOST_PASSWORD",
"EMAIL_HOST_USER",
"EMAIL_PORT",
"EMAIL_SSL_CERTFILE",
"EMAIL_SSL_KEYFILE",
"EMAIL_TIMEOUT",
"EMAIL_USE_SSL",
"EMAIL_USE_TLS",
}
EMAIL_SETTING_DEPRECATED_MSG = (
"The {name} setting is deprecated. Migrate to MAILERS before Django 7.0."
)


# RemovedInDjango70Warning.
# Must be called with the complete set of user-defined setting names (but no
# default settings).
def _check_email_settings_conflicts(explicit_settings):
deprecated = DEPRECATED_EMAIL_SETTINGS.intersection(explicit_settings)
if deprecated and "MAILERS" in explicit_settings:
deprecated_str = ", ".join(sorted(deprecated))
raise ImproperlyConfigured(
"Deprecated email settings are not allowed when MAILERS is "
f"defined: {deprecated_str}."
)


class SettingsReference(str):
"""
Expand Down Expand Up @@ -85,6 +117,16 @@ def __getattr__(self, name):
_wrapped = self._wrapped
val = getattr(_wrapped, name)

# RemovedInDjango70Warning.
if name in DEPRECATED_EMAIL_SETTINGS:
if hasattr(_wrapped, "MAILERS"):
raise AttributeError(
f"The {name} setting is not available when MAILERS is defined."
)
_show_settings_deprecation_warning(
EMAIL_SETTING_DEPRECATED_MSG.format(name=name), RemovedInDjango70Warning
)

# Special case some settings which require further modification.
# This is done here for performance reasons so the modified value is
# cached.
Expand All @@ -105,13 +147,44 @@ def __setattr__(self, name, value):
self.__dict__.clear()
else:
self.__dict__.pop(name, None)

# RemovedInDjango70Warning.
if name == "USE_BLANK_CHOICE_DASH":
_show_settings_deprecation_warning(
USE_BLANK_CHOICE_DASH_DEPRECATED_MSG, RemovedInDjango70Warning
)
# RemovedInDjango70Warning.
if name == "MAILERS":
# When MAILERS is set, clear any cached values of
# deprecated settings so that __getattr__() runs again for them.
for setting in DEPRECATED_EMAIL_SETTINGS:
self.__dict__.pop(setting, None)
if name in DEPRECATED_EMAIL_SETTINGS:
_show_settings_deprecation_warning(
EMAIL_SETTING_DEPRECATED_MSG.format(name=name), RemovedInDjango70Warning
)

super().__setattr__(name, value)

def __delattr__(self, name):
"""Delete a setting and clear it from cache if needed."""
super().__delattr__(name)
self.__dict__.pop(name, None)

# RemovedInDjango70Warning.
def __dir__(self):
attrs = super().__dir__()
if hasattr(self._wrapped, "MAILERS"):
# When MAILERS is defined, filter out deprecated email
# settings that are from the global_settings defaults.
attrs = [
name
for name in attrs
if name not in DEPRECATED_EMAIL_SETTINGS
or self._wrapped.is_overridden(name)
]
return attrs

def configure(self, default_settings=global_settings, **options):
"""
Called to manually configure the settings. The 'default_settings'
Expand All @@ -120,6 +193,10 @@ def configure(self, default_settings=global_settings, **options):
"""
if self._wrapped is not empty:
raise RuntimeError("Settings already configured.")

# RemovedInDjango70Warning.
_check_email_settings_conflicts(options.keys())

holder = UserSettingsHolder(default_settings)
for name, value in options.items():
if not name.isupper():
Expand Down Expand Up @@ -182,6 +259,22 @@ def __init__(self, settings_module):
setattr(self, setting, setting_value)
self._explicit_settings.add(setting)

# RemovedInDjango70Warning.
if "USE_BLANK_CHOICE_DASH" in self._explicit_settings:
warnings.warn(
USE_BLANK_CHOICE_DASH_DEPRECATED_MSG,
RemovedInDjango70Warning,
skip_file_prefixes=django_file_prefixes(),
)
# RemovedInDjango70Warning.
_check_email_settings_conflicts(self._explicit_settings)
for name in DEPRECATED_EMAIL_SETTINGS.intersection(self._explicit_settings):
warnings.warn(
EMAIL_SETTING_DEPRECATED_MSG.format(name=name),
RemovedInDjango70Warning,
skip_file_prefixes=django_file_prefixes(),
)

if hasattr(time, "tzset") and self.TIME_ZONE:
# When we can, attempt to validate the timezone. If we can't find
# this file, no check happens and it's harmless.
Expand Down Expand Up @@ -227,11 +320,15 @@ def __getattr__(self, name):
def __setattr__(self, name, value):
self._deleted.discard(name)
if name == "USE_BLANK_CHOICE_DASH":
warnings.warn(
USE_BLANK_CHOICE_DASH_DEPRECATED_MSG,
RemovedInDjango70Warning,
skip_file_prefixes=django_file_prefixes(),
_show_settings_deprecation_warning(
USE_BLANK_CHOICE_DASH_DEPRECATED_MSG, RemovedInDjango70Warning
)
# RemovedInDjango70Warning.
if name in DEPRECATED_EMAIL_SETTINGS:
_show_settings_deprecation_warning(
EMAIL_SETTING_DEPRECATED_MSG.format(name=name), RemovedInDjango70Warning
)

super().__setattr__(name, value)

def __delattr__(self, name):
Expand Down
8 changes: 8 additions & 0 deletions django/conf/global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,29 @@ def gettext_noop(s):
# Classes used to implement DB routing behavior.
DATABASE_ROUTERS = []

# Mailer configurations. No mailers are defined by default.
# RemovedInDjango70Warning: uncomment the next line.
# MAILERS = {}

# RemovedInDjango70Warning.
# The email backend to use. For possible shortcuts see django.core.mail.
# The default is to use the SMTP backend.
# Third-party backends can be specified by providing a Python path
# to a module that defines an EmailBackend class.
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"

# RemovedInDjango70Warning.
# Host for sending email.
EMAIL_HOST = "localhost"

# RemovedInDjango70Warning.
# Port for sending email.
EMAIL_PORT = 25

# Whether to send SMTP 'Date' header in the local time zone or in UTC.
EMAIL_USE_LOCALTIME = False

# RemovedInDjango70Warning.
# Optional SMTP authentication information for EMAIL_HOST.
EMAIL_HOST_USER = ""
EMAIL_HOST_PASSWORD = ""
Expand Down
10 changes: 10 additions & 0 deletions django/conf/project_template/project_name/settings.py-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,13 @@ USE_TZ = True
# https://docs.djangoproject.com/en/{{ docs_version }}/howto/static-files/

STATIC_URL = 'static/'


# Email
# https://docs.djangoproject.com/en/{{ docs_version }}/topics/email/#topic-email-configuration

MAILERS = {
'default': {
'BACKEND': 'django.core.mail.backends.console.EmailBackend',
},
}
Loading
Loading