Skip to content

Commit

Permalink
Merge pull request #27 from Naz-iv/prepare-documentation
Browse files Browse the repository at this point in the history
Prepare documentation
  • Loading branch information
RVChornyy authored Dec 18, 2023
2 parents 558e8f1 + 0d300d9 commit c23e79c
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
18 changes: 18 additions & 0 deletions book_service/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from drf_spectacular.utils import extend_schema, OpenApiParameter
from rest_framework import viewsets

from book_service.models import Book
Expand Down Expand Up @@ -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)
14 changes: 14 additions & 0 deletions borrowing_service/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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":
Expand Down
11 changes: 10 additions & 1 deletion core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"customer",
"book_service",
"borrowing_service",
"payment_service"
"payment_service",
"drf_spectacular"
]

MIDDLEWARE = [
Expand Down Expand Up @@ -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 = {
Expand Down
13 changes: 12 additions & 1 deletion core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -30,5 +35,11 @@
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"),
]
2 changes: 0 additions & 2 deletions payment_service/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions payment_service/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 "
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c23e79c

Please sign in to comment.