From dafa3802d3f721c6cb3ca3bc651c5828b5986781 Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:05:26 +0200 Subject: [PATCH 01/11] Test CI workflow --- .env.ci | 18 ++++++++++++++ .github/workflows/django-ci.yml | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 .env.ci create mode 100644 .github/workflows/django-ci.yml diff --git a/.env.ci b/.env.ci new file mode 100644 index 0000000..59fdf07 --- /dev/null +++ b/.env.ci @@ -0,0 +1,18 @@ +# Database +DATABASE_URI=postgresql://postgres:postgres@localhost:5432/web +DATABASE_URI_TEST=postgresql://postgres:postgres@localhost:5432/web_test + +# Django +SECRET_KEY=ci-secret-key +DEBUG=False +ALLOWED_HOSTS=localhost,127.0.0.1 +CORS_ALLOWED_ORIGINS=http://localhost:3000,http://127.0.0.1:3000 + +# Celery / Redis +CELERY_BROKER_URL=redis://localhost:6379/0 +CELERY_RESULT_BACKEND=redis://localhost:6379/1 + +# Postgres +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres +POSTGRES_DB=postgres \ No newline at end of file diff --git a/.github/workflows/django-ci.yml b/.github/workflows/django-ci.yml new file mode 100644 index 0000000..85fad2c --- /dev/null +++ b/.github/workflows/django-ci.yml @@ -0,0 +1,44 @@ +jobs: + test: + runs-on: ubuntu-latest + services: + postgres: + image: postgres:15 + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: web_test + ports: ["5432:5432"] + options: >- + --health-cmd="pg_isready -U postgres" + --health-interval=10s + --health-timeout=5s + --health-retries=5 + + redis: + image: redis:7 + ports: ["6379:6379"] + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Install dependencies + run: | + pip install -r requirements.txt + + - name: Load CI environment variables + run: | + set -o allexport + source .env.ci + set +o allexport + + - name: Run migrations + run: python manage.py migrate + + - name: Run tests + run: pytest \ No newline at end of file From f16f2038c8b069a3ed3f496b6ceb892e41512cbb Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:13:53 +0200 Subject: [PATCH 02/11] Added missing GitHub Actions triggers to CI workflow --- .github/workflows/django-ci.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/django-ci.yml b/.github/workflows/django-ci.yml index 85fad2c..466414c 100644 --- a/.github/workflows/django-ci.yml +++ b/.github/workflows/django-ci.yml @@ -1,3 +1,11 @@ +name: Django CI (Postgres) + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + jobs: test: runs-on: ubuntu-latest @@ -14,7 +22,6 @@ jobs: --health-interval=10s --health-timeout=5s --health-retries=5 - redis: image: redis:7 ports: ["6379:6379"] From bdff8cd6ad5aa5ddecc0f8abc544e3a279859dad Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:19:22 +0200 Subject: [PATCH 03/11] Added missing work directory in django-ci.yml file --- .github/workflows/django-ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/django-ci.yml b/.github/workflows/django-ci.yml index 466414c..eea834b 100644 --- a/.github/workflows/django-ci.yml +++ b/.github/workflows/django-ci.yml @@ -35,17 +35,21 @@ jobs: python-version: "3.11" - name: Install dependencies + working-directory: src run: | pip install -r requirements.txt - name: Load CI environment variables + working-directory: src run: | set -o allexport source .env.ci set +o allexport - name: Run migrations + working-directory: src run: python manage.py migrate - name: Run tests + working-directory: src run: pytest \ No newline at end of file From 6720836d772ae28884c326da8807b8594803be0c Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:21:11 +0200 Subject: [PATCH 04/11] Remove work directory in django-ci.yml file for install requirements step. --- .github/workflows/django-ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/django-ci.yml b/.github/workflows/django-ci.yml index eea834b..fe4027e 100644 --- a/.github/workflows/django-ci.yml +++ b/.github/workflows/django-ci.yml @@ -35,7 +35,6 @@ jobs: python-version: "3.11" - name: Install dependencies - working-directory: src run: | pip install -r requirements.txt From 97a2ff1d56dbf5742355f527ffc7c6cd2d2c094c Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:23:34 +0200 Subject: [PATCH 05/11] Remove work directory in django-ci.yml file for loading ci.env variables step. --- .github/workflows/django-ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/django-ci.yml b/.github/workflows/django-ci.yml index fe4027e..9abd764 100644 --- a/.github/workflows/django-ci.yml +++ b/.github/workflows/django-ci.yml @@ -39,7 +39,6 @@ jobs: pip install -r requirements.txt - name: Load CI environment variables - working-directory: src run: | set -o allexport source .env.ci From 6d4cc7b46ea2896810c18b5523815ed3421cc587 Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:31:21 +0200 Subject: [PATCH 06/11] Fixed ci.env variables loading for django-ci.yml. --- .github/workflows/django-ci.yml | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/.github/workflows/django-ci.yml b/.github/workflows/django-ci.yml index 9abd764..1aba49e 100644 --- a/.github/workflows/django-ci.yml +++ b/.github/workflows/django-ci.yml @@ -35,19 +35,20 @@ jobs: python-version: "3.11" - name: Install dependencies - run: | - pip install -r requirements.txt + run: pip install -r requirements.txt - - name: Load CI environment variables + - name: Run migrations + working-directory: src run: | set -o allexport - source .env.ci + source ../.env.ci + python manage.py migrate set +o allexport - - name: Run migrations - working-directory: src - run: python manage.py migrate - - name: Run tests working-directory: src - run: pytest \ No newline at end of file + run: | + set -o allexport + source ../.env.ci + pytest + set +o allexport \ No newline at end of file From 2ce97ae81b338e931ef5d65ad48340a58353a651 Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:36:38 +0200 Subject: [PATCH 07/11] Fixed ci.env variable for web_db in django-ci.yml. --- .env.ci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.ci b/.env.ci index 59fdf07..809588b 100644 --- a/.env.ci +++ b/.env.ci @@ -1,5 +1,5 @@ # Database -DATABASE_URI=postgresql://postgres:postgres@localhost:5432/web +DATABASE_URI=postgresql://postgres:postgres@localhost:5432/web_test DATABASE_URI_TEST=postgresql://postgres:postgres@localhost:5432/web_test # Django From dc64c033942123fde9eb98c4bdc85eb346e702fa Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:57:40 +0200 Subject: [PATCH 08/11] Update Docker configs, add lint workflow, and remove unused env files --- .devcontainer/docker-compose.yml | 8 ++++---- .env.ci => .env_files/.env.ci | 0 .sample_env => .env_files/.env.sample | 0 .github/workflows/lint.yml | 27 +++++++++++++++++++++++++++ docker-compose.yml | 8 ++++---- 5 files changed, 35 insertions(+), 8 deletions(-) rename .env.ci => .env_files/.env.ci (100%) rename .sample_env => .env_files/.env.sample (100%) create mode 100644 .github/workflows/lint.yml diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index 5668144..61bb013 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -9,7 +9,7 @@ services: - ..:/workspace:cached working_dir: /workspace/src env_file: - - ../.env + - ../.env_files/.env command: sleep infinity depends_on: @@ -22,7 +22,7 @@ services: - ..:/workspace:cached working_dir: /workspace/src env_file: - - ../.env + - ../.env_files/.env command: celery -A backend worker --loglevel=info depends_on: @@ -37,7 +37,7 @@ services: - ..:/workspace:cached working_dir: /workspace/src env_file: - - ../.env + - ../.env_files/.env command: celery -A backend beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler depends_on: - redis @@ -57,7 +57,7 @@ services: ports: - "5432:5432" env_file: - - ../.env + - ../.env_files/.env volumes: postgres-data: diff --git a/.env.ci b/.env_files/.env.ci similarity index 100% rename from .env.ci rename to .env_files/.env.ci diff --git a/.sample_env b/.env_files/.env.sample similarity index 100% rename from .sample_env rename to .env_files/.env.sample diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..56f7d43 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,27 @@ +name: Lint & Format + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + - name: Install dependencies + run: pip install -r requirements.txt + - name: Run Black + run: black --check . + - name: Run isort + run: isort --check-only . + - name: Run flake8 + run: flake8 . + - name: Run mypy + run: mypy your_project/ \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 86d0c04..3a54711 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ services: volumes: - ./:/workspace:cached env_file: - - .env + - .env_files/.env command: ["/bin/sh", "/workspace/scripts/django/start.sh"] ports: - "8000:8000" @@ -25,7 +25,7 @@ services: volumes: - ./:/workspace:cached env_file: - - .env + - .env_files/.env command: ["/bin/sh", "/workspace/scripts/celery/worker.sh"] depends_on: db: @@ -43,7 +43,7 @@ services: - ./:/workspace:cached working_dir: /workspace/src env_file: - - .env + - .env_files/.env command: ["/bin/sh", "/workspace/scripts/celery/beat.sh"] depends_on: db: @@ -74,7 +74,7 @@ services: ports: - "5432:5432" env_file: - - .env + - .env_files/.env networks: - backend healthcheck: From 37600b25fe3d0c62fa3483a805b776fea14773e4 Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:22:28 +0000 Subject: [PATCH 09/11] Configure mypy, django-stubs, and VS Code settings; update CI workflows and backend/app files --- .github/workflows/django-ci.yml | 4 ++-- .github/workflows/lint.yml | 4 +--- .vscode/settings.json | 3 ++- setup.cfg | 12 ++++++++---- src/backend/settings.py | 12 +++++++----- src/backend/settings_test.py | 2 +- src/backend/urls.py | 3 ++- src/items/apps.py | 2 +- src/items/migrations/0002_item_external_price.py | 1 + src/items/tests/test_tasks.py | 6 ++---- src/items/views.py | 3 ++- 11 files changed, 29 insertions(+), 23 deletions(-) diff --git a/.github/workflows/django-ci.yml b/.github/workflows/django-ci.yml index 1aba49e..95bd350 100644 --- a/.github/workflows/django-ci.yml +++ b/.github/workflows/django-ci.yml @@ -41,7 +41,7 @@ jobs: working-directory: src run: | set -o allexport - source ../.env.ci + source ../.env_files/.env.ci python manage.py migrate set +o allexport @@ -49,6 +49,6 @@ jobs: working-directory: src run: | set -o allexport - source ../.env.ci + source ../.env_files/.env.ci pytest set +o allexport \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 56f7d43..3a400e3 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -22,6 +22,4 @@ jobs: - name: Run isort run: isort --check-only . - name: Run flake8 - run: flake8 . - - name: Run mypy - run: mypy your_project/ \ No newline at end of file + run: flake8 . \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index e88c9ce..80182b5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,7 +22,8 @@ "python.testing.pytestEnabled": true, "editor.formatOnSave": true, "editor.codeActionsOnSave": { - "source.organizeImports": "always" + "source.organizeImports": "always", + "source.fixAll": "always" }, "editor.rulers": [ 88 diff --git a/setup.cfg b/setup.cfg index e17bcee..4cab5f5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -5,14 +5,18 @@ line-length = 88 # Set line length to 88 disable = C0114 # Disable missing module docstring warning [flake8] -max-line-length = 88 # Set line length to 88 -extend-ignore = E203, W503 # Ignore specific warnings +max-line-length = 88 +extend-ignore = E203, W503 +exclude = .git,__pycache__,migrations,venv +per-file-ignores = + src/backend/settings_test.py: F405 -[isort] +[tool.isort] +profile = "black" multi_line_output = 3 # Vertical hanging indent for imports include_trailing_comma = true # Add trailing comma force_grid_wrap = 0 # Disable wrapping use_parentheses = true # Use parentheses for wrapping ensure_newline_before_comments = true # Newline before comments in imports line_length = 88 # Set line length to 88 -remove_redundant_aliases = true # Remove redundant aliases \ No newline at end of file +remove_redundant_aliases = true # Remove redundant aliases diff --git a/src/backend/settings.py b/src/backend/settings.py index cc55764..1cc45ce 100644 --- a/src/backend/settings.py +++ b/src/backend/settings.py @@ -44,7 +44,7 @@ "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", - "corsheaders.middleware.CorsMiddleware", # CORS middleware should be high in the order + "corsheaders.middleware.CorsMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", @@ -80,11 +80,13 @@ # Password validation AUTH_PASSWORD_VALIDATORS = [ { - "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator" + "NAME": ( + "django.contrib.auth.password_validation.UserAttributeSimilarityValidator" + ) }, - {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"}, - {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"}, - {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"}, + {"NAME": ("django.contrib.auth.password_validation.MinimumLengthValidator")}, + {"NAME": ("django.contrib.auth.password_validation.CommonPasswordValidator")}, + {"NAME": ("django.contrib.auth.password_validation.NumericPasswordValidator")}, ] # Internationalization settings diff --git a/src/backend/settings_test.py b/src/backend/settings_test.py index e98e7b0..8aeae3a 100644 --- a/src/backend/settings_test.py +++ b/src/backend/settings_test.py @@ -1,3 +1,3 @@ -from backend.settings import * # noqa: F401,F403 +from backend.settings import * # noqa: F401,F403,F405 DATABASES = {"default": env.db("DATABASE_URI")} diff --git a/src/backend/urls.py b/src/backend/urls.py index 5d8dac0..e362440 100644 --- a/src/backend/urls.py +++ b/src/backend/urls.py @@ -1,7 +1,8 @@ from django.contrib import admin from django.urls import include, path from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView -from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView +from rest_framework_simplejwt.views import (TokenObtainPairView, + TokenRefreshView) urlpatterns = [ path("admin/", admin.site.urls), diff --git a/src/items/apps.py b/src/items/apps.py index f6e4373..f3a05b1 100644 --- a/src/items/apps.py +++ b/src/items/apps.py @@ -6,4 +6,4 @@ class ItemsConfig(AppConfig): name = "items" def ready(self): - import items.signals + pass diff --git a/src/items/migrations/0002_item_external_price.py b/src/items/migrations/0002_item_external_price.py index da7af5e..17450f4 100644 --- a/src/items/migrations/0002_item_external_price.py +++ b/src/items/migrations/0002_item_external_price.py @@ -1,6 +1,7 @@ # Generated by Django 5.1.7 on 2025-08-01 11:09 from decimal import Decimal + from django.db import migrations, models diff --git a/src/items/tests/test_tasks.py b/src/items/tests/test_tasks.py index d217776..4cb8b69 100644 --- a/src/items/tests/test_tasks.py +++ b/src/items/tests/test_tasks.py @@ -5,10 +5,8 @@ import pytest from items.models import Item -from items.tasks import ( - hourly_external_price_sync, - simulate_external_price_sync_for_item, -) +from items.tasks import (hourly_external_price_sync, + simulate_external_price_sync_for_item) from items.utils.price_sync import sync_all_items diff --git a/src/items/views.py b/src/items/views.py index 02fc5a6..28bb89f 100644 --- a/src/items/views.py +++ b/src/items/views.py @@ -7,7 +7,8 @@ from .models import Item from .serializers import ItemSerializer -from .tasks import hourly_external_price_sync, simulate_external_price_sync_for_item +from .tasks import (hourly_external_price_sync, + simulate_external_price_sync_for_item) class ItemViewSet(viewsets.ModelViewSet): From 610694acc2e9fbd14702c81a63ff9388811631bd Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:25:58 +0000 Subject: [PATCH 10/11] Formated files with black command. --- src/backend/urls.py | 3 +-- src/items/tests/test_tasks.py | 6 ++++-- src/items/views.py | 3 +-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/backend/urls.py b/src/backend/urls.py index e362440..5d8dac0 100644 --- a/src/backend/urls.py +++ b/src/backend/urls.py @@ -1,8 +1,7 @@ from django.contrib import admin from django.urls import include, path from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView -from rest_framework_simplejwt.views import (TokenObtainPairView, - TokenRefreshView) +from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView urlpatterns = [ path("admin/", admin.site.urls), diff --git a/src/items/tests/test_tasks.py b/src/items/tests/test_tasks.py index 4cb8b69..d217776 100644 --- a/src/items/tests/test_tasks.py +++ b/src/items/tests/test_tasks.py @@ -5,8 +5,10 @@ import pytest from items.models import Item -from items.tasks import (hourly_external_price_sync, - simulate_external_price_sync_for_item) +from items.tasks import ( + hourly_external_price_sync, + simulate_external_price_sync_for_item, +) from items.utils.price_sync import sync_all_items diff --git a/src/items/views.py b/src/items/views.py index 28bb89f..02fc5a6 100644 --- a/src/items/views.py +++ b/src/items/views.py @@ -7,8 +7,7 @@ from .models import Item from .serializers import ItemSerializer -from .tasks import (hourly_external_price_sync, - simulate_external_price_sync_for_item) +from .tasks import hourly_external_price_sync, simulate_external_price_sync_for_item class ItemViewSet(viewsets.ModelViewSet): From 0650b0fb71df5151230422230f6b5e219f6d118d Mon Sep 17 00:00:00 2001 From: junior92jr Date: Fri, 22 Aug 2025 19:33:52 +0000 Subject: [PATCH 11/11] Improved setup.cfg file. --- setup.cfg | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/setup.cfg b/setup.cfg index 4cab5f5..6c99721 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,8 +1,15 @@ [black] -line-length = 88 # Set line length to 88 +line-length = 88 -[pylint] -disable = C0114 # Disable missing module docstring warning +[isort] +profile = black +multi_line_output = 3 +include_trailing_comma = true +force_grid_wrap = 0 +use_parentheses = true +ensure_newline_before_comments = true +line_length = 88 +remove_redundant_aliases = true [flake8] max-line-length = 88 @@ -11,12 +18,16 @@ exclude = .git,__pycache__,migrations,venv per-file-ignores = src/backend/settings_test.py: F405 -[tool.isort] -profile = "black" -multi_line_output = 3 # Vertical hanging indent for imports -include_trailing_comma = true # Add trailing comma -force_grid_wrap = 0 # Disable wrapping -use_parentheses = true # Use parentheses for wrapping -ensure_newline_before_comments = true # Newline before comments in imports -line_length = 88 # Set line length to 88 -remove_redundant_aliases = true # Remove redundant aliases +[pylint] +disable = C0114 + +[mypy] +plugins = mypy_django_plugin.main +ignore_missing_imports = True + +[mypy-tests.*] +ignore_errors = True + +[mypy.plugins.django-stubs] +django_settings_module = "backend.settings" +strict_settings = True \ No newline at end of file