Skip to content

Commit

Permalink
refactor: settings from env to yaml
Browse files Browse the repository at this point in the history
Instead of reading settings from env file read them from an yaml file.
Use sqlite for testing.
Remove the restart always on docker compose, so the project want start
on laptop next reboot.
Remove python-decouple package as dependency.
  • Loading branch information
igobranco committed Dec 4, 2023
1 parent dfe05a9 commit 11f9f9c
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 125 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@ jobs:
- name: Setup Poetry
run: make install-poetry install-packages

- name: Get all containers up and running
run: cp sample-dev.env .env; make hr-docker

- name: Run Django tests
run: make test
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ pyrightconfig.json
# Ignore all local history of files
.history
.ionide
.env
.python-version
.env
.vscode/settings.json
db.sqlite3

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SRC_DIR = src
TEST_DIR = tests
POETRY_RUN = poetry run
DOCKER_COMPOSE = docker-compose
TEST_CMD = $(POETRY_RUN) python manage.py test
TEST_CMD = $(POETRY_RUN) python manage.py test --settings=nau_financial_manager.test
LINT_CMD = $(POETRY_RUN) black .
PRE_COMMIT = $(POETRY_RUN) pre-commit run --all-files
RUN_CMD = $(POETRY_RUN) python manage.py runserver
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ poetry env use 3.11.4
poetry shell
```

## Create .env file for enviromnent desired

```bash
cp sample-dev.env .env
```

## Run for DEV

Start the app's dependencies services:
Expand Down
9 changes: 9 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This is default configuration settings used to development purposes.
# We can override any default Django setting.
# The good default setting value should be the default value of the
#
# CONFIG.get("KEY", <default value>)
#

# Currently we are just enforcing the debug.
DEBUG: True
2 changes: 2 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ COPY poetry.lock pyproject.toml /app/
# Install packages via poetry:
RUN poetry install --verbose

ENV FINANCIAL_MANAGER_CFG=/app/config.yml

USER ${USER}:${USER}
2 changes: 0 additions & 2 deletions docker/docker-compose-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ services:

nau-financial-app:
container_name: nau-financial-app
restart: always
command: sh -c './wait-for-mysql.sh && python manage.py runserver 0.0.0.0:8000'
build:
context: ../
dockerfile: docker/Dockerfile
env_file: ../.env
# on docker mode change use the container directly
environment:
- DB_HOST=nau-database-mysql
Expand Down
15 changes: 6 additions & 9 deletions docker/docker-compose-dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ version: "3.5"
services:
database-mysql:
container_name: nau-database-mysql
restart: always
image: mysql:8.1
env_file: ../.env
environment:
- MYSQL_DATABASE=nau_db
- MYSQL_USER=nau_user
- MYSQL_USER_ROOT=root
- MYSQL_PASSWORD=nau_password
- MYSQL_ROOT_PASSWORD=nau_password
hostname: database
ports:
- "3306:3306"

redis:
hostname: nau-redis
restart: always
image: redis:latest
ports:
- "6379:6379"

celery:
hostname: nau-celery
restart: always
build:
context: ../
dockerfile: docker/Dockerfile
command: celery -A nau_financial_manager worker --loglevel=info
env_file: ../.env
volumes:
- ../:/app
depends_on:
Expand All @@ -33,12 +34,10 @@ services:

celery-beat:
hostname: nau-celery-beat
restart: always
build:
context: ../
dockerfile: docker/Dockerfile
command: celery -A nau_financial_manager beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler
env_file: ../.env
environment:
- DB_HOST=nau-database-mysql
volumes:
Expand All @@ -52,8 +51,6 @@ services:
context: ../
dockerfile: docker/Dockerfile
command: celery -A nau_financial_manager flower --port=5555
env_file: ../.env
restart: always
volumes:
- ../:/app
ports:
Expand Down
142 changes: 95 additions & 47 deletions nau_financial_manager/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,48 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""
import codecs
import copy
import os
from pathlib import Path

from decouple import Csv, config
import yaml
from django.core.exceptions import ImproperlyConfigured


def get_env_setting(env_variable, default=None):
"""Get the environment variable value or return its default value."""
try:
return os.environ[env_variable]
except KeyError:
if default:
return default
else:
error_msg = "Set the {} environment variable".format(env_variable)
raise ImproperlyConfigured(error_msg)


CONFIG_FILE = get_env_setting("FINANCIAL_MANAGER_CFG", "./config.yml")

# Load the configuration from an YAML file and expose the configurations on a `CONFIG` object.
with codecs.open(CONFIG_FILE, encoding="utf-8") as f:
CONFIG = yaml.safe_load(f)

# Add the key/values from config into the global namespace of this module.
__config_copy__ = copy.deepcopy(CONFIG)

vars().update(__config_copy__)


BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = config("SECRET_KEY")
DEBUG = CONFIG.get("DEBUG", False)

DEBUG = config("DEBUG", default=False, cast=bool)
SECRET_KEY = CONFIG.get("SECRET_KEY", "change-me")
if SECRET_KEY == "change-me" and not DEBUG:
raise ImproperlyConfigured("For security reasons you need to change the 'SECRET_KEY'.")

ALLOWED_HOSTS = config("ALLOWED_HOSTS", cast=Csv(), default="localhost")
ALLOWED_HOSTS = CONFIG.get("ALLOWED_HOSTS", ["127.0.0.1", "localhost"])

DJANGO_APPS = [
"django.contrib.admin",
Expand Down Expand Up @@ -83,16 +114,24 @@

WSGI_APPLICATION = "nau_financial_manager.wsgi.application"

DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": config("MYSQL_DATABASE"),
"USER": config("MYSQL_USER_ROOT"),
"PASSWORD": config("MYSQL_PASSWORD"),
"HOST": config("DB_HOST"),
"PORT": config("DB_PORT"),
}
}
# The normal Django `DATABASES`` setting
DATABASES = CONFIG.get(
"DATABASES",
{
"default": {
# The DB_HOST needs to be overridden from environment variable because we have 2 modes
# of running on the development mode, one from docker and another outside docker.
# Each modes connects to MySQL host differently.
"ENGINE": get_env_setting("ENGINE", "django.db.backends.mysql"),
"NAME": get_env_setting("MYSQL_DATABASE", "nau_db"),
"USER": get_env_setting("MYSQL_USER", "nau_user"),
"PASSWORD": get_env_setting("MYSQL_PASSWORD", "nau_password"),
# Default mode it the development mode without docker
"HOST": get_env_setting("DB_HOST", "127.0.0.1"),
"PORT": get_env_setting("DB_PORT", 3306),
}
},
)

REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
Expand All @@ -105,33 +144,38 @@
],
}

CELERY_APP = "nau_financial_manager"
CELERY_BROKER_URL = config("CELERY_BROKER_URL", "redis://nau-redis:6379/0")
CELERY_RESULT_BACKEND = config("CELERY_RESULT_BACKEND", "django-db")
CELERY_CACHE_BACKEND = "default"
CELERY_TASK_TRACK_STARTED = True
CELERY_ACCEPT_CONTENT = ["application/json"]
CELERY_TASK_SERIALIZER = "json"
CELERY_RESULT_SERIALIZER = "json"
CELERY_RESULTS_EXTENDED = True
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"
# Celery settings
CELERY_APP = CONFIG.get("CELERY_APP", "nau_financial_manager")
CELERY_BROKER_URL = CONFIG.get("CELERY_BROKER_URL", "redis://nau-redis:6379/0")
CELERY_RESULT_BACKEND = CONFIG.get("CELERY_RESULT_BACKEND", "django-db")
CELERY_CACHE_BACKEND = CONFIG.get("CELERY_CACHE_BACKEND", "default")
CELERY_TASK_TRACK_STARTED = CONFIG.get("CELERY_TASK_TRACK_STARTED", True)
CELERY_ACCEPT_CONTENT = CONFIG.get("CELERY_ACCEPT_CONTENT", ["application/json"])
CELERY_TASK_SERIALIZER = CONFIG.get("CELERY_TASK_SERIALIZER", "json")
CELERY_RESULT_SERIALIZER = CONFIG.get("CELERY_RESULT_SERIALIZER", "json")
CELERY_RESULTS_EXTENDED = CONFIG.get("CELERY_RESULTS_EXTENDED", True)
CELERY_BEAT_SCHEDULER = CONFIG.get("CELERY_BEAT_SCHEDULER", "django_celery_beat.schedulers:DatabaseScheduler")
# WORKAROUND TO CELERY USING MYSQL
DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH = 191


