Friday, 25 August 2023

Implementing Name Synchronization and Money Transfers in Transactions Model with Account Number Input

I have the following models in my Django application:

class Transaction (models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    account_number = models.IntegerField()
    name = models.CharField(max_length=50)
    amount = models.DecimalField(max_digits=5, decimal_places=2)
    created_on = models.DateTimeField()

class Wallet(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    account_balance = models.DecimalField(max_digits=5, decimal_places=2, default=0)

class AccountNum(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    account_number = models.IntegerField()
    slug = models.SlugField(unique=True)

I want to implement a feature where the name field in the Transactions model gets synchronized with the account owner's name based on the provided account_number input. Additionally, I want to enable money transfers using the current user's wallet and the specified amount in the Transactions model.

To provide some context, I have a post-save signal generate_account_number which generates a random 10-digit account number.

What are some recommended techniques or approaches to achieve this synchronization of the name field with the account owner's name and enable money transfers using the wallet model and specified amount in the Transaction model?

This is what I have try.

I try to use django-channels for synchronization, But I have failed to do so. I tried to copy from some people by Googling how to do that, but I don't think I can do this alone. Here is what I have tried:

consumers.py

from channels.generic.websocket import AsyncWebsocketConsumer
import json
from .models import AccountNum

class TransferConsumers(AsyncWebsocketConsumer):
    async def connect(self):
        await self.connect()

    async def disconnect(self, close_data):
        pass

    async def receive(self, text_data):
        data = json.load(text_data)
        account_number = data['account_number']

        try:
            account = AccountNum.objects.get(account_number=account_number)
            access_name = account.user.username
        except AccountNum.DoesNotExist:
            access_name = 'Name Not found'

routing.py

from django.urls import re_path
from . consumers import TransferConsumers

websocket_urlpatters = [
    re_path('/ws/access/', TransferConsumers.as_asgi()
]

The reason I didn't add my template was due to the JavaScript code required to make that happen. I don't have experience in JavaScript, but I would like to learn more about it.

views.py:

def make_transfer(request):
    if request.method == 'POST':
        account_number = request.POST.get('account_number')
        name = request.POST.get('name')
        amount = request.POST.get('amount')
    return render(request, 'Profile/make_transfer.html')


from Implementing Name Synchronization and Money Transfers in Transactions Model with Account Number Input

No comments:

Post a Comment