rename fields
This commit is contained in:
parent
43fd0ff3b3
commit
bd47c2252d
10 changed files with 116 additions and 20 deletions
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 5.0.6 on 2024-08-10 12:13
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('gooyal_oauth2', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='accesstoken',
|
||||||
|
old_name='client_owner',
|
||||||
|
new_name='application_owner',
|
||||||
|
),
|
||||||
|
]
|
||||||
18
apps/gooyal_oauth2/migrations/0003_application_balance.py
Normal file
18
apps/gooyal_oauth2/migrations/0003_application_balance.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 5.0.6 on 2024-08-10 12:28
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('gooyal_oauth2', '0002_rename_client_owner_accesstoken_application_owner'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='application',
|
||||||
|
name='balance',
|
||||||
|
field=models.BigIntegerField(default=0, verbose_name='balance'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -3,13 +3,15 @@ from django.db import models
|
||||||
from oauth2_provider.models import AbstractApplication, AbstractAccessToken, AbstractGrant, AbstractRefreshToken, \
|
from oauth2_provider.models import AbstractApplication, AbstractAccessToken, AbstractGrant, AbstractRefreshToken, \
|
||||||
AbstractIDToken
|
AbstractIDToken
|
||||||
import uuid
|
import uuid
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
class AccessToken(AbstractAccessToken):
|
class AccessToken(AbstractAccessToken):
|
||||||
id = None
|
id = None
|
||||||
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4, unique=True, db_index=True)
|
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4, unique=True, db_index=True)
|
||||||
detail = models.JSONField(null=True, blank=True)
|
detail = models.JSONField(null=True, blank=True)
|
||||||
client_id = models.CharField(max_length=255, null=True, blank=True)
|
client_id = models.CharField(max_length=255, null=True, blank=True)
|
||||||
client_owner = models.ForeignKey('users.User', on_delete=models.PROTECT, null=True, blank=True)
|
application_owner = models.ForeignKey('users.User', on_delete=models.PROTECT, null=True, blank=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = False
|
abstract = False
|
||||||
|
|
@ -18,6 +20,7 @@ class AccessToken(AbstractAccessToken):
|
||||||
class Application(AbstractApplication):
|
class Application(AbstractApplication):
|
||||||
id = None
|
id = None
|
||||||
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4, unique=True, db_index=True)
|
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4, unique=True, db_index=True)
|
||||||
|
balance = models.BigIntegerField(_('balance'), default=0)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = False
|
abstract = False
|
||||||
|
|
|
||||||
|
|
@ -160,7 +160,7 @@ class OAuth2Validator(BaseOAuth2Validator): # pylint: disable=w0223
|
||||||
defaults={
|
defaults={
|
||||||
"user": user,
|
"user": user,
|
||||||
"client_id": content["client_id"],
|
"client_id": content["client_id"],
|
||||||
"client_owner": owner,
|
"application_owner": owner,
|
||||||
"application_id": application_uuid,
|
"application_id": application_uuid,
|
||||||
"scope": scope,
|
"scope": scope,
|
||||||
"expires": expires,
|
"expires": expires,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 5.0.6 on 2024-08-10 12:36
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('users', '0004_alter_oauthcode_code_verifier'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='user',
|
||||||
|
old_name='last_update',
|
||||||
|
new_name='updated_at',
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -66,7 +66,7 @@ class User(AbstractUser):
|
||||||
iban = models.CharField(null=True, max_length=30, blank=True)
|
iban = models.CharField(null=True, max_length=30, blank=True)
|
||||||
iban_verified = models.BooleanField(null=True)
|
iban_verified = models.BooleanField(null=True)
|
||||||
|
|
||||||
last_update = models.DateTimeField(_('last update'), max_length=30, blank=True, null=True, auto_now=True)
|
updated_at = models.DateTimeField(_('last update'), max_length=30, blank=True, null=True, auto_now=True)
|
||||||
last_login = models.DateTimeField(_('last login'), blank=True, null=True)
|
last_login = models.DateTimeField(_('last login'), blank=True, null=True)
|
||||||
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
|
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
# Generated by Django 5.0.6 on 2024-08-10 12:28
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('wallet', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RenameField(
|
||||||
|
model_name='transaction',
|
||||||
|
old_name='last_update',
|
||||||
|
new_name='updated_at',
|
||||||
|
),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='transaction',
|
||||||
|
name='create',
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='transaction',
|
||||||
|
name='created_at',
|
||||||
|
field=models.DateTimeField(auto_now=True, null=True, verbose_name='created_at'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -57,8 +57,10 @@ class Transaction(models.Model):
|
||||||
payer = models.ForeignKey(User, on_delete=models.PROTECT, related_name='payments', blank=True, null=True)
|
payer = models.ForeignKey(User, on_delete=models.PROTECT, related_name='payments', blank=True, null=True)
|
||||||
payee = models.ForeignKey(User, on_delete=models.PROTECT, related_name='receipts')
|
payee = models.ForeignKey(User, on_delete=models.PROTECT, related_name='receipts')
|
||||||
state = models.IntegerField(choices=StateChoices.choices, default=StateChoices.CREATED)
|
state = models.IntegerField(choices=StateChoices.choices, default=StateChoices.CREATED)
|
||||||
create = models.DateTimeField(_('create'), blank=True, null=True, auto_now=True)
|
# create = models.DateTimeField(_('create'), blank=True, null=True, auto_now=True)
|
||||||
last_update = models.DateTimeField(_('last update'), blank=True, null=True, auto_now=True)
|
created_at = models.DateTimeField(_('created_at'), blank=True, null=True, auto_now=True)
|
||||||
|
# last_update = models.DateTimeField(_('last update'), blank=True, null=True, auto_now=True)
|
||||||
|
updated_at = models.DateTimeField(_('last update'), blank=True, null=True, auto_now=True)
|
||||||
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL, on_delete=models.PROTECT,
|
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL, on_delete=models.PROTECT,
|
||||||
related_name='transactions', null=True)
|
related_name='transactions', null=True)
|
||||||
detail_id = models.IntegerField(null=True, blank=True) # TODO: or better detail url
|
detail_id = models.IntegerField(null=True, blank=True) # TODO: or better detail url
|
||||||
|
|
@ -83,7 +85,7 @@ class Transaction(models.Model):
|
||||||
self.payer = payer
|
self.payer = payer
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
self.last_update = datetime.datetime.now()
|
self.updated_at = datetime.datetime.now()
|
||||||
|
|
||||||
if self.payer.balance < self.amount:
|
if self.payer.balance < self.amount:
|
||||||
raise APIException('not enough balance')
|
raise APIException('not enough balance')
|
||||||
|
|
@ -115,7 +117,7 @@ class Transaction(models.Model):
|
||||||
raise APIException('this transaction is not delayed')
|
raise APIException('this transaction is not delayed')
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
self.last_update = datetime.datetime.now()
|
self.updated_at = datetime.datetime.now()
|
||||||
self.payee.balance = F('balance') + self.amount
|
self.payee.balance = F('balance') + self.amount
|
||||||
self.state = StateChoices.SUCCESS
|
self.state = StateChoices.SUCCESS
|
||||||
self.payee.save()
|
self.payee.save()
|
||||||
|
|
@ -133,7 +135,7 @@ class Transaction(models.Model):
|
||||||
raise APIException('this transaction is not ready to pay')
|
raise APIException('this transaction is not ready to pay')
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
self.last_update = datetime.datetime.now()
|
self.updated_at = datetime.datetime.now()
|
||||||
|
|
||||||
if self.payer.balance < self.amount:
|
if self.payer.balance < self.amount:
|
||||||
raise APIException('not enough balance')
|
raise APIException('not enough balance')
|
||||||
|
|
@ -155,7 +157,7 @@ class Transaction(models.Model):
|
||||||
raise APIException('this transaction is not pending')
|
raise APIException('this transaction is not pending')
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
self.last_update = datetime.datetime.now()
|
self.updated_at = datetime.datetime.now()
|
||||||
self.payee.balance = F('balance') + self.amount
|
self.payee.balance = F('balance') + self.amount
|
||||||
self.state = StateChoices.SUCCESS
|
self.state = StateChoices.SUCCESS
|
||||||
self.payee.save()
|
self.payee.save()
|
||||||
|
|
|
||||||
|
|
@ -14,15 +14,15 @@ class TransactionSerializer(serializers.ModelSerializer):
|
||||||
fields = ('uuid',
|
fields = ('uuid',
|
||||||
'payer',
|
'payer',
|
||||||
'payee',
|
'payee',
|
||||||
"create",
|
"created_at",
|
||||||
"state",
|
"state",
|
||||||
'amount',
|
'amount',
|
||||||
'delay',
|
'delay',
|
||||||
'last_update',
|
'updated_at',
|
||||||
# 'detail_id',
|
# 'detail_id',
|
||||||
)
|
)
|
||||||
|
|
||||||
read_only_fields = ['uuid', 'last_update', 'state', 'create'] # , 'detail_id']
|
read_only_fields = ['uuid', 'updated_at', 'state', 'created_at'] # , 'detail_id']
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
# payer = validated_data.get('payer')
|
# payer = validated_data.get('payer')
|
||||||
|
|
@ -48,15 +48,15 @@ class DepositSerializer(serializers.ModelSerializer):
|
||||||
'uuid',
|
'uuid',
|
||||||
'payer',
|
'payer',
|
||||||
'payee',
|
'payee',
|
||||||
"create",
|
"created_at",
|
||||||
"state",
|
"state",
|
||||||
'amount',
|
'amount',
|
||||||
'delay',
|
'delay',
|
||||||
'last_update',
|
'updated_at',
|
||||||
# 'detail_id',
|
# 'detail_id',
|
||||||
)
|
)
|
||||||
|
|
||||||
read_only_fields = ['last_update', 'state', 'create', 'payer'] # , 'detail_id']
|
read_only_fields = ['updated_at', 'state', 'created_at', 'payer'] # , 'detail_id']
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
with atomic():
|
with atomic():
|
||||||
|
|
@ -78,15 +78,15 @@ class WithdrawSerializer(serializers.ModelSerializer):
|
||||||
'uuid',
|
'uuid',
|
||||||
'payer',
|
'payer',
|
||||||
'payee',
|
'payee',
|
||||||
"create",
|
"created_at",
|
||||||
"state",
|
"state",
|
||||||
'amount',
|
'amount',
|
||||||
'delay',
|
'delay',
|
||||||
'last_update',
|
'updated_at',
|
||||||
# 'detail_id',
|
# 'detail_id',
|
||||||
)
|
)
|
||||||
|
|
||||||
read_only_fields = ['last_update', 'state', 'create', 'payee'] # , 'detail_id']
|
read_only_fields = ['updated_at', 'state', 'created_at', 'payee'] # , 'detail_id']
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
with atomic():
|
with atomic():
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,9 @@ def get_application(request):
|
||||||
|
|
||||||
|
|
||||||
class WalletBalanceView(generics.RetrieveAPIView):
|
class WalletBalanceView(generics.RetrieveAPIView):
|
||||||
|
'''
|
||||||
|
API for getting authenticated user balance.
|
||||||
|
'''
|
||||||
permission_classes = [permissions.IsAuthenticated, IsAuthenticatedOrTokenHasScope]
|
permission_classes = [permissions.IsAuthenticated, IsAuthenticatedOrTokenHasScope]
|
||||||
serializer_class = WalletSerializer
|
serializer_class = WalletSerializer
|
||||||
required_scopes = ['wallet.wallet:get_balance']
|
required_scopes = ['wallet.wallet:get_balance']
|
||||||
|
|
@ -55,10 +58,17 @@ class WalletBalanceView(generics.RetrieveAPIView):
|
||||||
|
|
||||||
# this is application create invoice view
|
# this is application create invoice view
|
||||||
class TransactionList(generics.ListCreateAPIView):
|
class TransactionList(generics.ListCreateAPIView):
|
||||||
|
'''
|
||||||
|
GET: API for getting list of transactions for authenticated user
|
||||||
|
POST: API for creating transaction for transferring funds from authenticated user to application owner. (deprecated)
|
||||||
|
args:
|
||||||
|
- user: authenticated user
|
||||||
|
|
||||||
|
'''
|
||||||
permission_classes = [IsAuthenticatedOrTokenMatchesOASRequirements]
|
permission_classes = [IsAuthenticatedOrTokenMatchesOASRequirements]
|
||||||
serializer_class = TransactionSerializer
|
serializer_class = TransactionSerializer
|
||||||
required_alternate_scopes = {
|
required_alternate_scopes = {
|
||||||
"GET": [['wallet.transaction:list']],
|
"GET": [['wallet.user:transaction_list']],
|
||||||
"POST": [['wallet.invoice:create']],
|
"POST": [['wallet.invoice:create']],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,7 +78,7 @@ class TransactionList(generics.ListCreateAPIView):
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
def perform_create(self, serializer):
|
||||||
payer = self.request.user
|
payer = self.request.user
|
||||||
payee = self.request.auth.client_owner
|
payee = self.request.auth.application_owner
|
||||||
serializer.save(payer=payer, payee=payee, application=get_application(self.request))
|
serializer.save(payer=payer, payee=payee, application=get_application(self.request))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue