diff --git a/apps/wallet/tests/application.py b/apps/wallet/tests/application.py index 201b188..dcb35a4 100755 --- a/apps/wallet/tests/application.py +++ b/apps/wallet/tests/application.py @@ -94,8 +94,11 @@ class ApplicationApiFlowsTests(APITestCase): def test_authentication_allow(self): auth = self._create_authorization_header(self.application_access_token.token) - response = self.client.get(reverse('wallet:user_wallet_balance_api'), HTTP_AUTHORIZATION=auth) - self.assertContains(response, 'balance') + wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] + response = self.client.get(reverse('wallet:user_wallet_account_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + self.assertGreater(response.json()['count'], 1) + from_wallet = response.json()['results'][0] + self.assertEqual(from_wallet['balance'], 300) def test_authentication_expired_token(self): @@ -112,7 +115,7 @@ class ApplicationApiFlowsTests(APITestCase): }, ) auth = self._create_authorization_header(expired_access_token.token) - response = self.client.get(reverse('wallet:user_wallet_balance_api'), HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:user_wallet_account_api'), HTTP_AUTHORIZATION=auth) self.assertEqual(response.status_code, 401) # def test_authentication_disallow(self): @@ -125,14 +128,14 @@ class ApplicationApiFlowsTests(APITestCase): # Checking wallets initials values(application balance = 300 and payee balance = zero) wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] - response = self.client.get(reverse('wallet:application_account_balance_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_account_list_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) from_wallet = response.json()['results'][0] self.assertEqual(from_wallet['balance'], 300) params = { 'owner': self.payee_uuid, } - response = self.client.get(reverse('wallet:application_user_account_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_user_account_list_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) to_wallet = response.json()['results'][0] self.assertEqual(to_wallet['balance'], 0) @@ -160,7 +163,7 @@ class ApplicationApiFlowsTests(APITestCase): self.assertEqual(response.json()['state'], 5) # Desired results - response = self.client.get(reverse('wallet:application_account_balance_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_account_list_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) from_wallet = response.json()['results'][0] self.assertEqual(from_wallet['balance'], 200) @@ -168,7 +171,7 @@ class ApplicationApiFlowsTests(APITestCase): params = { 'owner': self.payee_uuid, } - response = self.client.get(reverse('wallet:application_user_account_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_user_account_list_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) to_wallet = response.json()['results'][0] self.assertEqual(to_wallet['balance'], 100) @@ -179,7 +182,7 @@ class ApplicationApiFlowsTests(APITestCase): # Checking wallets initials values(application balance = 300 and payee balance = zero) wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] - response = self.client.get(reverse('wallet:application_account_balance_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_account_list_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) from_wallet = response.json()['results'][0] self.assertEqual(from_wallet['balance'], 300) @@ -187,7 +190,7 @@ class ApplicationApiFlowsTests(APITestCase): 'owner': self.payee_uuid, } - response = self.client.get(reverse('wallet:application_user_account_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_user_account_list_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) to_wallet = response.json()['results'][0] self.assertEqual(to_wallet['balance'], 0) @@ -212,14 +215,14 @@ class ApplicationApiFlowsTests(APITestCase): # Checking wallets initials values(application balance = 300 and payee balance = zero) wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] - response = self.client.get(reverse('wallet:application_account_balance_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_account_list_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) from_wallet = response.json()['results'][0] self.assertEqual(from_wallet['balance'], 300) params = { 'owner': self.payee_uuid, } - response = self.client.get(reverse('wallet:application_user_account_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_user_account_list_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) to_wallet = response.json()['results'][0] self.assertEqual(to_wallet['balance'], 0) @@ -277,14 +280,14 @@ class ApplicationApiFlowsTests(APITestCase): # Checking wallets initials values(application balance = 300 and payee balance = zero) wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] - response = self.client.get(reverse('wallet:application_account_balance_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_account_list_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) from_wallet = response.json()['results'][0] self.assertEqual(from_wallet['balance'], 300) params = { 'owner': self.payee_uuid, } - response = self.client.get(reverse('wallet:application_user_account_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_user_account_list_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) to_wallet = response.json()['results'][0] self.assertEqual(to_wallet['balance'], 0) @@ -310,14 +313,14 @@ class ApplicationApiFlowsTests(APITestCase): # Checking wallets initials values(application balance = 300 and payee balance = zero) wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] - response = self.client.get(reverse('wallet:application_account_balance_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_account_list_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) from_wallet = response.json()['results'][0] self.assertEqual(from_wallet['balance'], 300) params = { 'owner': self.payer_uuid, } - response = self.client.get(reverse('wallet:application_user_account_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_user_account_list_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) to_wallet = response.json()['results'][0] self.assertEqual(to_wallet['balance'], 300) @@ -348,14 +351,14 @@ class ApplicationApiFlowsTests(APITestCase): # Desired results wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] - response = self.client.get(reverse('wallet:application_account_balance_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_account_list_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) from_wallet = response.json()['results'][0] self.assertEqual(from_wallet['balance'], 400) params = { 'owner': self.payer_uuid, } - response = self.client.get(reverse('wallet:application_user_account_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_user_account_list_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) to_wallet = response.json()['results'][0] self.assertEqual(to_wallet['balance'], 200) @@ -401,14 +404,14 @@ class ApplicationApiFlowsTests(APITestCase): } # Checking wallets initials values(application balance = 300 and payee balance = zero) wallets = [self.wallet_rial_uuid.hex, self.wallet_reward_uuid.hex] - response = self.client.get(reverse('wallet:application_account_balance_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_account_list_api'), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) from_wallet = response.json()['results'][0] self.assertEqual(from_wallet['balance'], 300) params = { 'owner': self.payer_uuid, } - response = self.client.get(reverse('wallet:application_user_account_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) + response = self.client.get(reverse('wallet:application_user_account_list_api', kwargs=params), query_params={'wallet': ','.join(wallets)}, HTTP_AUTHORIZATION=auth) self.assertGreater(response.json()['count'], 1) to_wallet = response.json()['results'][0] self.assertEqual(to_wallet['balance'], 300) diff --git a/apps/wallet/urls.py b/apps/wallet/urls.py index 0a29b08..760e069 100644 --- a/apps/wallet/urls.py +++ b/apps/wallet/urls.py @@ -6,25 +6,21 @@ from .views import user as user_views app_name = "wallet" user_urlpatterns = [ - path('transactions/', user_views.TransactionListView.as_view(), name='user_transaction_list_page'), - path('transactions/', user_views.TransactionDetailView.as_view(), name='user_transaction_detail_page'), - # path('api/wallet/balance', user_views.WalletMyAccountBalanceView.as_view(), name='user_wallet_balance_api'), - path('api/transactions/', user_views.TransactionListAPIView.as_view(), name='transaction_list_api'), + # path('transactions/', user_views.TransactionListView.as_view(), name='user_transaction_list_page'), + # path('transactions/', user_views.TransactionDetailView.as_view(), name='user_transaction_detail_page'), + path('api/wallets/', user_views.WalletAccountListAPIView.as_view(), name='user_wallet_account_api'), + path('api/transactions/', user_views.TransactionListAPIView.as_view(), name='user_transaction_list_api'), - # path('api/user//balance', user_views.WalletMyAccountBalanceView.as_view(), name='user_wallet_balance_api'), - path('api/user//transactions/', user_views.WalletTransactionListView.as_view(), name='transaction_list_api'), + # path('api//balance', user_views.WalletMyAccountBalanceView.as_view(), name='user_wallet_balance_api'), + # path('api//transactions/', user_views.WalletTransactionListView.as_view(), name='transaction_list_api'), ] application_urlpatterns = [ - path('api/application/accounts/', application_views.AccountListAPIView.as_view(), name='application_account_balance_api'), - path('api/application/user//accounts/', application_views.UserAccountListAPIView.as_view(), name='application_user_account_api'), + path('api/application/wallets/', application_views.AccountListAPIView.as_view(), name='application_account_list_api'), + path('api/application/wallets/user//', application_views.UserAccountListAPIView.as_view(), name='application_user_account_list_api'), - # path('api/application/', application_views.TransactionListAPIView.as_view(), name='application_transaction_list_api'), - # path('api/application//', application_views.TransactionDetailAPIView.as_view(), name='application_transaction_detail'), - - - # path('api/application//wallet', application_views.BalanceAPIView.as_view(), name='application_wallet_balance_api'), - # path('api/application//user//wallet', application_views.UserBalanceAPIView.as_view(), name='application_user_wallet_balance_api'), + path('api/application/transactions/', application_views.TransactionListAPIView.as_view(), name='application_transaction_list_api'), + path('api/application/transactions//', application_views.TransactionDetailAPIView.as_view(), name='application_transaction_detail_api'), path('api/application//deposit/', application_views.DepositSubmitAPIView.as_view(), name='application_deposit_submit_api'), path('api/application//deposit//verify', application_views.DepositVerifyAPIView.as_view(), name='application_deposit_verify_api'), @@ -32,8 +28,8 @@ application_urlpatterns = [ path('api/application//withdraw/', application_views.WithdrawSubmitAPIView.as_view(), name='application_withdraw_submit_api'), path('api/application//withdraw//verify', application_views.WithdrawVerifyAPIView.as_view(), name='application_withdraw_verify_api'), - path('api/application//', application_views.WalletTransactionListAPIView.as_view(), name='application_transaction_list_api'), - path('api/application///', application_views.WalletTransactionDetailAPIView.as_view(), name='application_transaction_detail'), + # path('api/application/wallets//', application_views.WalletTransactionListAPIView.as_view(), name='application_transaction_list_api'), + # path('api/application/wallets///', application_views.WalletTransactionDetailAPIView.as_view(), name='application_transaction_detail'), ] diff --git a/apps/wallet/views/application.py b/apps/wallet/views/application.py index bcce293..ad26522 100755 --- a/apps/wallet/views/application.py +++ b/apps/wallet/views/application.py @@ -42,11 +42,7 @@ class AccountListAPIView(generics.ListAPIView): def get_object(self): wallet_uuid = self.kwargs.get('wallet') - if wallet_uuid: - wallet = get_object_or_404(Wallet.objects.all(), uuid=wallet_uuid) - else: - wallet = None - + wallet = get_object_or_404(Wallet.objects.all(), uuid=wallet_uuid) application = get_application(self.request) account, created = Account.objects.get_or_create(owner_uuid=application.uuid, owner_type=TypeChoices.APPLICATION, wallet=wallet) diff --git a/apps/wallet/views/user.py b/apps/wallet/views/user.py index 85e5b98..813b5f5 100755 --- a/apps/wallet/views/user.py +++ b/apps/wallet/views/user.py @@ -17,10 +17,10 @@ from apps.wallet.models import Transaction, Wallet, Account from apps.wallet.serializers import TransactionSerializer, DepositSerializer, WithdrawSerializer, \ AccountSerializer from ..constans import StateChoices, TypeChoices -from ..filters import UserTransactionFilter -from ..forms import TransactionActionForm +from ..filters import AccountFilter, UserTransactionFilter +# from ..forms import TransactionActionForm -from django.utils.translation import gettext_lazy as _ +# from django.utils.translation import gettext_lazy as _ def get_application(request): @@ -186,65 +186,63 @@ class WithdrawVerify(generics.RetrieveAPIView): return Response(serializer.data) -@method_decorator(login_required, name='dispatch') -class TransactionListView(ListView): - model = Transaction - template_name = 'wallet/transaction_list.html' - - def get_queryset(self): - user = self.request.user - qs = super().get_queryset() - return qs.filter(Q(payee_id=user.pk) | Q(payer_id=user.pk)).all() +# @method_decorator(login_required, name='dispatch') +# class TransactionListView(ListView): +# model = Transaction +# template_name = 'wallet/transaction_list.html' +# +# def get_queryset(self): +# user = self.request.user +# qs = super().get_queryset() +# return qs.filter(Q(payee_id=user.pk) | Q(payer_id=user.pk)).all() -@method_decorator(login_required, name='dispatch') -class TransactionDetailView(UpdateView): - model = Transaction - form_class = TransactionActionForm - template_name = 'wallet/transaction_detail.html' - - def get_context_data(self, **kwargs): - context_data = super().get_context_data(**kwargs) - context_data['StateChoices'] = StateChoices - return context_data - - def get_queryset(self): - user = self.request.user - return Transaction.objects.filter(Q(payee=user) | Q(payer=user) | Q(payer__isnull=True)).all() - - def get_success_url(self): - return reverse('wallet:transaction_detail', kwargs={'pk': self.get_object().pk}) - - def form_valid(self, form): - transaction: Transaction = form.instance - if 'pay' in self.request.POST: - result = transaction.pay(payer=self.request.user) - messages.error(self.request, _('success.')) - if 'receipt' in self.request.POST: - result = transaction.receipt() - messages.error(self.request, _('success.')) - return super().form_valid(form) +# @method_decorator(login_required, name='dispatch') +# class TransactionDetailView(UpdateView): +# model = Transaction +# form_class = TransactionActionForm +# template_name = 'wallet/transaction_detail.html' +# +# def get_context_data(self, **kwargs): +# context_data = super().get_context_data(**kwargs) +# context_data['StateChoices'] = StateChoices +# return context_data +# +# def get_queryset(self): +# user = self.request.user +# return Transaction.objects.filter(Q(payee=user) | Q(payer=user) | Q(payer__isnull=True)).all() +# +# def get_success_url(self): +# return reverse('wallet:transaction_detail', kwargs={'pk': self.get_object().pk}) +# +# def form_valid(self, form): +# transaction: Transaction = form.instance +# if 'pay' in self.request.POST: +# result = transaction.pay(payer=self.request.user) +# messages.error(self.request, _('success.')) +# if 'receipt' in self.request.POST: +# result = transaction.receipt() +# messages.error(self.request, _('success.')) +# return super().form_valid(form) -class WalletMyAccountBalanceView(generics.RetrieveAPIView): +class WalletAccountListAPIView(generics.ListAPIView): ''' API for getting authenticated user balance. ''' permission_classes = [IsAuthenticatedOrTokenHasScope] serializer_class = AccountSerializer + filter_backends = (DjangoFilterBackend,) + filterset_class = AccountFilter required_scopes = ['wallet.wallet:get_balance'] + queryset = Account.objects.all() def get_object(self): user = self.request.user - wallet_uuid = self.kwargs.get('wallet_uuid') - if wallet_uuid: - wallet = get_object_or_404(Wallet.objects.all(), uuid=wallet_uuid) - else: - wallet = None - - wallet, created = Account.objects.get_or_create(owner_uuid=user.uuid, owner_type=TypeChoices.USER, - wallet=wallet) + wallet_uuid = self.kwargs.get('wallet') + wallet = get_object_or_404(Wallet.objects.all(), uuid=wallet_uuid) + wallet, created = Account.objects.get_or_create(owner_uuid=user.uuid, owner_type=TypeChoices.USER, wallet=wallet) return wallet @@ -271,8 +269,8 @@ class TransactionListAPIView(generics.ListAPIView): # TODO: redundant def get_queryset(self): user = self.request.user - return Transaction.objects.filter(Q(payee_wallet__owner_uuid=user.uuid) | - Q(payer_wallet__owner_uuid=user.uuid)).all() + return Transaction.objects.filter(Q(payee_account__owner_uuid=user.uuid) | + Q(payer_account__owner_uuid=user.uuid)).all() class WalletTransactionListView(generics.ListAPIView): @@ -295,15 +293,8 @@ class WalletTransactionListView(generics.ListAPIView): def get_queryset(self): user = self.request.user - wallet_uuid = self.kwargs.get('wallet_uuid') - if wallet_uuid: - wallet = get_object_or_404(Wallet.objects.all(), uuid=wallet_uuid) - else: - wallet = None - - wallet, created = Account.objects.get_or_create(owner_uuid=user.uuid, owner_type=TypeChoices.USER, - wallet=wallet) - - return Transaction.objects.filter(Q(payee_wallet=wallet) | - Q(payer_wallet=wallet)).all() + wallet_uuid = self.kwargs.get('wallet') + wallet = get_object_or_404(Wallet.objects.all(), uuid=wallet_uuid) + account, created = Account.objects.get_or_create(owner_uuid=user.uuid, owner_type=TypeChoices.USER, wallet=wallet) + return Transaction.objects.filter(Q(payee_account=account) | Q(payer_account=account)).all()