Repository: CiviWiki/OpenCiviWiki

Issue:
Remove django-environ
We currently load environment variables using django-environ in settings.py. However, django-environment only supports Django up to around version 2. Since we aim to use the latest Django LTS (version 3.2 #848) we should remove django-environ.

Remove django-environ and use `os.getenv` instead. Any code using environment variables should be allowed to have a None value for the environment variable, with sensible defaults. 


Discussion:

Resolving patch:
diff --git a/project/civiwiki/settings.py b/project/civiwiki/settings.py
--- a/project/civiwiki/settings.py
+++ b/project/civiwiki/settings.py
@@ -5,24 +5,15 @@
 Production settings file to select proper environment variables.
 """
 import os
-import environ
-
-from django.core.exceptions import ImproperlyConfigured
-
-env = environ.Env(
-    # set casting, default value
-    DEBUG=(bool, False)
-)
-# reading .env file
-environ.Env.read_env()
 
 # False if not in os.environ
-DEBUG = env("DEBUG")
+DEBUG = os.getenv("DEBUG", False)
 
-DJANGO_HOST = env("DJANGO_HOST", default="LOCALHOST")
+# defaults to second value if not found in os.environ
+DJANGO_HOST = os.getenv("DJANGO_HOST", "LOCALHOST")
 
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
-SECRET_KEY = env("DJANGO_SECRET_KEY", default="TEST_KEY_FOR_DEVELOPMENT")
+SECRET_KEY = os.getenv("DJANGO_SECRET_KEY", "TEST_KEY_FOR_DEVELOPMENT")
 ALLOWED_HOSTS = [".herokuapp.com", ".civiwiki.org", "127.0.0.1", "localhost", "0.0.0.0"]
 
 INSTALLED_APPS = (
@@ -100,17 +91,18 @@
 WSGI_APPLICATION = "civiwiki.wsgi.application"
 
 # Global user privilege settings
-CLOSED_BETA = env("CLOSED_BETA", default=False)
+CLOSED_BETA = os.getenv("CLOSED_BETA", False)
 
 # Apex Contact for Production Errors
 ADMINS = [("Development Team", "dev@civiwiki.org")]
 
 # API keys
-SUNLIGHT_API_KEY = env("SUNLIGHT_API_KEY")
-GOOGLE_API_KEY = env("GOOGLE_MAP_API_KEY")
+# returns None if not found in os.environ
+SUNLIGHT_API_KEY = os.getenv("SUNLIGHT_API_KEY")
+GOOGLE_API_KEY = os.getenv("GOOGLE_MAP_API_KEY")
 
 # Channels Setup
-REDIS_URL = env("REDIS_URL", default="redis://localhost:6379")
+REDIS_URL = os.getenv("REDIS_URL", default="redis://localhost:6379")
 CHANNEL_LAYERS = {
     "default": {
         "BACKEND": "asgi_redis.RedisChannelLayer",
@@ -134,9 +126,9 @@
     MEDIA_URL = "/media/"
     MEDIA_ROOT = os.path.join(BASE_DIR, "media")
 else:
-    AWS_STORAGE_BUCKET_NAME = env("AWS_STORAGE_BUCKET_NAME")
-    AWS_S3_ACCESS_KEY_ID = env("AWS_S3_ACCESS_KEY_ID")
-    AWS_S3_SECRET_ACCESS_KEY = env("AWS_S3_SECRET_ACCESS_KEY")
+    AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STORAGE_BUCKET_NAME")
+    AWS_S3_ACCESS_KEY_ID = os.getenv("AWS_S3_ACCESS_KEY_ID")
+    AWS_S3_SECRET_ACCESS_KEY = os.getenv("AWS_S3_SECRET_ACCESS_KEY")
     DEFAULT_FILE_STORAGE = "storages.backends.s3boto.S3BotoStorage"
     AWS_S3_SECURE_URLS = False
     AWS_QUERYSTRING_AUTH = False
@@ -149,16 +141,16 @@
 if "CIVIWIKI_LOCAL_NAME" not in os.environ:
     STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
 
-    DATABASES = {"default": env.db()}
+    DATABASES = {"default": os.getenv("DATABASE_URL")}
 else:
     DATABASES = {
         "default": {
-            "HOST": env("CIVIWIKI_LOCAL_DB_HOST", "localhost"),
+            "HOST": os.getenv("CIVIWIKI_LOCAL_DB_HOST", "localhost"),
             "PORT": "5432",
-            "NAME": env("CIVIWIKI_LOCAL_NAME"),
+            "NAME": os.getenv("CIVIWIKI_LOCAL_NAME"),
             "ENGINE": "django.db.backends.postgresql_psycopg2",
-            "USER": env("CIVIWIKI_LOCAL_USERNAME"),
-            "PASSWORD": env("CIVIWIKI_LOCAL_PASSWORD"),
+            "USER": os.getenv("CIVIWIKI_LOCAL_USERNAME"),
+            "PASSWORD": os.getenv("CIVIWIKI_LOCAL_PASSWORD"),
         },
     }
 
@@ -168,10 +160,10 @@
     EMAIL_HOST_USER = "test@civiwiki.org"
 else:
     EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
-    EMAIL_HOST = env("EMAIL_HOST")
-    EMAIL_PORT = env("EMAIL_PORT")
-    EMAIL_HOST_USER = env("EMAIL_HOST_USER")
-    EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD")
+    EMAIL_HOST = os.getenv("EMAIL_HOST")
+    EMAIL_PORT = os.getenv("EMAIL_PORT")
+    EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER")
+    EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD")
     EMAIL_USE_SSL = True
     DEFAULT_FROM_EMAIL = EMAIL_HOST
 
@@ -201,4 +193,4 @@
 }
 # CORS Settings
 CORS_ORIGIN_ALLOW_ALL = True
-PROPUBLICA_API_KEY = env("PROPUBLICA_API_KEY", default="TEST")
+PROPUBLICA_API_KEY = os.getenv("PROPUBLICA_API_KEY", "TEST")


Test patch: