mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-04-19 17:36:53 +00:00
Add has_a_pending_payment field to Device model and update related views for payment handling
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 2m38s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 2m38s
This commit is contained in:
parent
c127c5d093
commit
c3abdd8e34
@ -1,6 +1,6 @@
|
||||
# Create your views here.
|
||||
# billing/views.py
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
import requests
|
||||
@ -64,6 +64,9 @@ class ListCreatePaymentView(StaffEditorPermissionMixin, generics.ListCreateAPIVi
|
||||
|
||||
# Connect devices to payment
|
||||
devices = Device.objects.filter(id__in=device_ids, user=user)
|
||||
for device in devices:
|
||||
device.has_a_pending_payment = True
|
||||
device.save()
|
||||
payment.devices.set(devices)
|
||||
|
||||
serializer = PaymentSerializer(payment)
|
||||
@ -82,20 +85,9 @@ class UpdatePaymentAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
lookup_field = "pk"
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
number_of_months = request.data.get("number_of_months")
|
||||
|
||||
if not number_of_months:
|
||||
return Response(
|
||||
{"message": "number_of_months is required."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
if not isinstance(number_of_months, int):
|
||||
return Response(
|
||||
{"message": "number_of_months must be an integer."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
device_expire_date = timezone.now() + timedelta(days=30 * number_of_months)
|
||||
instance = self.get_object()
|
||||
number_of_months = instance.number_of_months
|
||||
device_expire_date = timezone.now() + timedelta(days=30 * number_of_months)
|
||||
devices = instance.devices.all()
|
||||
serializer = self.get_serializer(instance, data=request.data, partial=False)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
@ -142,8 +134,10 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
self.verify_external_payment(data, payment)
|
||||
|
||||
# Update devices
|
||||
expiry_date = datetime.now() + timedelta(days=30 * payment.number_of_months)
|
||||
devices.update(is_active=True, expiry_date=expiry_date)
|
||||
expiry_date = timezone.now() + timedelta(days=30 * payment.number_of_months)
|
||||
devices.update(
|
||||
is_active=True, expiry_date=expiry_date, has_a_pending_payment=False
|
||||
)
|
||||
|
||||
return Response({"message": "Payment verified successfully."})
|
||||
|
||||
@ -171,7 +165,7 @@ class VerifyPaymentView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
)
|
||||
|
||||
payment.paid = True
|
||||
payment.paid_at = datetime.now()
|
||||
payment.paid_at = timezone.now()
|
||||
payment.method = "WALLET"
|
||||
payment.save()
|
||||
|
||||
|
17
devices/migrations/0005_device_has_a_pending_payment.py
Normal file
17
devices/migrations/0005_device_has_a_pending_payment.py
Normal file
@ -0,0 +1,17 @@
|
||||
# Generated by Django 5.2 on 2025-04-07 17:11
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("devices", "0004_alter_device_id"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="device",
|
||||
name="has_a_pending_payment",
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
@ -3,19 +3,25 @@ from django.utils import timezone
|
||||
from api.models import User
|
||||
|
||||
|
||||
|
||||
class Device(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
mac = models.CharField(max_length=255)
|
||||
has_a_pending_payment = models.BooleanField(default=False)
|
||||
reason_for_blocking = models.CharField(max_length=255, null=True, blank=True)
|
||||
is_active = models.BooleanField(default=False)
|
||||
registered = models.BooleanField(default=False)
|
||||
blocked = models.BooleanField(default=False)
|
||||
blocked_by = models.CharField(max_length=255, choices=[('ADMIN', 'Admin'), ('PARENT', 'Parent')], default='PARENT')
|
||||
blocked_by = models.CharField(
|
||||
max_length=255,
|
||||
choices=[("ADMIN", "Admin"), ("PARENT", "Parent")],
|
||||
default="PARENT",
|
||||
)
|
||||
expiry_date = models.DateTimeField(null=True, blank=True)
|
||||
created_at = models.DateTimeField(default=timezone.now)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='devices')
|
||||
user = models.ForeignKey(
|
||||
User, on_delete=models.SET_NULL, null=True, blank=True, related_name="devices"
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
return self.name
|
||||
|
@ -10,7 +10,13 @@ class CreateDeviceSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Device
|
||||
fields = ["name", "mac", "registered", "blocked_by"]
|
||||
fields = [
|
||||
"name",
|
||||
"mac",
|
||||
"registered",
|
||||
"blocked_by",
|
||||
]
|
||||
depth = 2
|
||||
|
||||
|
||||
class BlockDeviceSerializer(serializers.ModelSerializer):
|
||||
|
@ -17,7 +17,7 @@ class DeviceListCreateAPIView(
|
||||
StaffEditorPermissionMixin,
|
||||
generics.ListCreateAPIView,
|
||||
):
|
||||
queryset = Device.objects.select_related("user").all()
|
||||
queryset = Device.objects.select_related("user").prefetch_related("payments").all()
|
||||
serializer_class = CreateDeviceSerializer
|
||||
filter_backends = [DjangoFilterBackend]
|
||||
filterset_fields = "__all__"
|
||||
|
Loading…
x
Reference in New Issue
Block a user