From b1d26db2ca1d4eb08f16777bc63d80f35617bd1d Mon Sep 17 00:00:00 2001 From: naiv Date: Sun, 17 Dec 2023 03:58:34 +0200 Subject: [PATCH 1/2] added drf-spectacular and configured documentations --- book_service/views.py | 18 ++++++++++++++++++ borrowing_service/views.py | 14 ++++++++++++++ core/settings.py | 11 ++++++++++- core/urls.py | 11 ++++++++++- payment_service/tasks.py | 2 -- payment_service/views.py | 3 +++ requirements.txt | 2 +- 7 files changed, 56 insertions(+), 5 deletions(-) diff --git a/book_service/views.py b/book_service/views.py index f950684..98c686c 100644 --- a/book_service/views.py +++ b/book_service/views.py @@ -1,3 +1,4 @@ +from drf_spectacular.utils import extend_schema, OpenApiParameter from rest_framework import viewsets from book_service.models import Book @@ -27,3 +28,20 @@ def get_queryset(self): self.queryset = self.queryset.filter(author__icontains=author) return self.queryset + + @extend_schema( + parameters=[ + OpenApiParameter( + name="title", + type={"type": "string", "items": {"type": "string"}}, + description="Filter books by title (ex. ?title=the)" + ), + OpenApiParameter( + name="author", + type={"type": "string", "items": {"type": "string"}}, + description="Filter books by author (ex. ?author=Rob)" + ) + ] + ) + def list(self, request, *args, **kwargs): + return super().list(request, args, kwargs) diff --git a/borrowing_service/views.py b/borrowing_service/views.py index 211f737..043ff2c 100644 --- a/borrowing_service/views.py +++ b/borrowing_service/views.py @@ -1,5 +1,6 @@ from django.utils import timezone from django.db import transaction +from drf_spectacular.utils import extend_schema, OpenApiParameter from rest_framework import viewsets, mixins from rest_framework.decorators import api_view, permission_classes from rest_framework.permissions import IsAuthenticated @@ -54,11 +55,24 @@ def get_serializer_class(self): def perform_create(self, serializer): serializer.save(user=self.request.user) + @extend_schema( + parameters=[ + OpenApiParameter( + name="is_active", + type={"type": "string", "items": {"type": "string"}}, + description="Filter by borrowing status (ex. ?is_active=True)" + ) + ] + ) + def list(self, request, *args, **kwargs): + return super().list(request, args, kwargs) + @api_view(["POST"]) @permission_classes([IsAuthenticated]) @transaction.atomic def borrowing_return(request, pk): + """Endpoint for returning book""" borrowing = Borrowing.objects.get(pk=pk) if request.method == "POST": diff --git a/core/settings.py b/core/settings.py index c2d6545..1365aca 100644 --- a/core/settings.py +++ b/core/settings.py @@ -48,7 +48,8 @@ "customer", "book_service", "borrowing_service", - "payment_service" + "payment_service", + "drf_spectacular" ] MIDDLEWARE = [ @@ -142,6 +143,14 @@ "DEFAULT_AUTHENTICATION_CLASSES": ( "rest_framework_simplejwt.authentication.JWTAuthentication", ), + "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema", +} + +SPECTACULAR_SETTINGS = { + "TITLE": "Your Project API", + "DESCRIPTION": "Your project description", + "VERSION": "1.0.0", + "SERVE_INCLUDE_SCHEMA": False, } SIMPLE_JWT = { diff --git a/core/urls.py b/core/urls.py index e01a50c..7c4a677 100644 --- a/core/urls.py +++ b/core/urls.py @@ -16,6 +16,11 @@ """ from django.contrib import admin from django.urls import path, include +from drf_spectacular.views import ( + SpectacularAPIView, + SpectacularSwaggerView, + SpectacularRedocView +) urlpatterns = [ path("admin/", admin.site.urls), @@ -30,5 +35,9 @@ path("api/users/", include("customer.urls", namespace="customer")), path("api/payment_service/", - include("payment_service.urls", namespace="payment_service")) + include("payment_service.urls", namespace="payment_service")), + + path("api/doc/", SpectacularAPIView.as_view(), name="schema"), + path("api/doc/swagger/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger"), + path("api/doc/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"), ] diff --git a/payment_service/tasks.py b/payment_service/tasks.py index d9804b1..5e1aab8 100644 --- a/payment_service/tasks.py +++ b/payment_service/tasks.py @@ -16,8 +16,6 @@ def check_if_session_expired(session_id: str) -> bool: @shared_task def verify_session_status() -> None: """Verify if pending sessions did not expire""" - print("checking for expired checkouts") - payments = Payment.objects.filter(status=Payment.PaymentStatus.PENDING) for payment in payments: diff --git a/payment_service/views.py b/payment_service/views.py index 8b993d6..b74f691 100644 --- a/payment_service/views.py +++ b/payment_service/views.py @@ -32,6 +32,7 @@ def get_serializer_class(self): @action(methods=["GET"], url_path="success", detail=True) def payment_successful(self, request, pk: None): + """Endpoint for redirection after successful payment""" payment = self.get_object() session = stripe.checkout.Session.retrieve(payment.session_id) status = session.get("payment_intent", {}).get("status") @@ -54,6 +55,7 @@ def payment_successful(self, request, pk: None): @action(methods=["GET"], url_path="cancel", detail=True) def payment_canceled(self, request, pk: None): + """Endpoint for redirection after payment was canceled or failed""" return Response( {"status": "fail", "message": "Payment was canceled. Please complete payment " @@ -63,6 +65,7 @@ def payment_canceled(self, request, pk: None): @action(methods=["POST"], url_path="renew-session", detail=True) def renew_checkout(self, request, pk: None): + """Endpoint for renewing checkout session if it expired""" payment = self.get_object() new_session = get_checkout_session(payment.borrowing, payment.id) diff --git a/requirements.txt b/requirements.txt index af77829..3ac5963 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,7 +17,7 @@ django-debug-toolbar==3.4.0 django-timezone-field==6.1.0 djangorestframework==3.13.1 djangorestframework-simplejwt==5.2.0 -drf-spectacular==0.22.1 +drf-spectacular==0.27.0 flake8==5.0.4 flake8-quotes==3.3.1 flake8-variables-names==0.0.5 From 0d300d9b5daf8d4849e278526787e778067e47ee Mon Sep 17 00:00:00 2001 From: naiv Date: Sun, 17 Dec 2023 04:01:18 +0200 Subject: [PATCH 2/2] corrected flake8 --- core/urls.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/urls.py b/core/urls.py index 7c4a677..3057efc 100644 --- a/core/urls.py +++ b/core/urls.py @@ -38,6 +38,8 @@ include("payment_service.urls", namespace="payment_service")), path("api/doc/", SpectacularAPIView.as_view(), name="schema"), - path("api/doc/swagger/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger"), - path("api/doc/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"), + path("api/doc/swagger/", + SpectacularSwaggerView.as_view(url_name="schema"), name="swagger"), + path("api/doc/redoc/", + SpectacularRedocView.as_view(url_name="schema"), name="redoc"), ]