mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-04-19 23:46:53 +00:00
Add device existence check in CreatePaymentView to handle invalid device IDs
This commit is contained in:
parent
43f9b7ef7c
commit
d386c7e37c
@ -1,5 +1,6 @@
|
|||||||
# Create your views here.
|
# Create your views here.
|
||||||
# billing/views.py
|
# billing/views.py
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
from .models import Payment, Device
|
from .models import Payment, Device
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import requests
|
import requests
|
||||||
@ -29,6 +30,13 @@ class CreatePaymentView(StaffEditorPermissionMixin, generics.CreateAPIView):
|
|||||||
device_ids = data.get("device_ids", [])
|
device_ids = data.get("device_ids", [])
|
||||||
print(amount, number_of_months, device_ids)
|
print(amount, number_of_months, device_ids)
|
||||||
|
|
||||||
|
for device_id in device_ids:
|
||||||
|
device = get_object_or_404(Device, id=device_id)
|
||||||
|
if not device:
|
||||||
|
return Response(
|
||||||
|
{"message": f"Device with id {device_id} not found."},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
if not amount or not number_of_months:
|
if not amount or not number_of_months:
|
||||||
return Response(
|
return Response(
|
||||||
{"message": "amount and number_of_months are required."},
|
{"message": "amount and number_of_months are required."},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user