Refactor payment views: rename CreatePaymentView to ListCreatePaymentView, add payment retrieval endpoint, and enhance queryset filtering for user-specific payments.
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 3m15s

This commit is contained in:
i701 2025-04-05 17:23:18 +05:00
parent 80e388a2a0
commit 02f680d579
Signed by: i701
GPG Key ID: 54A0DA1E26D8E587
2 changed files with 17 additions and 4 deletions

View File

@ -1,8 +1,9 @@
# billing/urls.py # billing/urls.py
from django.urls import path from django.urls import path
from .views import CreatePaymentView, VerifyPaymentView from .views import ListCreatePaymentView, VerifyPaymentView
urlpatterns = [ urlpatterns = [
path("create-payment/", CreatePaymentView.as_view(), name="create-payment"), path("payment/", ListCreatePaymentView.as_view(), name="create-payment"),
path("payment/<str:pk>/", ListCreatePaymentView.as_view(), name="retrieve-payment"),
path("verify-payment/", VerifyPaymentView.as_view(), name="verify-payment"), path("verify-payment/", VerifyPaymentView.as_view(), name="verify-payment"),
] ]

View File

@ -17,9 +17,15 @@ class InsufficientFundsError(Exception):
pass pass
class CreatePaymentView(StaffEditorPermissionMixin, generics.CreateAPIView): class ListCreatePaymentView(StaffEditorPermissionMixin, generics.ListCreateAPIView):
serializer_class = PaymentSerializer serializer_class = PaymentSerializer
queryset = Payment.objects.all() queryset = Payment.objects.all().select_related("user")
def get_queryset(self):
queryset = super().get_queryset()
if self.request.user.is_superuser:
return queryset
return queryset.filter(user=self.request.user)
def create(self, request): def create(self, request):
data = request.data data = request.data
@ -63,6 +69,12 @@ class CreatePaymentView(StaffEditorPermissionMixin, generics.CreateAPIView):
return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_201_CREATED)
class PaymentDetailAPIView(StaffEditorPermissionMixin, generics.RetrieveAPIView):
queryset = Payment.objects.select_related("user", "devices").all()
serializer_class = PaymentSerializer
lookup_field = "pk"
class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView): class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
serializer_class = PaymentSerializer serializer_class = PaymentSerializer
queryset = Payment.objects.all() queryset = Payment.objects.all()