mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-10-09 09:51:36 +00:00
fix(tasks): add device activation/deactivation & blocking/unblocking for background task 🐛
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m17s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m17s
This commit is contained in:
@@ -27,6 +27,7 @@ from .filters import PaymentFilter, TopupFilter, WalletTransactionFilter
|
||||
from dataclasses import dataclass, asdict
|
||||
from typing import Optional
|
||||
from api.models import User
|
||||
from api.omada import Omada
|
||||
|
||||
env.read_env(os.path.join(BASE_DIR, ".env"))
|
||||
|
||||
@@ -77,10 +78,10 @@ class ListCreatePaymentView(StaffEditorPermissionMixin, generics.ListCreateAPIVi
|
||||
def create(self, request):
|
||||
data = request.data
|
||||
user = request.user
|
||||
amount = data.get("amount")
|
||||
number_of_months = data.get("number_of_months")
|
||||
number_of_devices = 0
|
||||
device_ids = data.get("device_ids", [])
|
||||
print(amount, number_of_months, device_ids)
|
||||
print(number_of_months, device_ids)
|
||||
current_time = timezone.now()
|
||||
expires_at = current_time + timedelta(minutes=10)
|
||||
for device_id in device_ids:
|
||||
@@ -91,9 +92,10 @@ class ListCreatePaymentView(StaffEditorPermissionMixin, generics.ListCreateAPIVi
|
||||
{"message": f"Device with id {device_id} not found."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
if not amount or not number_of_months:
|
||||
number_of_devices += 1
|
||||
if not number_of_months:
|
||||
return Response(
|
||||
{"message": "amount and number_of_months are required."},
|
||||
{"message": "number_of_months is required."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
if not device_ids:
|
||||
@@ -102,6 +104,7 @@ class ListCreatePaymentView(StaffEditorPermissionMixin, generics.ListCreateAPIVi
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
# Create payment
|
||||
amount = 100
|
||||
payment = Payment.objects.create(
|
||||
amount=amount,
|
||||
number_of_months=number_of_months,
|
||||
@@ -180,8 +183,7 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
devices = payment.devices.all()
|
||||
data = request.data
|
||||
user = request.user
|
||||
print("logged in user", user)
|
||||
print("Payment user", payment.user)
|
||||
omada_client = Omada()
|
||||
if payment.paid:
|
||||
return Response(
|
||||
{"message": "Payment has already been verified."},
|
||||
@@ -211,6 +213,23 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
payment,
|
||||
devices,
|
||||
)
|
||||
device_list = []
|
||||
for device in devices:
|
||||
device_list.append(
|
||||
{
|
||||
"mac": device.mac,
|
||||
"name": device.name,
|
||||
}
|
||||
)
|
||||
if device.registered:
|
||||
omada_client.block_device(
|
||||
mac_address=device.mac, operation="unblock"
|
||||
)
|
||||
if not device.registered:
|
||||
# Add to omada
|
||||
add_new_devices_to_omada.defer(new_devices=device_list)
|
||||
device.registered = True
|
||||
device.save()
|
||||
return Response(
|
||||
{
|
||||
"status": True,
|
||||
@@ -233,7 +252,6 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
is_active=True,
|
||||
expiry_date=expiry_date,
|
||||
has_a_pending_payment=False,
|
||||
registered=True,
|
||||
)
|
||||
payment.status = "PAID"
|
||||
payment.save()
|
||||
@@ -246,6 +264,10 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
"name": device.name,
|
||||
}
|
||||
)
|
||||
if device.registered:
|
||||
omada_client.block_device(
|
||||
mac_address=device.mac, operation="unblock"
|
||||
)
|
||||
if not device.registered:
|
||||
# Add to omada
|
||||
add_new_devices_to_omada.defer(new_devices=device_list)
|
||||
@@ -288,7 +310,6 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
is_active=True,
|
||||
expiry_date=expiry_date,
|
||||
has_a_pending_payment=False,
|
||||
registered=True,
|
||||
)
|
||||
payment.save()
|
||||
|
||||
|
Reference in New Issue
Block a user