diff --git a/CodeReview/load_env.py b/CodeReview/load_env.py deleted file mode 100644 index 4c64a58..0000000 --- a/CodeReview/load_env.py +++ /dev/null @@ -1,42 +0,0 @@ -""" - This file is used to load environment variables from a powershell file -""" - -import os -from typing import Optional - - -def get_env(filename: str) -> Optional[dict]: - """ - This function is used to get environment variables from a powershell file as a dictionary - - :param filename: The file to read - :type filename: str - :returns: The environment variables in the file as a dictionary - :rtype: dict - """ - - env = {} - if os.path.exists(filename): - with open(filename, 'r') as file: - for raw_line in file.readlines(): - if raw_line[0] != "#": - line = raw_line[5:].split('=', 1) - env[line[0]] = line[1].replace('"', "")[:-1] - return env - else: - return None - - -def load_to_env(filename: str) -> None: - """ - This function is used to load a powershell file into the environment - - :param filename: The name/path of the powershell file to load - :type filename: str - """ - - env = get_env(filename) - if env is not None: - for key in env.keys(): - os.environ[key] = env[key] diff --git a/CodeReview/wsgi.py b/CodeReview/wsgi.py index 679b012..6865074 100644 --- a/CodeReview/wsgi.py +++ b/CodeReview/wsgi.py @@ -3,13 +3,9 @@ It acts as the entrypoint of the site, we point the front-end webserver to this file """ -import os - from django.core.wsgi import get_wsgi_application +from dotenv import load_dotenv -from CodeReview.load_env import load_to_env - -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'CodeReview.settings') -load_to_env('env.ps1') +load_dotenv() application = get_wsgi_application() diff --git a/Pipfile b/Pipfile index 4b7cae7..c7103c4 100644 --- a/Pipfile +++ b/Pipfile @@ -21,6 +21,7 @@ tzdata = "==2022.1" zipp = "==3.7.0" Django = "==4.0.3" wfastcgi = "*" +python-dotenv = "*" [dev-packages] django-debug-toolbar = "*" diff --git a/Pipfile.lock b/Pipfile.lock index 89d7d8a..bf292c7 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "ec504cdac65b525f0b0f1e60f22051224047038d2a7c2fdb1934bd702a5021ee" + "sha256": "15b4c08719163c67a30d696d51165c59cb1e03be9c4443e8e539fd1172666499" }, "pipfile-spec": 6, "requires": { @@ -140,6 +140,14 @@ "index": "pypi", "version": "==0.18.1" }, + "python-dotenv": { + "hashes": [ + "sha256:b7e3b04a59693c42c36f9ab1cc2acc46fa5df8c78e178fc33a8d4cd05c8d498f", + "sha256:d92a187be61fe482e4fd675b6d52200e7be63a12b724abbf931a40ce4fa92938" + ], + "index": "pypi", + "version": "==0.20.0" + }, "pytz": { "hashes": [ "sha256:1e760e2fe6a8163bc0b3d9a19c4f84342afa0a2affebfaa84b01b978a02ecaa7", @@ -220,6 +228,14 @@ "markers": "python_version >= '3.6'", "version": "==8.0.4" }, + "colorama": { + "hashes": [ + "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b", + "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2" + ], + "markers": "platform_system == 'Windows'", + "version": "==0.4.4" + }, "coverage": { "hashes": [ "sha256:03e2a7826086b91ef345ff18742ee9fc47a6839ccd517061ef8fa1976e652ce9", @@ -383,6 +399,14 @@ "index": "pypi", "version": "==0.4.2" }, + "tzdata": { + "hashes": [ + "sha256:238e70234214138ed7b4e8a0fab0e5e13872edab3be586ab8198c407620e2ab9", + "sha256:8b536a8ec63dc0751342b3984193a3118f8fca2afe25752bb9b7fffd398552d3" + ], + "index": "pypi", + "version": "==2022.1" + }, "urllib3": { "hashes": [ "sha256:44ece4d53fb1706f667c9bd1c648f5469a2ec925fcf3a776667042d645472c14", diff --git a/tests/test_misc.py b/tests/test_misc.py index 56d65a3..e9247e9 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -1,42 +1,12 @@ -import os - from django.conf import settings from django.test import TestCase, RequestFactory from django.urls import reverse -from CodeReview.load_env import load_to_env, get_env from Main.views import error_500_handler from Users.models import User from tests.testing_base import BaseCase -class EnvLoadTest(TestCase): - - def setUp(self) -> None: - with open('env_test.ps1', 'w+') as file: - file.writelines([ - '#This is a comment, it shouldn\'t load\n', - '$Env:TEST_VAR=\"Hello, World!\"\n', - '$Env:EQUAL=\"hi=hi\"\n' - ]) - - def test_get(self) -> None: - results = get_env('env_test.ps1') - self.assertDictEqual(results, {'TEST_VAR': "Hello, World!", 'EQUAL': "hi=hi"}) - - def test_load(self) -> None: - load_to_env('env_test.ps1') - self.assertEqual(os.getenv('TEST_VAR'), "Hello, World!") - - def test_no_file(self) -> None: - results = get_env("not_a_file.ps1") - self.assertIsNone(results) - - def tearDown(self) -> None: - if os.path.exists('env_test.ps1'): - os.remove('env_test.ps1') - - class UserSetupTest(BaseCase): @staticmethod