From 9125c329969e23a8261685fa976eed4ba90cc172 Mon Sep 17 00:00:00 2001
From: Max Pearl <maxwellbpearl@gmail.com>
Date: Thu, 18 Jun 2020 10:29:56 -0700
Subject: [PATCH] Moving all salesforce variables to environment

---
 .gitignore                           |  2 +-
 docker/Dockerfile.dev                |  2 ++
 docker/docker-compose.yaml           |  2 ++
 hypha/settings/base.py               | 34 +++++++++++++++++++++++-----
 hypha/settings/dev.py                |  8 -------
 hypha/settings/production.py         |  8 -------
 hypha/settings/salesforce.py.example | 14 ------------
 hypha/settings/test.py               |  8 -------
 8 files changed, 33 insertions(+), 45 deletions(-)
 delete mode 100644 hypha/settings/salesforce.py.example

diff --git a/.gitignore b/.gitignore
index 45c723e1b..864632c41 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,7 +12,7 @@
 node_modules/
 /npm-debug.log
 /.idea/
-salesforce.py
+sf.env
 
 # Distribution / packaging
 .Python
diff --git a/docker/Dockerfile.dev b/docker/Dockerfile.dev
index 587a757e4..b66138df1 100644
--- a/docker/Dockerfile.dev
+++ b/docker/Dockerfile.dev
@@ -9,6 +9,8 @@ ENV DATABASE_URL postgres://hypha:hypha@db:5432/hypha
 ENV DJANGO_SETTINGS_MODULE hypha.settings.dev
 ENV PYTHONDONTWRITEBYTECODE 1
 ENV PYTHONUNBUFFERED 1
+
+# Salesforce Integration
 ENV SALESFORCE_INTEGRATION 1
 
 # Set owner on /usr/local.
diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml
index d36e65f4f..5402dbaa8 100644
--- a/docker/docker-compose.yaml
+++ b/docker/docker-compose.yaml
@@ -18,6 +18,8 @@ services:
     build:
       context: ..
       dockerfile: docker/Dockerfile.dev
+    env_file: 
+      - sf.env
     ports:
       - 9001:9001
     volumes:
diff --git a/hypha/settings/base.py b/hypha/settings/base.py
index 08bdafd94..e7d3de977 100644
--- a/hypha/settings/base.py
+++ b/hypha/settings/base.py
@@ -203,12 +203,34 @@ WSGI_APPLICATION = 'hypha.wsgi.application'
 # Database
 # https://docs.djangoproject.com/en/stable/ref/settings/#databases
 
-DATABASES = {
-    'default': dj_database_url.config(
-        conn_max_age=600,
-        default=f"postgres:///{APP_NAME}"
-    )
-}
+if 'SALESFORCE_INTEGRATION' in env and env['SALESFORCE_INTEGRATION']:
+    DATABASES = {
+        'default': dj_database_url.config(
+            conn_max_age=600,
+            default=f"postgres:///{APP_NAME}"
+        ),
+        'salesforce': {
+            'ENGINE': 'salesforce.backend',
+            'CONSUMER_KEY': env['CONSUMER_KEY'],
+            'CONSUMER_SECRET': env['CONSUMER_SECRET'],
+            'USER': env['SALESFORCE_USER'],
+            'PASSWORD': env['SALESFORCE_PASSWORD'],
+            'HOST': env['SALESFORCE_LOGIN_URL']
+        }
+    }
+
+    SALESFORCE_QUERY_TIMEOUT = (30, 30)  # (connect timeout, data timeout)
+
+    DATABASE_ROUTERS = [
+        "salesforce.router.ModelRouter"
+    ]
+else:
+    DATABASES = {
+        'default': dj_database_url.config(
+            conn_max_age=600,
+            default=f"postgres:///{APP_NAME}"
+        )
+    }
 
 
 # Cache
diff --git a/hypha/settings/dev.py b/hypha/settings/dev.py
index f4a4f3fbb..76926f9bf 100644
--- a/hypha/settings/dev.py
+++ b/hypha/settings/dev.py
@@ -145,11 +145,3 @@ DEBUG_TOOLBAR_CONFIG = {
 WEBPACK_LOADER['DEFAULT'].update({
     'STATS_FILE': os.path.join(BASE_DIR, './hypha/static_compiled/app/webpack-stats.json'),
 })
-
-# Salesforce Integration
-if 'SALESFORCE_INTEGRATION' in env and env['SALESFORCE_INTEGRATION']:
-    try:
-        from .salesforce import *
-    except ImportError:
-        pass
-    DATABASES.update(SALESFORCE_DB)
diff --git a/hypha/settings/production.py b/hypha/settings/production.py
index 4111cec13..1c4a98095 100644
--- a/hypha/settings/production.py
+++ b/hypha/settings/production.py
@@ -39,11 +39,3 @@ if 'SENTRY_DSN' in env:
 if 'ON_HEROKU' in env:
     import django_heroku
     django_heroku.settings(locals())
-
-# Salesforce Integration
-if 'SALESFORCE_INTEGRATION' in env and env['SALESFORCE_INTEGRATION']:
-    try:
-        from .salesforce import *
-    except ImportError:
-        pass
-    DATABASES.update(SALESFORCE_DB)
diff --git a/hypha/settings/salesforce.py.example b/hypha/settings/salesforce.py.example
deleted file mode 100644
index 63bdb82ce..000000000
--- a/hypha/settings/salesforce.py.example
+++ /dev/null
@@ -1,14 +0,0 @@
-SALESFORCE_DB = {
-    'ENGINE': 'salesforce.backend',
-    'CONSUMER_KEY': 'Consumer Key from Salesforce App',
-    'CONSUMER_SECRET': 'Consumer Secret from Salesforce APP',
-    'USER': 'user@email.com',
-    'PASSWORD': 'Password and security token concatenated',
-    'HOST': 'https://login.salesforce.com' # Or test or URL of salesforce org
-}
-
-SALESFORCE_QUERY_TIMEOUT = (30, 30)  # (connect timeout, data timeout)
-
-DATABASE_ROUTERS = [
-    "salesforce.router.ModelRouter"
-]
diff --git a/hypha/settings/test.py b/hypha/settings/test.py
index cac29f9a7..610dc3b03 100644
--- a/hypha/settings/test.py
+++ b/hypha/settings/test.py
@@ -17,11 +17,3 @@ STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
 PASSWORD_HASHERS = [
     'django.contrib.auth.hashers.MD5PasswordHasher',
 ]
-
-# Salesforce Integration
-if 'SALESFORCE_INTEGRATION' in env and env['SALESFORCE_INTEGRATION']:
-    try:
-        from .salesforce import *
-    except ImportError:
-        pass
-    DATABASES.update(SALESFORCE_DB)
-- 
GitLab