From a8c87962723c29a446265c8fda87f1bb9d958bdd Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Sun, 6 Oct 2019 22:14:46 +0800 Subject: [PATCH 1/5] Migrate to django-environ This patch uses django-environ and drop deprecated and old packages django-cache-url, dj-email-url, dj-database-url and dj-cache-url. Tests are updated according to the behavior of django-environ. Signed-off-by: Chenxiong Qi --- configurations/values.py | 9 +++++---- setup.py | 9 +-------- tests/test_values.py | 19 ++++--------------- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/configurations/values.py b/configurations/values.py index 0f96e3d..a8e1847 100644 --- a/configurations/values.py +++ b/configurations/values.py @@ -7,6 +7,7 @@ import sys from django.core import validators from django.core.exceptions import ValidationError, ImproperlyConfigured from django.utils import six +from environ import Env from .utils import import_by_path, getargspec @@ -414,7 +415,7 @@ class SecretValue(Value): class EmailURLValue(CastingMixin, MultipleMixin, Value): - caster = 'dj_email_url.parse' + caster = Env.email_url_config message = 'Cannot interpret email URL value {0!r}' late_binding = True @@ -449,21 +450,21 @@ class DictBackendMixin(Value): class DatabaseURLValue(DictBackendMixin, CastingMixin, Value): - caster = 'dj_database_url.parse' + caster = Env.db_url_config message = 'Cannot interpret database URL value {0!r}' environ_name = 'DATABASE_URL' late_binding = True class CacheURLValue(DictBackendMixin, CastingMixin, Value): - caster = 'django_cache_url.parse' + caster = Env.cache_url_config message = 'Cannot interpret cache URL value {0!r}' environ_name = 'CACHE_URL' late_binding = True class SearchURLValue(DictBackendMixin, CastingMixin, Value): - caster = 'dj_search_url.parse' + caster = Env.search_url_config message = 'Cannot interpret Search URL value {0!r}' environ_name = 'SEARCH_URL' late_binding = True diff --git a/setup.py b/setup.py index 7a2efdf..7ffb01a 100644 --- a/setup.py +++ b/setup.py @@ -41,18 +41,11 @@ setup( 'django-cadmin = configurations.management:execute_from_command_line', ], }, + install_requires=['django-environ'], extras_require={ - 'cache': ['django-cache-url'], - 'database': ['dj-database-url'], - 'email': ['dj-email-url'], - 'search': ['dj-search-url'], 'testing': [ 'django-discover-runner', 'mock', - 'django-cache-url>=1.0.0', - 'dj-database-url', - 'dj-email-url', - 'dj-search-url', 'six', 'Sphinx>=1.4', ], diff --git a/tests/test_values.py b/tests/test_values.py index 853b4be..4c387fe 100644 --- a/tests/test_values.py +++ b/tests/test_values.py @@ -374,7 +374,6 @@ class ValueTests(TestCase): with env(DATABASE_URL='sqlite://'): self.assertEqual(value.setup('DATABASE_URL'), { 'default': { - 'CONN_MAX_AGE': 0, 'ENGINE': 'django.db.backends.sqlite3', 'HOST': '', 'NAME': ':memory:', @@ -411,18 +410,15 @@ class ValueTests(TestCase): 'EMAIL_HOST_PASSWORD': 'password', 'EMAIL_HOST_USER': 'user@domain.com', 'EMAIL_PORT': 587, - 'EMAIL_USE_SSL': False, 'EMAIL_USE_TLS': True}) - with env(EMAIL_URL='console://'): + with env(EMAIL_URL='consolemail://'): self.assertEqual(value.setup('EMAIL_URL'), { 'EMAIL_BACKEND': 'django.core.mail.backends.console.EmailBackend', # noqa: E501 'EMAIL_FILE_PATH': '', 'EMAIL_HOST': None, 'EMAIL_HOST_PASSWORD': None, 'EMAIL_HOST_USER': None, - 'EMAIL_PORT': None, - 'EMAIL_USE_SSL': False, - 'EMAIL_USE_TLS': False}) + 'EMAIL_PORT': None}) with env(EMAIL_URL='smtps://user@domain.com:password@smtp.example.com:wrong'): # noqa: E501 self.assertRaises(ValueError, value.setup, 'TEST') @@ -430,7 +426,7 @@ class ValueTests(TestCase): cache_setting = { 'default': { 'BACKEND': 'django_redis.cache.RedisCache', - 'LOCATION': 'redis://host:6379/1', + 'LOCATION': 'redis://user@host:6379/1', } } cache_url = 'redis://user@host:6379/1' @@ -443,13 +439,7 @@ class ValueTests(TestCase): with env(CACHE_URL='wrong://user@host:port/1'): with self.assertRaises(Exception) as cm: value.setup('TEST') - self.assertEqual(cm.exception.args[0], 'Unknown backend: "wrong"') - with env(CACHE_URL='redis://user@host:port/1'): - with self.assertRaises(ValueError) as cm: - value.setup('TEST') - self.assertEqual( - cm.exception.args[0], - "Cannot interpret cache URL value 'redis://user@host:port/1'") + self.assertEqual(cm.exception.args[0], 'wrong') def test_search_url_value(self): value = SearchURLValue() @@ -503,7 +493,6 @@ class ValueTests(TestCase): 'EMAIL_HOST_PASSWORD': 'password', 'EMAIL_HOST_USER': 'user@domain.com', 'EMAIL_PORT': 587, - 'EMAIL_USE_SSL': False, 'EMAIL_USE_TLS': True }) self.assertEqual( From f3763410b186f5b9b699484f624d81e583480d47 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Sat, 12 Oct 2019 11:21:34 +0800 Subject: [PATCH 2/5] Warn email schema console:// to be deprecated Apps using schema console:// still works, which is converted to consolemail:// internally in order to work with django-environ. Meanwhile, a deprecation warning is output for console://. Signed-off-by: Chenxiong Qi --- configurations/values.py | 19 ++++++++++++++++++- tests/test_values.py | 6 +++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/configurations/values.py b/configurations/values.py index a8e1847..7cfffba 100644 --- a/configurations/values.py +++ b/configurations/values.py @@ -3,6 +3,7 @@ import copy import decimal import os import sys +import warnings from django.core import validators from django.core.exceptions import ValidationError, ImproperlyConfigured @@ -414,8 +415,24 @@ class SecretValue(Value): return value +def env_email_url_config(cls, url, backend=None): + """ + Convert schema to consolemail in order to be compatible with old + console:// schema. + + This could be removed and set EmailURLValue.caster to Env.email_url_config + directly after the deprecation is removed. + """ + if url.startswith('console://'): + warnings.warn('Email schema console:// is deprecated. Please use ' + 'consolemail:// in new code.', + DeprecationWarning) + url = url.replace('console://', 'consolemail://') + return Env.email_url_config(url, backend=backend) + + class EmailURLValue(CastingMixin, MultipleMixin, Value): - caster = Env.email_url_config + caster = env_email_url_config message = 'Cannot interpret email URL value {0!r}' late_binding = True diff --git a/tests/test_values.py b/tests/test_values.py index 4c387fe..e967582 100644 --- a/tests/test_values.py +++ b/tests/test_values.py @@ -411,7 +411,7 @@ class ValueTests(TestCase): 'EMAIL_HOST_USER': 'user@domain.com', 'EMAIL_PORT': 587, 'EMAIL_USE_TLS': True}) - with env(EMAIL_URL='consolemail://'): + with env(EMAIL_URL='console://'): self.assertEqual(value.setup('EMAIL_URL'), { 'EMAIL_BACKEND': 'django.core.mail.backends.console.EmailBackend', # noqa: E501 'EMAIL_FILE_PATH': '', @@ -419,6 +419,10 @@ class ValueTests(TestCase): 'EMAIL_HOST_PASSWORD': None, 'EMAIL_HOST_USER': None, 'EMAIL_PORT': None}) + with env(EMAIL_URL='console://'): + with patch('warnings.warn') as warn: + value.setup('EMAIL_URL') + warn.asset_called_once() with env(EMAIL_URL='smtps://user@domain.com:password@smtp.example.com:wrong'): # noqa: E501 self.assertRaises(ValueError, value.setup, 'TEST') From ec51904bd710a44d72c1e19b0201d10b165cfd9f Mon Sep 17 00:00:00 2001 From: Asif Saif Uddin Date: Wed, 27 Oct 2021 14:13:40 +0600 Subject: [PATCH 3/5] Update setup.py Co-authored-by: Brian Helba --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 0ad1e30..9637f5d 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,6 @@ setup( extras_require={ 'testing': [ 'django-discover-runner', - 'mock', 'django-cache-url>=1.0.0', 'dj-database-url', 'dj-email-url', From 3077eaf20018d47a37fb87ac308d7cd1fbd8a86f Mon Sep 17 00:00:00 2001 From: Asif Saif Uddin Date: Wed, 27 Oct 2021 14:14:04 +0600 Subject: [PATCH 4/5] Update setup.py Co-authored-by: Brian Helba --- setup.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 9637f5d..20cfa9e 100644 --- a/setup.py +++ b/setup.py @@ -27,9 +27,10 @@ setup( ], }, - install_requires=['django-environ', - 'django>=2.2', - 'importlib-metadata;python_version<"3.8"', + install_requires=[ + 'django>=2.2', + 'django-environ', + 'importlib-metadata;python_version<"3.8"', ], extras_require={ 'testing': [ From fb7417fa7623011ff1a199613aa2638d300e8cd2 Mon Sep 17 00:00:00 2001 From: Asif Saif Uddin Date: Wed, 27 Oct 2021 14:14:24 +0600 Subject: [PATCH 5/5] Update setup.py Co-authored-by: Brian Helba --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 20cfa9e..e3864ed 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,6 @@ setup( ], extras_require={ 'testing': [ - 'django-discover-runner', 'django-cache-url>=1.0.0', 'dj-database-url', 'dj-email-url',