Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use python decouple for environment variables #715

Merged
merged 6 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions .env-normal-template

This file was deleted.

File renamed without changes.
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ To get started with Project Moore, follow these instructions to set up a

1. Install Python 3, at least version 3.6 or up.
2. [Install postgresql](INSTALLING_POSTGRES.md)
2. Install the following python packages:
- python3-venv
- python3-dev
- build-essential
- libpq-dev
3. Clone the repository.
3. Copy the file `.env-normal-template` and name the copy `.env-normal`
3. Fill in the necessary variables in `.env`. `MELOS_URL` and `MELOS_ADMIN` are required. You might have to fill in some database credidentils. Check `src/moore/settings/dev.py` for which default values are used if you don't specify and credidentials.
4. Run `source ./source_me.sh` to create a virtual environment.
4. Run `pip install --upgrade pip` to make sure that pip is running the latest version
5. Run `pip install -r dev-requirements.txt`
6. Use `cd src` to enter the website directory.
7. Run `./manage.py migrate` to initialize the database.
8. Run `./manage.py createsuperuser` to create an admin user. (if the ssn is not passed, the most likely fault lies with the db-credentials)
3. Install the following python packages:
- python3-venv
- python3-dev
- build-essential
- libpq-dev
4. Clone the repository.
5. Copy the file `.env-template` and name the copy `.env`
6. Fill in the necessary variables in `.env`. `MELOS_URL` and `MELOS_ADMIN` are required. You might have to fill in some database credidentils. Check `src/moore/settings/base.py` for which default values are used if you don't specify and credidentials.
7. Run `source ./source_me.sh` to create a virtual environment.
8. Run `pip install --upgrade pip` to make sure that pip is running the latest version
9. Run `pip install -r dev-requirements.txt`
10. Use `cd src` to enter the website directory.
11. Run `./manage.py migrate` to initialize the database.
12. Run `./manage.py createsuperuser` to create an admin user. (if the ssn is not passed, the most likely fault lies with the db-credentials)

During development, you can run a test web server using `./manage.py runserver`.

Expand Down Expand Up @@ -80,32 +80,32 @@ These tests are run automatically using Github Actions.
If, however, you want to run these tests locally you can run the following
commands in the project root directory:

- `./src/manage.py test src` - to test with our Django test suites
- `flake8 src` - to run the flake8 style enforcer
- `./src/manage.py test src` - to test with our Django test suites
- `flake8 src` - to run the flake8 style enforcer

## Translating

Project Moore intends to be multilingual. The web application is available in
both Swedish and English. Whenever any translatable text is added or changed it
should be translated using translation files.

*Within Project Moore we use American English.*
_Within Project Moore we use American English._

To create translations for an app:

1. `cd src/<appname>`
1. `../manage.py makemessages -l sv`
2. This will create or update the files under `src/<appname>/locale/`.
3. Use poedit (or your favourite tool -- please do not use a plain text editor
since those cannot handle all the subtleties) to fix the translations.
4. `../manage.py compilemessages`
1. This will create or update the files under `src/<appname>/locale/`.
1. Use poedit (or your favourite tool -- please do not use a plain text editor
since those cannot handle all the subtleties) to fix the translations.
1. `../manage.py compilemessages`

## Notes about the materialize framework

Project moore uses materialize as a css framework to get pre-built components.
The following components have been disabled in the `materialize.scss` file in the materialize app folder:

- `navbar`
- `navbar`

The reason for this is that they are not needed and are interfering with the code that we write. Keep this in mind
when updating or reinstalling materialize.
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ djangorestframework==3.13.1
wagtail-django-recaptcha==1.0

django-jsonschema-form==1.0.3

# Improved environment variable handling
python-decouple==3.7
4 changes: 0 additions & 4 deletions source_me.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ else
python3 -m venv venv
source venv/bin/activate
fi

if [ -f .env-normal ]; then
source .env-normal
fi
38 changes: 34 additions & 4 deletions src/moore/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from django.conf.global_settings import LOGIN_URL
from django.utils.translation import gettext_lazy as _

from decouple import config, UndefinedValueError
import sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
Expand Down Expand Up @@ -90,6 +90,17 @@
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': config('DJANGO_DB_NAME', default='moore'),
'USER': config('DJANGO_DB_USER', default='moore'),
'PASSWORD': config('DJANGO_DB_PASS', default='moore'),
'HOST': config('DJANGO_DB_HOST', default='127.0.0.1'),
'PORT': config('DJANGO_DB_PORT', default='5432'),
}
}

ROOT_URLCONF = 'moore.urls'

