From 1439659a018c3e8ff224c8ed467825d8410d8103 Mon Sep 17 00:00:00 2001 From: Dmytro Hryshchenko Date: Mon, 29 Jun 2026 17:32:03 +0100 Subject: [PATCH] style: fix ruff linting and formatting issues --- .github/workflows/pre-commit.yml | 23 ++++++++++++++++++++ docker-compose.test.yml | 2 +- docker-compose.yml | 2 +- website/apps/core/api/__init__.py | 0 website/apps/core/api/docstring.py | 2 ++ website/apps/core/api/schema.py | 15 +++++++++++++ website/apps/core/api/urls.py | 9 ++++++++ website/apps/core/api/views.py | 10 +++++++++ website/apps/core/apps.py | 4 ++-- website/apps/polls/api/v1/docstring.py | 3 +-- website/apps/polls/api/v1/schema.py | 14 ++++++------ website/apps/polls/api/v1/serializers.py | 6 +++++ website/apps/polls/api/v1/urls.py | 4 ++-- website/apps/polls/api/v1/views.py | 19 +++++++++++----- website/manage.py | 1 + website/website/api.py | 1 + website/website/settings/components/cache.py | 2 +- 17 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/pre-commit.yml create mode 100644 website/apps/core/api/__init__.py create mode 100644 website/apps/core/api/docstring.py create mode 100644 website/apps/core/api/schema.py create mode 100644 website/apps/core/api/urls.py create mode 100644 website/apps/core/api/views.py create mode 100644 website/apps/polls/api/v1/serializers.py diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..4c1df67 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,23 @@ +name: Pre-commit + +on: + push: + branches: + - master + pull_request: + +jobs: + pre-commit: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Run pre-commit + uses: pre-commit/action@v3.0.1 \ No newline at end of file diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 34946e7..428d92c 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -32,4 +32,4 @@ services: condition: service_healthy volumes: - ./website:/usr/src/website - command: ["./pytest.sh"] \ No newline at end of file + command: ["./pytest.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index d27ed23..587eff0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -67,4 +67,4 @@ volumes: networks: traefik-public: - external: true \ No newline at end of file + external: true diff --git a/website/apps/core/api/__init__.py b/website/apps/core/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/website/apps/core/api/docstring.py b/website/apps/core/api/docstring.py new file mode 100644 index 0000000..8ff60a1 --- /dev/null +++ b/website/apps/core/api/docstring.py @@ -0,0 +1,2 @@ +HEALTH_DOCS = "Check the health of the API" +HEALTH_RESPONSE_DOCS = "Returns the current health status" diff --git a/website/apps/core/api/schema.py b/website/apps/core/api/schema.py new file mode 100644 index 0000000..1a09e5e --- /dev/null +++ b/website/apps/core/api/schema.py @@ -0,0 +1,15 @@ +from drf_spectacular.utils import OpenApiResponse, extend_schema +from rest_framework import status + +from apps.core.api import docstring + +health_schema = extend_schema( + summary="Check Health", + description=docstring.HEALTH_DOCS, + tags=["Health"], + responses={ + status.HTTP_200_OK: OpenApiResponse( + description=docstring.HEALTH_RESPONSE_DOCS, + ), + }, +) diff --git a/website/apps/core/api/urls.py b/website/apps/core/api/urls.py new file mode 100644 index 0000000..e3d2c6e --- /dev/null +++ b/website/apps/core/api/urls.py @@ -0,0 +1,9 @@ +from django.urls import path + +from apps.core.api.views import health + +app_name = "core" + +urlpatterns = [ + path("health/", health, name="health"), +] diff --git a/website/apps/core/api/views.py b/website/apps/core/api/views.py new file mode 100644 index 0000000..cc2fc1d --- /dev/null +++ b/website/apps/core/api/views.py @@ -0,0 +1,10 @@ +from rest_framework.decorators import api_view +from rest_framework.response import Response + +from apps.core.api.schema import health_schema + + +@health_schema +@api_view(["GET"]) +def health(request): + return Response({"status": "ok"}) diff --git a/website/apps/core/apps.py b/website/apps/core/apps.py index 4143768..ab0051e 100644 --- a/website/apps/core/apps.py +++ b/website/apps/core/apps.py @@ -2,5 +2,5 @@ class CoreConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'apps.core' + default_auto_field = "django.db.models.BigAutoField" + name = "apps.core" diff --git a/website/apps/polls/api/v1/docstring.py b/website/apps/polls/api/v1/docstring.py index 493b189..2aee407 100644 --- a/website/apps/polls/api/v1/docstring.py +++ b/website/apps/polls/api/v1/docstring.py @@ -1,2 +1 @@ -CHECK_HEALTH_DOCS = "Check the health of the API" -CHECK_HEALTH_RESPONSE_DOCS = "Returns empty response" +LIST_POLLS_DOCS = "List all available polls" diff --git a/website/apps/polls/api/v1/schema.py b/website/apps/polls/api/v1/schema.py index c9eb5c6..fbb8ebd 100644 --- a/website/apps/polls/api/v1/schema.py +++ b/website/apps/polls/api/v1/schema.py @@ -1,14 +1,14 @@ -from drf_spectacular.utils import OpenApiResponse, extend_schema +from drf_spectacular.utils import extend_schema from rest_framework import status from apps.polls.api.v1 import docstring +from apps.polls.api.v1.serializers import PollSerializer -check_health_schema = extend_schema( - summary="Check Health API", - description=docstring.CHECK_HEALTH_DOCS, +list_polls_schema = extend_schema( + summary="List Polls", + description=docstring.LIST_POLLS_DOCS, + tags=["Polls"], responses={ - status.HTTP_200_OK: OpenApiResponse( - description=docstring.CHECK_HEALTH_RESPONSE_DOCS, - ), + status.HTTP_200_OK: PollSerializer(many=True), }, ) diff --git a/website/apps/polls/api/v1/serializers.py b/website/apps/polls/api/v1/serializers.py new file mode 100644 index 0000000..9ae7c00 --- /dev/null +++ b/website/apps/polls/api/v1/serializers.py @@ -0,0 +1,6 @@ +from rest_framework import serializers + + +class PollSerializer(serializers.Serializer): + id = serializers.IntegerField() + question = serializers.CharField() diff --git a/website/apps/polls/api/v1/urls.py b/website/apps/polls/api/v1/urls.py index 5832ca1..570f901 100644 --- a/website/apps/polls/api/v1/urls.py +++ b/website/apps/polls/api/v1/urls.py @@ -1,9 +1,9 @@ from django.urls import path -from .views import CheckHealthView +from .views import list_polls app_name = "v1" urlpatterns = [ - path("health/", CheckHealthView.as_view(), name="health"), + path("polls/", list_polls, name="polls"), ] diff --git a/website/apps/polls/api/v1/views.py b/website/apps/polls/api/v1/views.py index d4ea82d..fb0909d 100644 --- a/website/apps/polls/api/v1/views.py +++ b/website/apps/polls/api/v1/views.py @@ -1,10 +1,17 @@ +from rest_framework.decorators import api_view from rest_framework.response import Response -from rest_framework.views import APIView -from apps.polls.api.v1.schema import check_health_schema +from apps.polls.api.v1.schema import list_polls_schema +from apps.polls.api.v1.serializers import PollSerializer +FAKE_POLLS = [ + {"id": 1, "question": "What is your favorite color?"}, + {"id": 2, "question": "What is your favorite food?"}, +] -class CheckHealthView(APIView): - @check_health_schema - def get(self, request): - return Response({"status": "ok"}) + +@list_polls_schema +@api_view(["GET"]) +def list_polls(request): + serializer = PollSerializer(FAKE_POLLS, many=True) + return Response(serializer.data) diff --git a/website/manage.py b/website/manage.py index f425f2f..1a1444c 100755 --- a/website/manage.py +++ b/website/manage.py @@ -1,5 +1,6 @@ #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" + import os import sys diff --git a/website/website/api.py b/website/website/api.py index a866667..052aaf9 100644 --- a/website/website/api.py +++ b/website/website/api.py @@ -3,5 +3,6 @@ app_name = "api" urlpatterns = [ + path("", include("apps.core.api.urls")), path("", include("apps.polls.api.urls")), ] diff --git a/website/website/settings/components/cache.py b/website/website/settings/components/cache.py index 7726f86..8cda70d 100644 --- a/website/website/settings/components/cache.py +++ b/website/website/settings/components/cache.py @@ -10,4 +10,4 @@ "CLIENT_CLASS": "django_redis.client.DefaultClient", }, } -} \ No newline at end of file +}