From 680fdcd617da0091588aaff9dd987b65e2801c5e Mon Sep 17 00:00:00 2001 From: Kennedy Kori Date: Wed, 5 Apr 2023 16:43:07 +0300 Subject: [PATCH] chore(conf): update storages configuration Update [Django Storages](https://docs.djangoproject.com/en/4.2/ref/settings/#storages) configuration to use the new setting added in [Django 4.2](https://docs.djangoproject.com/en/4.2/releases/4.2/#custom-file-storages). Included also is an update of pyscopg to [version 3](https://www.psycopg.org/psycopg3/docs/basic/install.html) as this is now supported in [Django 4.2](https://docs.djangoproject.com/en/4.2/releases/4.2/#psycopg-3-support). Included also are other minor improvements and fixes. --- config/settings/base.py | 9 +++++++-- config/settings/github_ci.py | 22 ++-------------------- config/settings/local.py | 4 ++-- config/settings/production.py | 14 +++++++++----- config/settings/test.py | 22 ++++++++++++++-------- requirements/base.txt | 2 +- 6 files changed, 35 insertions(+), 38 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index 01a94a1..1aa429e 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -204,7 +204,7 @@ LANGUAGE_CODE = "en-us" TIME_ZONE = "Africa/Nairobi" USE_I18N = True -# USE_L10N = True +USE_L10N = True USE_TZ = True @@ -223,7 +223,12 @@ "django.contrib.staticfiles.finders.AppDirectoriesFinder", "compressor.finders.CompressorFinder", ] -STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage" +STORAGES = { + "default": {"BACKEND": "django.core.files.storage.FileSystemStorage"}, + "staticfiles": { + "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", + }, +} WHITENOISE_MANIFEST_STRICT = False diff --git a/config/settings/github_ci.py b/config/settings/github_ci.py index 9739177..d52ae2d 100644 --- a/config/settings/github_ci.py +++ b/config/settings/github_ci.py @@ -17,8 +17,8 @@ "NAME": env.str("POSTGRES_DB"), "USER": env.str("POSTGRES_USER"), "PASSWORD": env.str("POSTGRES_PASSWORD"), - "HOST": env.str("POSTGRES_HOST"), - "PORT": env.str("POSTGRES_PORT", None), + "HOST": env.str("POSTGRES_HOST", default="localhost"), + "PORT": env.int("POSTGRES_PORT", default=5432), "ENGINE": "django.contrib.gis.db.backends.postgis", "ATOMIC_REQUESTS": True, "CONN_MAX_AGE": env.int("CONN_MAX_AGE", default=60) @@ -26,15 +26,6 @@ } -############################################################################### -# STORAGES -############################################################################### - -INSTALLED_APPS += ["storages"] -GS_BUCKET_NAME = env.str("DJANGO_GCP_STORAGE_BUCKET_NAME") -GS_DEFAULT_ACL = "project-private" - - ############################################################################### # DJANGO COMPRESSOR ############################################################################### @@ -52,12 +43,3 @@ ], "js": ["compressor.filters.jsmin.JSMinFilter"], } - - -############################################################################### -# STATIC ASSETS AND MEDIA FILES -############################################################################### - -DEFAULT_FILE_STORAGE = "utils.storages.MediaRootGoogleCloudStorage" -MEDIA_URL = "https://storage.googleapis.com/%s/media/" % GS_BUCKET_NAME -STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" diff --git a/config/settings/local.py b/config/settings/local.py index 7779682..3a07c76 100644 --- a/config/settings/local.py +++ b/config/settings/local.py @@ -16,8 +16,8 @@ "NAME": env.str("POSTGRES_DB"), "USER": env.str("POSTGRES_USER"), "PASSWORD": env.str("POSTGRES_PASSWORD"), - "HOST": env.str("POSTGRES_HOST"), - "PORT": env.str("POSTGRES_PORT", None), + "HOST": env.str("POSTGRES_HOST", defualt="localhost"), + "PORT": env.int("POSTGRES_PORT", default=5432), "ENGINE": "django.contrib.gis.db.backends.postgis", "ATOMIC_REQUESTS": True, }, diff --git a/config/settings/production.py b/config/settings/production.py index 54f7143..6395e3f 100644 --- a/config/settings/production.py +++ b/config/settings/production.py @@ -96,7 +96,7 @@ "USER": env.str("POSTGRES_USER"), "PASSWORD": env.str("POSTGRES_PASSWORD"), "HOST": env.str("POSTGRES_HOST"), - "PORT": env.str("POSTGRES_PORT", None), + "PORT": env.str("POSTGRES_PORT", default=None), "ENGINE": "django.contrib.gis.db.backends.postgis", "ATOMIC_REQUESTS": False, "CONN_HEALTH_CHECKS": True, @@ -155,10 +155,12 @@ # STATIC ASSETS AND MEDIA FILES ############################################################################### -DEFAULT_FILE_STORAGE = "utils.storages.MediaRootGoogleCloudStorage" MEDIA_URL = "https://storage.googleapis.com/%s/media/" % GS_BUCKET_NAME STATIC_URL = "https://storage.googleapis.com/%s/static/" % GS_BUCKET_NAME -STATICFILES_STORAGE = "utils.storages.StaticRootGoogleCloudStorage" +STORAGES = { + "default": {"BACKEND": "utils.storages.MediaRootGoogleCloudStorage"}, + "staticfiles": {"BACKEND": "utils.storages.StaticRootGoogleCloudStorage"}, +} ############################################################################### @@ -177,9 +179,11 @@ # https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_OFFLINE COMPRESS_OFFLINE = True # https://django-compressor.readthedocs.io/en/stable/settings.html#django.conf.settings.COMPRESS_OFFLINE_MANIFEST_STORAGE -COMPRESS_OFFLINE_MANIFEST_STORAGE = STATICFILES_STORAGE +COMPRESS_OFFLINE_MANIFEST_STORAGE = ( + "utils.storages.StaticRootGoogleCloudStorage" +) # https://django-compressor.readthedocs.io/en/stable/settings.html#django.conf.settings.COMPRESS_STORAGE -COMPRESS_STORAGE = STATICFILES_STORAGE +COMPRESS_STORAGE = "utils.storages.StaticRootGoogleCloudStorage" # https://django-compressor.readthedocs.io/en/latest/settings/#django.conf.settings.COMPRESS_URL COMPRESS_URL = STATIC_URL diff --git a/config/settings/test.py b/config/settings/test.py index 17277ca..9a7539b 100644 --- a/config/settings/test.py +++ b/config/settings/test.py @@ -7,17 +7,23 @@ DATABASES = { "default": { - "NAME": env.str("TEST_POSTGRES_DB"), - "USER": env.str("TEST_POSTGRES_USER"), - "PASSWORD": env.str("TEST_POSTGRES_PASSWORD"), - "HOST": env.str("TEST_POSTGRES_HOST"), - "PORT": env.str("TEST_POSTGRES_PORT", None), + "NAME": env.str( + "TEST_POSTGRES_DB", default=f"test_{env.str('POSTGRES_DB')}" + ), + "USER": env.str( + "TEST_POSTGRES_USER", default=env.str("POSTGRES_USER") + ), + "PASSWORD": env.str( + "TEST_POSTGRES_PASSWORD", defualt=env.str("POSTGRES_PASSWORD") + ), + "HOST": env.str("TEST_POSTGRES_HOST", default="localhost"), + "PORT": env.int("TEST_POSTGRES_PORT", port=5432), "ENGINE": "django.contrib.gis.db.backends.postgis", "ATOMIC_REQUESTS": True, "TEST": { - "NAME": env.str("TEST_POSTGRES_DB"), - "USER": env.str("TEST_POSTGRES_USER"), - "PASSWORD": env.str("TEST_POSTGRES_PASSWORD"), + "NAME": env.str( + "TEST_POSTGRES_DB", default=f"test_{env.str('POSTGRES_DB')}" + ) }, }, } diff --git a/requirements/base.txt b/requirements/base.txt index 892c90a..4e48601 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -16,7 +16,7 @@ environs~=9.5.0 google-auth~=2.17.1 google-cloud-pubsub~=2.15.2 openpyxl~=3.1.2 -psycopg2~=2.9.5 +psycopg-binary~=3.1.8 pyexcel-io~=0.6.6 pyexcel-xlsx~=0.6.0 pyexcel~=0.7.0