REDIS_URL = config("REDIS_URL", "redis://localhost:6379/")
REDIS_HOST = config("REDIS_HOST", "nau-redis")
REDIS_PORT = config("REDIS_PORT", 6379)
REDIS_DB = config("REDIS_DB", 0)

CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": config("REDIS_URL", "redis://localhost:6379/"),
"KEY_PREFIX": "naufm",
"TIMEOUT": 60 * 15, # in seconds: 60 * 15 (15 minutes)
}
}
# Django Cache settings
#
# By default it uses the Redis as Django Cache.
#
# The next configuration is used for development proposes.
# If you need to change this for a specific environment update the `CACHES` key
# on the `FINANCIAL_MANAGER_CFG` yaml file.
CACHES = CONFIG.get(
"CACHES",
{
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "redis://localhost:6379/0",
"KEY_PREFIX": "naufm",
"TIMEOUT": 60 * 15, # in seconds: 60 * 15 (15 minutes)
}
},
)

AUTH_PASSWORD_VALIDATORS = [
{
Expand Down Expand Up @@ -160,14 +204,18 @@

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# Transcation processor settings
TRANSACTION_PROCESSOR_URL = config("TRANSACTION_PROCESSOR_URL", "")
IVA_VACITM1_FIELD = config("IVA_VACITM1_FIELD", "")
GEOGRAPHIC_ACTIVITY_VACBPR_FIELD = config("GEOGRAPHIC_ACTIVITY_VACBPR_FIELD", "")
USER_PROCESSOR_AUTH = config("USER_PROCESSOR_AUTH", "")
USER_PROCESSOR_PASSWORD = config("USER_PROCESSOR_PASSWORD", "")
# Transaction processor settings
TRANSACTION_PROCESSOR_URL = CONFIG.get("TRANSACTION_PROCESSOR_URL", "")
IVA_VACITM1_FIELD = CONFIG.get("IVA_VACITM1_FIELD", "NOR")
GEOGRAPHIC_ACTIVITY_VACBPR_FIELD = CONFIG.get("GEOGRAPHIC_ACTIVITY_VACBPR_FIELD", "CON")
USER_PROCESSOR_AUTH = CONFIG.get("USER_PROCESSOR_AUTH", "")
USER_PROCESSOR_PASSWORD = CONFIG.get("USER_PROCESSOR_PASSWORD", "")

# Invoice host information
INVOICE_HOST_URL = config("INVOICE_HOST_URL", "")
INVOICE_HOST_AUTH = config("INVOICE_HOST_AUTH", "")
INVOICE_HOST_PASSWORD = config("INVOICE_HOST_PASSWORD", "")
INVOICE_HOST_URL = CONFIG.get("INVOICE_HOST_URL", "")
INVOICE_HOST_AUTH = CONFIG.get("INVOICE_HOST_AUTH", "")
INVOICE_HOST_PASSWORD = CONFIG.get("INVOICE_HOST_PASSWORD", "")

SWAGGER_PROJECT_NAME = CONFIG.get("SWAGGER_PROJECT_NAME", "Nau Financial Manager")
SWAGGER_PROJECT_VERSION = CONFIG.get("SWAGGER_PROJECT_VERSION", "1.0.0")
SWAGGER_DESCRIPTION = CONFIG.get("SWAGGER_DESCRIPTION", "API for Nau Financial Manager")
9 changes: 9 additions & 0 deletions nau_financial_manager/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .settings import *

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': "test.db",
'ATOMIC_REQUESTS': True,
},
}
8 changes: 4 additions & 4 deletions nau_financial_manager/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from decouple import config
from django.conf import settings
from django.contrib import admin
from django.urls import include, path
from drf_yasg import openapi
Expand All @@ -23,9 +23,9 @@

schema_view = get_schema_view(
openapi.Info(
title=config("SWAGGER_PROJECT_NAME", default="Your project name"),
default_version=config("SWAGGER_PROJECT_VERSION", default="1.0.0"),
description=config("SWAGGER_DESCRIPTION", default="Your project description"),
title=settings.SWAGGER_PROJECT_NAME,
default_version=settings.SWAGGER_PROJECT_VERSION,
description=settings.SWAGGER_DESCRIPTION,
),
public=True,
permission_classes=(permissions.AllowAny,),
Expand Down
Loading

0 comments on commit 11f9f9c

Please sign in to comment.