TEMPLATES = [
Expand Down Expand Up @@ -230,9 +241,28 @@

IS_RUNNING_TEST = 'test' in sys.argv

INSTAGRAM_APP_ID = os.environ.get('INSTAGRAM_APP_ID')
INSTAGRAM_APP_SECRET = os.environ.get('INSTAGRAM_APP_SECRET')
INSTAGRAM_REDIRECT_URL = os.environ.get('INSTAGRAM_REDIRECT_URL')
INSTAGRAM_APP_ID = config('INSTAGRAM_APP_ID', default='')
INSTAGRAM_APP_SECRET = config('INSTAGRAM_APP_SECRET', default='')
INSTAGRAM_REDIRECT_URL = config('INSTAGRAM_REDIRECT_URL', default='')

try:
MELOS_URL = config('MELOS_URL')
MELOS_ADMIN = config('MELOS_ADMIN')
except UndefinedValueError:
# This allows the tests to be runned without having to have MELOS_URL and
# MELOS_ADMIN since they don't use the MELOS API. But this also raises
# the error if for example a developer tries to start the server but has
# not filled in the variables in their .env. I.e. The variables are still
# required, except for when the tests are runned.
if not IS_RUNNING_TEST:
raise UndefinedValueError(
"You must add MELOS_URL and MELOS_ADMIN to you .env file"
)

MELOS_ORG_ID = config('MELOS_ORG_ID', default='')

# Google API
GOOGLE_API_KEY = config('GOOGLE_API_KEY', default='')

REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
Expand Down
23 changes: 0 additions & 23 deletions src/moore/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,6 @@
'PORT': 5432,
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DJANGO_DB_NAME', 'moore'),
'USER': os.environ.get('DJANGO_DB_USER', 'moore'),
'PASSWORD': os.environ.get('DJANGO_DB_PASS', 'moore'),
'HOST': os.environ.get('DJANGO_DB_HOST', '127.0.0.1'),
'PORT': os.environ.get('DJANGO_DB_PORT', '5432'),
}
}

# Base URL to use when referring to full URLs within the Wagtail admin
# backend - e.g. in notification emails. Don't include '/admin' or a
Expand All @@ -74,15 +63,3 @@
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

DEFAULT_FROM_EMAIL = 'info@localhost'

MELOS_URL = os.environ.get('MELOS_URL')
MELOS_ORG_ID = os.environ.get('MELOS_ORG_ID')
MELOS_ADMIN = os.environ.get('MELOS_ADMIN')

# Google API
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')

try:
from .local import *
except ImportError:
pass
38 changes: 5 additions & 33 deletions src/moore/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,20 @@
DEBUG = False

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get(
'DJANGO_SECRET',
'za7^0@54n&p-dg4)_l12q_3^o5awz_uym0osqaz2!myki_8kw0'
)
SECRET_KEY = config('DJANGO_SECRET')

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DJANGO_DB_NAME', 'moore'),
'USER': os.environ.get('DJANGO_DB_USER', 'moore'),
'PASSWORD': os.environ.get('DJANGO_DB_PASS'),
'HOST': os.environ.get('DJANGO_DB_HOST', '127.0.0.1'),
'PORT': os.environ.get('DJANGO_DB_PORT', '5432'),
}
}

sentry_sdk.init(
dsn=os.environ.get("SENTRY_DSN"),
dsn=config("SENTRY_DSN"),
integrations=[DjangoIntegration()],

# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True
)

# CONN_MAX_AGE = 0

# Base URL to use when referring to full URLs within the Wagtail admin
# backend - e.g. in notification emails. Don't include '/admin' or a
# trailing slash
Expand Down Expand Up @@ -96,21 +80,9 @@

SESSION_COOKIE_SECURE = True

MELOS_URL = os.environ.get('MELOS_URL')
MELOS_ORG_ID = os.environ.get('MELOS_ORG_ID')
MELOS_ADMIN = os.environ.get('MELOS_ADMIN')

# Google API
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')

RECAPTCHA_PUBLIC_KEY = os.environ.get("RECAPTCHA_PUBLIC_KEY", "")
RECAPTCHA_PRIVATE_KEY = os.environ.get("RECAPTCHA_PRIVATE_KEY", "")

try:
from .local import *
except ImportError:
pass
RECAPTCHA_PUBLIC_KEY = config("RECAPTCHA_PUBLIC_KEY")
RECAPTCHA_PRIVATE_KEY = config("RECAPTCHA_PRIVATE_KEY")

KRONOS_PREFIX = (
'export SENTRY_DSN="{0}" &&'.format(os.environ.get("SENTRY_DSN"))
'export SENTRY_DSN="{0}" &&'.format(config("SENTRY_DSN"))
)
59 changes: 0 additions & 59 deletions src/moore/settings/staging.py

This file was deleted.