Skip to content

Commit

Permalink
Merge pull request #9 from bensi94/update-dev-dependencies
Browse files Browse the repository at this point in the history
⬆️ Update dev dependencies and exstending example project.
  • Loading branch information
bensi94 authored Nov 1, 2023
2 parents 355f531 + 5b977f7 commit 4389e4a
Show file tree
Hide file tree
Showing 18 changed files with 678 additions and 629 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12-dev"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
poetry-version: ["1.2.2"]

steps:
Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ fail_fast: false

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- repo: https://github.com/psf/black
rev: 22.12.0
rev: 23.10.1
hooks:
- id: black
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.237
rev: v0.1.3
hooks:
- id: ruff
args:
- --fix
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
rev: v1.6.1
hooks:
- id: mypy
args: [ --strict ]
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ if DEBUG:
INTERNAL_IPS = ["127.0.0.1"]
```

⚠️ If using [Docker](https://www.docker.com/) the following will set your INTERNAL_IPS correctly in Debug mode:
```python
if DEBUG:
import socket # only if you haven't already imported this
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[: ip.rfind(".")] + ".1" for ip in ips] + ["127.0.0.1", "10.0.2.2"]
```

🚨 ️  It's recommended to only configure these settings in DEBUG mode.
Even though Django Requests Tracker will only track requests in DEBUG mode
it's still a good practice to only have it installed in DEBUG mode.
Expand Down
8 changes: 8 additions & 0 deletions example_project/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
venv
run_async.py
__pycache__
.gitignore
.idea
.env
.ruff_cache
db.sqlite3
13 changes: 13 additions & 0 deletions example_project/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.11

EXPOSE 8000

WORKDIR /app

COPY ./requirements.txt /app/requirements.txt

RUN pip install --no-cache-dir --upgrade -r requirements.txt

COPY . /app

CMD ["uvicorn", "example_project.asgi:application", "--host", "0.0.0.0", "--port", "8000"]
4 changes: 4 additions & 0 deletions example_project/core/management/commands/create_demo_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Command(BaseCommand):
help = "Create demo data for the example project app"

def handle(self, *args, **options):
if User.objects.filter(username="demo_user").exists():
print("Demo data already created. Skipping...")
return

user = User.objects.create_user(
username="demo_user",
email="test@example.com",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def add_arguments(self, parser):
parser.add_argument("--url", type=str, default="http://localhost:8000")

async def async_handle(self, base_url: str) -> None:

responses: dict[str, dict] = {}

async def send_get_request(client: httpx.AsyncClient, url: str) -> None:
Expand Down
41 changes: 41 additions & 0 deletions example_project/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: '3'

services:
db:
image: postgres:15
restart: always
expose:
- '5432'
ports:
- '7432:5432'
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
healthcheck:
test: /usr/bin/pg_isready
interval: 5s
timeout: 10s
retries: 120
migrations_and_demo_data:
build:
context: .
dockerfile: Dockerfile
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
DATABASE_TYPE: postgres
entrypoint: bash -c "python manage.py migrate && python manage.py create_demo_data"
depends_on:
db:
condition: service_healthy

example_project:
build:
context: .
dockerfile: Dockerfile
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
DATABASE_TYPE: postgres
ports:
- '8000:8000'
depends_on:
- migrations_and_demo_data
- db
36 changes: 29 additions & 7 deletions example_project/example_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""

import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand Down Expand Up @@ -77,12 +77,24 @@
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
if os.getenv("DATABASE_TYPE", None) == "postgres":
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.getenv("POSTGRES_DB", "postgres"),
"USER": os.getenv("POSTGRES_USER", "postgres"),
"PASSWORD": os.getenv("POSTGRES_PASSWORD"),
"HOST": os.getenv("POSTGRES_HOST", "db"),
"PORT": os.getenv("POSTGRES_PORT", "5432"),
}
}
else:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
}


# Password validation
Expand Down Expand Up @@ -132,4 +144,14 @@
if DEBUG:
INSTALLED_APPS += ["requests_tracker"]
MIDDLEWARE += ["requests_tracker.middleware.requests_tracker_middleware"]
INTERNAL_IPS = ["127.0.0.1"]

import socket

hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[: ip.rfind(".")] + ".1" for ip in ips] + [
"127.0.0.1",
"10.0.2.2",
]

# If using without docker, this is enough:
# INTERNAL_IPS = ["127.0.0.1"]
1 change: 0 additions & 1 deletion example_project/ninja_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand Down
2 changes: 1 addition & 1 deletion example_project/ninja_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def n_plus_1_query(request: HttpRequest) -> None:

@error_examples_router.get("/divide-by-zero", response=None)
def divide_by_zero(request: HttpRequest) -> None:
10 / 0
10 / 0 # noqa: B018


@error_examples_router.get("/not-authenticated", auth=lambda _: False)
Expand Down
3 changes: 3 additions & 0 deletions example_project/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ pyyaml
requests
uvicorn
httpx

# Only needed if running with postgres database, for example in docker setup.
psycopg2
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand Down
Loading

0 comments on commit 4389e4a

Please sign in to comment.