Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor borrowing service #37

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions borrowing_service/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class BorrowingViewSet(
mixins.RetrieveModelMixin,
viewsets.GenericViewSet
):
queryset = Borrowing.objects.select_related("book", "user")
queryset = Borrowing.objects.all()
permission_classes = (IsAuthenticated,)

def get_queryset(self):
queryset = self.queryset.select_related("book", "user")
if not self.request.user.is_superuser:
queryset = self.queryset.filter(user=self.request.user)
queryset = queryset.filter(user=self.request.user)
else:
queryset = self.queryset
user_id = self.request.query_params.get("user_id")
if user_id:
queryset = queryset.filter(user__id=user_id)
Expand Down
2 changes: 1 addition & 1 deletion payment_service/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def check_if_session_expired(session_id: str) -> bool:
session = stripe.checkout.Session.retrieve(session_id)
print(session)
if session:
status = session.get("payment_intent", {}).get("status")
status = session.status
if status == "expired":
return True
return False
Expand Down
15 changes: 8 additions & 7 deletions payment_service/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@


class PaymentViewSet(viewsets.ModelViewSet):
queryset = Payment.objects.select_related(
"borrowing", "borrowing__book", "borrowing__user"
)
queryset = Payment.objects.all()
permission_classes = (IsAuthenticated,)

def get_queryset(self):
queryset = self.queryset.select_related(
"borrowing", "borrowing__book", "borrowing__user"
)
if (not self.request.user.is_staff
or not self.request.user.is_superuser):
return self.queryset.filter(borrowing__user=self.request.user)
return self.queryset
return queryset

def get_serializer_class(self):
if self.action == "list":
Expand All @@ -40,15 +41,15 @@ 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")
if status != "succeeded":
status = session.status
if status != "complete":
return Response(
{"status": "fail",
"message": "Payment failed, please complete payment "
"within 24 hours from book borrowing time!"},
status=400,
)
payment.status = "paid"
payment.status = payment.PaymentStatus.PAID
payment.save()

successful_payment_notification(payment)
Expand Down
Loading