Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/transaction state #29

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
coverage
node_modules
npm-debug.log
env/*
19 changes: 18 additions & 1 deletion platalbank_core/tests/test_transactions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from platalbank_core.models import Event, Transaction, Account

from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase

def reload(obj):
Expand Down Expand Up @@ -40,7 +42,22 @@ def test_not_duplicate_transfer(self):
debited_account=self.accA,
credited_account=self.accB,
event=self.event
).save()
)
self.accA, self.accB = reload(self.accA), reload(self.accB)
self.assertEqual(self.accA.balance, 3700)
self.assertEqual(self.accB.balance, 1837)

def test_transaction_setState(self):
Transaction.objects.create(
amount=500,
label='Test',
debited_account=self.accA,
credited_account=self.accB,
event=self.event
)
self.assertEqual(Transaction.objects.get(id=1).state, 'P')
url = reverse('transaction-setState',args=[1])
data = {'state': 'C'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(Transaction.objects.get(id=1).state, 'C')
31 changes: 31 additions & 0 deletions platalbank_core/views/transaction.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
from rest_framework.response import Response
from rest_framework import viewsets
from rest_framework import exceptions
from rest_framework.decorators import detail_route
from django.http import HttpResponse

from platalbank_core.models import Transaction
from platalbank_core.serializers import TransactionSerializer

class TransactionViewSet(viewsets.ModelViewSet):
queryset = Transaction.objects.all()
serializer_class = TransactionSerializer

@detail_route(methods=['post'])
def setState(self, request, pk=None):

try :
transaction = Transaction.objects.get(id=pk)
except Transaction.DoesNotExist:
return HttpResponse(status=404)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Même remarque ici : HttpResponse est avantageusement remplacé par Response :)

Et, pour en-dessous, les Response({'status': 'State modified'}) peuvent être remplacées par juste des Response ; vu que le message "tout s'est bien passé" est déjà transmis par le code HTTP 200 :)


state = request.data.get('state')

#Une fois les permissions implementees, ce type de changement d'etat demandera des permissions Khube ou User
if (state in [Transaction.AUTHORIZED , Transaction.REJECTED]):
transaction.state = state
transaction.save()

return Response({'status':'State modified'})

#Ce type de changement d'etat demandera des permissions seller
elif (state in [Transaction.ABORTED,Transaction.COMPLETED]):
transaction.state = state
transaction.save()

return Response({'status':'State modified'})

else :
raise exceptions.PermissionDenied