diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..c92bcb54 --- /dev/null +++ b/.env.example @@ -0,0 +1,11 @@ +SECRET_KEY='' +DEBUG=True +DB_HOST=db +DB_PORT=5432 +DB_NAME=term_db +DB_USER=sadilar +DB_PASSWORD=sadilar +LOGGING_FILE=debug.log +LOGGING_HANDLERS_LEVEL=INFO +LOGGING_LOGGERS_LEVEL=INFO +LOGGING_LOGGERS_DJANGO_LEVEL=INFO diff --git a/.env.testing b/.env.testing new file mode 100644 index 00000000..aa664bda --- /dev/null +++ b/.env.testing @@ -0,0 +1,11 @@ +SECRET_KEY='django-insecure-w!h85bp^$$e8gm%c23r!0%9i7yzd=6w$$s&ic+6!%306&kj8@k*5' +DEBUG=True +DB_HOST=db +DB_PORT=5432 +DB_NAME=term_db +DB_USER=sadilar +DB_PASSWORD=sadilar +LOGGING_FILE=debug.log +LOGGING_HANDLERS_LEVEL=INFO +LOGGING_LOGGERS_LEVEL=INFO +LOGGING_LOGGERS_DJANGO_LEVEL=INFO diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 00000000..8c1b06b8 --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,30 @@ +name: Testing Django +on: [ pull_request, push ] # activates the workflow when there is a push or pull request in the repo +jobs: + test_project: + runs-on: ubuntu-latest # operating system your code will run on + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + - name: Install Dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements-test.txt + - name: Run linting tools + run: | + cd app/ + ruff format . + - name: Create logging folder + run: | + sudo mkdir -p /logging + sudo chown runner:runner /logging + - name: Run Tests + run: | + cp .env.testing app/.env + cd app/ + mkdir -p static_files + python manage.py test + - name: Manager Check + run: | + cd app/ + python manage.py check diff --git a/.gitignore b/.gitignore index ec66bba8..627b4187 100644 --- a/.gitignore +++ b/.gitignore @@ -16,8 +16,7 @@ db.sqlite3 db.sqlite3-journal media -# macOS template -# General +# General Files .DS_Store .AppleDouble .LSOverride @@ -30,6 +29,10 @@ venv/ ENV/ env.bak/ venv.bak/ + +#folders app/static_files/ /app/documents/ app/media/ +/app/logging/ +/logging/ diff --git a/Makefile b/Makefile index 06e52de3..c9a6f503 100644 --- a/Makefile +++ b/Makefile @@ -112,3 +112,11 @@ dev-quick-install: @make load-fixtures echo "Creating superuser" @make create-super-user + +shell: + clear + docker exec -it sadilar-terminology-web bash + +check: + clear + @docker-compose run --rm web python manage.py check diff --git a/README.md b/README.md index d2f4ae1f..145b4bb5 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,24 @@ About the project: 3. Run `make run` to run the docker container 4. Run `make stop` to stop the docker container +## Production ### Plugins installed + #### Django Simple History https://django-simple-history.readthedocs.io/en/latest/ + +#### Basic setup for production + +### environment variables + +please use .env.example as example + + +## Production Information + +Docker Volumes for production: + +/media +/logging diff --git a/app/app/settings.py b/app/app/settings.py index f8680adf..9ac132e0 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -11,11 +11,17 @@ """ import os +import sys from pathlib import Path +import environ + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +# Take environment variables from .env file +environ.Env.read_env(os.path.join(BASE_DIR, ".env")) + # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/ @@ -98,6 +104,9 @@ } } +if "test" in sys.argv or "test_coverage" in sys.argv: # Covers regular testing and django-coverage + DATABASES["default"]["ENGINE"] = "django.db.backends.sqlite3" + # Password validation # https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators @@ -154,3 +163,61 @@ # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +LOGGING_FOLDER_DEFAULT = os.path.abspath(os.path.join("/logging/")) + +# Check if the application is under testing +if "test" in sys.argv: + DEBUG = False +else: + DEBUG = True + +if DEBUG: + LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "handlers": { + "console": { + "class": "logging.StreamHandler", + }, + }, + "root": { + "handlers": ["console"], + "level": "DEBUG", + }, + } +else: + LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "handlers": { + "console": { + "class": "logging.StreamHandler", + }, + "file": { + "level": os.environ.get("LOGGING_HANDLERS_LEVEL", "WARNING"), + "class": "logging.FileHandler", + "filename": os.path.join( + LOGGING_FOLDER_DEFAULT, os.environ.get("LOGGING_FILE", "debug.log") + ), + "formatter": "verbose", + }, + }, + "root": { + "handlers": ["console", "file"], + "level": os.environ.get("LOGGING_LOGGERS_LEVEL", "WARNING"), + }, + "loggers": { + "django": { + "handlers": ["file"], + "level": os.environ.get("LOGGING_LOGGERS_DJANGO_LEVEL", "WARNING"), + "propagate": True, + }, + }, + "formatters": { + "verbose": { + "format": "{asctime} {levelname} - {name} {module}.py (line: {lineno:d}). - {message}", + "style": "{", + }, + }, + } diff --git a/app/general/tests/test_logging.py b/app/general/tests/test_logging.py new file mode 100644 index 00000000..00926204 --- /dev/null +++ b/app/general/tests/test_logging.py @@ -0,0 +1,24 @@ +import logging +import os +import sys + +from django.test import TestCase + + +class LoggingTest(TestCase): + def setUp(self): + LOGGING_FOLDER_DEFAULT = os.path.abspath(os.path.join("/logging/")) + self.logger = logging.getLogger("django") + self.log_file = os.path.join(LOGGING_FOLDER_DEFAULT, "debug.log") + + def test_log_file_created(self): + """Test if the log file is created.""" + self.logger.error("This is a test error message.") + + self.assertTrue(os.path.exists(self.log_file)) + + def test_log_message(self): + """Test if the log message is written to the file.""" + with open(self.log_file, "r") as f: + content = f.read() + self.assertIn("This is a test error message.", content) diff --git a/docker-compose.yml b/docker-compose.yml index 6e7c2f09..b0d5b2c0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,6 +22,7 @@ services: command: python manage.py runserver 0.0.0.0:8000 volumes: - ./app:/app + - ./logging:/logging ports: - "8000:8000" depends_on: diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 00000000..e00d50bd --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1,3 @@ +-r requirements.txt +django-extensions +ruff diff --git a/requirements.txt b/requirements.txt index 6c93a0f9..c2b69d85 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ django==5.0.2 -psycopg2-binary +django-environ +django-simple-history gunicorn +psycopg2-binary whitenoise -django-simple-history