Skip to content

Commit 57d5556

Browse files
added basic logging and updated environment variables
- added github actions testing - updated enviroment variables in settings
1 parent 835f688 commit 57d5556

File tree

8 files changed

+117
-2
lines changed

8 files changed

+117
-2
lines changed

.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SECRET_KEY='django-insecure-w!h85bp^$$e8gm%c23r!0%9i7yzd=6w$$s&ic+6!%306&kj8@k*5'
2+
DEBUG=True
3+
DB_HOST=db
4+
DB_PORT=5432
5+
DB_NAME=term_db
6+
DB_USER=sadilar
7+
DB_PASSWORD=sadilar
8+
LOGGING_FILE=logs/debug.log
9+
LOGGING_HANDLERS_LEVEL=INFO
10+
LOGGING_LOGGERS_LEVEL=INFO
11+
LOGGING_LOGGERS_DJANGO_LEVEL=INFO

.env.testing

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SECRET_KEY='django-insecure-w!h85bp^$$e8gm%c23r!0%9i7yzd=6w$$s&ic+6!%306&kj8@k*5'
2+
DEBUG=True
3+
DB_HOST=db
4+
DB_PORT=5432
5+
DB_NAME=term_db
6+
DB_USER=sadilar
7+
DB_PASSWORD=sadilar
8+
LOGGING_FILE=logs/debug.log
9+
LOGGING_HANDLERS_LEVEL=INFO
10+
LOGGING_LOGGERS_LEVEL=INFO
11+
LOGGING_LOGGERS_DJANGO_LEVEL=INFO

.github/workflows/testing.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Testing Django
2+
on: [ pull_request, push ] # activates the workflow when there is a push or pull request in the repo
3+
jobs:
4+
test_project:
5+
runs-on: ubuntu-latest # operating system your code will run on
6+
steps:
7+
- uses: actions/checkout@v2
8+
- uses: actions/setup-python@v2
9+
- name: Install Dependencies
10+
run: |
11+
python -m pip install --upgrade pip
12+
pip install -r requirements-test.txt
13+
- name: Run linting tools
14+
run: |
15+
cd app/
16+
ruff format .
17+
- name: Run Tests
18+
run: |
19+
cp .env.testing app/.env
20+
cd app/
21+
mkdir static_files
22+
python manage.py test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ venv.bak/
3333
app/static_files/
3434
/app/documents/
3535
app/media/
36+
/app/logging/

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@ About the project:
2424
3. Run `make run` to run the docker container
2525
4. Run `make stop` to stop the docker container
2626

27+
## Production
2728

2829
### Plugins installed
30+
2931
#### Django Simple History
3032

3133
https://django-simple-history.readthedocs.io/en/latest/
34+
35+
#### Basic setup for production
36+
37+
### environment variables
38+
39+
please use .env.example as example

app/app/settings.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
"""
1212

1313
import os
14+
import sys
1415
from pathlib import Path
1516

17+
import environ
18+
1619
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1720
BASE_DIR = Path(__file__).resolve().parent.parent
1821

22+
# Take environment variables from .env file
23+
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))
24+
1925
# Quick-start development settings - unsuitable for production
2026
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
2127

@@ -98,6 +104,9 @@
98104
}
99105
}
100106

107+
if "test" in sys.argv or "test_coverage" in sys.argv: # Covers regular testing and django-coverage
108+
DATABASES["default"]["ENGINE"] = "django.db.backends.sqlite3"
109+
101110
# Password validation
102111
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
103112

@@ -154,3 +163,52 @@
154163
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
155164

156165
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
166+
167+
168+
if DEBUG:
169+
LOGGING = {
170+
"version": 1,
171+
"disable_existing_loggers": False,
172+
"handlers": {
173+
"console": {
174+
"class": "logging.StreamHandler",
175+
},
176+
},
177+
"root": {
178+
"handlers": ["console"],
179+
"level": "DEBUG",
180+
},
181+
}
182+
else:
183+
LOGGING = {
184+
"version": 1,
185+
"disable_existing_loggers": False,
186+
"handlers": {
187+
"console": {
188+
"class": "logging.StreamHandler",
189+
},
190+
"file": {
191+
"level": os.environ.get("LOGGING_HANDLERS_LEVEL", "WARNING"),
192+
"class": "logging.FileHandler",
193+
"filename": os.environ.get("LOGGING_FILE", "logging/debug.log"),
194+
"formatter": "verbose",
195+
},
196+
},
197+
"root": {
198+
"handlers": ["console", "file"],
199+
"level": os.environ.get("LOGGING_LOGGERS_LEVEL", "WARNING"),
200+
},
201+
"loggers": {
202+
"django": {
203+
"handlers": ["file"],
204+
"level": os.environ.get("LOGGING_LOGGERS_DJANGO_LEVEL", "WARNING"),
205+
"propagate": True,
206+
},
207+
},
208+
"formatters": {
209+
"verbose": {
210+
"format": "{asctime} {levelname} - {name} {module}.py (line: {lineno:d}). - {message}",
211+
"style": "{",
212+
},
213+
},
214+
}

requirements-test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-r requirements.txt
2+
django-extensions
3+
ruff

requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
django==5.0.2
2-
psycopg2-binary
2+
django-environ
3+
django-simple-history
34
gunicorn
5+
psycopg2-binary
46
whitenoise
5-
django-simple-history

0 commit comments

Comments
 (0)