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

Add img postgres, configuração do DB com vars de ambiente e dj e decoupe #17

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion .github/workflows/django_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ on:
jobs:
build:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:16.3
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports: [ '5432:5432' ]
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -28,6 +34,9 @@ jobs:
- name: Lint
run: poetry run task lint

- name: Copy env-sample for .env
run: cp config/.env-sample .env

- name: Rodar testes do Django com cobertura
run: poetry run pytest devpro --cov=devpro --cov-fail-under=82

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.vscode
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Projeto com código fonte do [curso Django para Profissionais](https://l.dev.pro

## Instalação

Para instalar o projeto localmente, instale o poetry e use o comando, com dependências desenvolvimento:
Para instalar o projeto localmente, instale o poetry e use o comando, com dependências desenvolvimento:

```bash
Expand All @@ -17,16 +18,18 @@ Ative o ambiente virtual:
poetry shell
```

Definir variáveis de ambiente em um .env de acordo com o [.env-sample](./config/env-sample)

Rode o servidor local:

```bash
python manage.py runserver
task run
```

Para rodar testes automáticos com pytest:

```bash
pytest devpro
task test
```

Para rodar testes automáticos com pytest e gerar relatório de cobertura:
Expand All @@ -40,4 +43,5 @@ Utilize também o formato de código configurado com ruff. Você pode consultar

```
task lint
```
```

6 changes: 6 additions & 0 deletions config/.env-sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BACKEND_SECRET_KEY=escolha-um-segredo-como-valor-para-o-django
# SECURITY WARNING: don't run with debug turned on in production!
BACKEND_DEBUG=False
# Configure os dominios ou ips que sua aplicação vai rodar, separadas por vírgula
BACKEND_ALLOWED_HOSTS=localhost,127.0.0.1
DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres
18 changes: 11 additions & 7 deletions devpro/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

from pathlib import Path

import dj_database_url
from decouple import Csv, config

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand All @@ -20,12 +23,12 @@
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-7hqdt_094rh^$!86*4bczk_u&ha5*+*oc9hvd^4*0eco!#)wou'
SECRET_KEY = config('BACKEND_SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = config('BACKEND_DEBUG', default=False, cast=bool)

ALLOWED_HOSTS = []
ALLOWED_HOSTS = config('BACKEND_ALLOWED_HOSTS', default='', cast=Csv())


# Application definition
Expand Down Expand Up @@ -75,10 +78,11 @@
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
'default': dj_database_url.parse(
'postgres://postgres:postgres@localhost:5432/postgres',
conn_max_age=600,
conn_health_checks=True,
)
}


Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
database:
container_name: dbprodb
image: postgres:16.3
volumes:
- ./.pgdata:/var/lib/postgresql/data
environment:
- LC_ALL=C.UTF-8
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
ports:
- 5432:5432

119 changes: 89 additions & 30 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ package-mode = false
[tool.poetry.dependencies]
python = "^3.12"
django = "^5.1"
psycopg2 = "^2.9.9"
dj-database-url = "^2.2.0"
python-decouple = "^3.8"

[tool.poetry.group.dev]
optional = true
Expand All @@ -27,7 +30,7 @@ python_files = ["test*.py", "*.p_testy"]

[tool.ruff]
line-length=120
exclude = ['./devpro/base/migrations']
exclude = ['./devpro/base/migrations', '.pgdata']

[tool.ruff.lint]
select = ['I', 'F', 'E', 'W', 'PL', 'PT']
Expand Down