From ba6507c8f92cfb2bea317e6a646561874203ccb4 Mon Sep 17 00:00:00 2001 From: mahdavi Date: Thu, 8 Sep 2022 17:21:28 +0430 Subject: [PATCH] application instead of application client id --- apps/users/views.py | 25 +++++++++---- apps/wallet/admin.py | 4 +-- ...nsaction_application_client_id_and_more.py | 25 +++++++++++++ apps/wallet/models.py | 11 +++--- apps/wallet/serializers.py | 11 ++---- apps/wallet/views.py | 35 ++++++++----------- templates/{registration => users}/login.html | 0 .../profile_detail.html} | 0 templates/users/profile_update.html | 9 +++++ 9 files changed, 78 insertions(+), 42 deletions(-) create mode 100644 apps/wallet/migrations/0002_remove_transaction_application_client_id_and_more.py rename templates/{registration => users}/login.html (100%) rename templates/{registration/profile.html => users/profile_detail.html} (100%) create mode 100644 templates/users/profile_update.html diff --git a/apps/users/views.py b/apps/users/views.py index 4ef4ce4..8d6e6a0 100644 --- a/apps/users/views.py +++ b/apps/users/views.py @@ -4,7 +4,7 @@ from django.contrib.auth.views import LoginView from django.shortcuts import render from django.utils import timezone from django.utils.decorators import method_decorator -from django.views.generic import DetailView +from django.views.generic import DetailView, UpdateView from oauth2_provider.contrib.rest_framework import IsAuthenticatedOrTokenHasScope from rest_framework import generics, status, permissions from rest_framework.response import Response @@ -97,18 +97,29 @@ class ChangePasswordView(generics.UpdateAPIView): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) +class OTPLoginView(LoginView): + template_name = 'users/login.html' + """ + Display the login form and handle the login action. + """ + form_class = OTPAuthenticationForm + + @method_decorator(login_required, name='dispatch') class ProfileView(DetailView): model = User - template_name = 'registration/profile.html' + template_name = 'users/profile_detail.html' pk_url_kwarg = 'uuid' def get_object(self, queryset=None): return self.request.user -class OTPLoginView(LoginView): - """ - Display the login form and handle the login action. - """ - form_class = OTPAuthenticationForm +@method_decorator(login_required, name='dispatch') +class ProfileUpdate(UpdateView): + model = User + template_name = 'users/profile_update.html' + pk_url_kwarg = 'uuid' + + def get_object(self, queryset=None): + return self.request.user diff --git a/apps/wallet/admin.py b/apps/wallet/admin.py index 245cc85..1c59e67 100755 --- a/apps/wallet/admin.py +++ b/apps/wallet/admin.py @@ -13,9 +13,9 @@ class TransactionAdmin(admin.ModelAdmin): 'state', 'delay', 'detail_id', - 'application_client_id' + 'application' ] - readonly_fields = ['uuid', 'last_update', 'create', 'detail_id', 'application_client_id'] + readonly_fields = ['uuid', 'last_update', 'create', 'detail_id', 'application'] admin.site.register(Transaction, TransactionAdmin) diff --git a/apps/wallet/migrations/0002_remove_transaction_application_client_id_and_more.py b/apps/wallet/migrations/0002_remove_transaction_application_client_id_and_more.py new file mode 100644 index 0000000..13fb061 --- /dev/null +++ b/apps/wallet/migrations/0002_remove_transaction_application_client_id_and_more.py @@ -0,0 +1,25 @@ +# Generated by Django 4.1 on 2022-09-08 12:16 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.OAUTH2_PROVIDER_APPLICATION_MODEL), + ('wallet', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='transaction', + name='application_client_id', + ), + migrations.AddField( + model_name='transaction', + name='application', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='transactions', to=settings.OAUTH2_PROVIDER_APPLICATION_MODEL), + ), + ] diff --git a/apps/wallet/models.py b/apps/wallet/models.py index cffd38b..54ad939 100755 --- a/apps/wallet/models.py +++ b/apps/wallet/models.py @@ -7,7 +7,7 @@ from django.db.models import F from django.utils.translation import gettext_lazy as _ from model_utils.choices import Choices from rest_framework.exceptions import APIException, ValidationError - +from django.conf import settings from .constans import STATE_CHOICES from apps.users.models import User @@ -20,7 +20,7 @@ class TransactionManager(models.Manager): payer: User = None, delay=0, # detail_id=None, - application_client_id=None, + application=None, # state=None, uuid=None ): @@ -40,7 +40,7 @@ class TransactionManager(models.Manager): amount=amount, delay=delay, # detail_id=detail_id, - application_client_id=application_client_id + application=application ) # if state: # transaction.state = state @@ -60,13 +60,14 @@ class Transaction(models.Model): state = models.IntegerField(choices=STATE_CHOICES, default=STATE_CHOICES.created) create = models.DateTimeField(_('create'), blank=True, null=True, auto_now=True) last_update = models.DateTimeField(_('last update'), blank=True, null=True, auto_now=True) - application_client_id = models.CharField(max_length=100, db_index=True, null=True) + application = models.ForeignKey(settings.OAUTH2_PROVIDER_APPLICATION_MODEL, on_delete=models.PROTECT, + related_name='transactions', null=True) detail_id = models.IntegerField(null=True, blank=True) # TODO: or better detail url objects = TransactionManager() # class Meta: - # unique_together = ('application_client_id', 'detail_id',) + # unique_together = ('application', 'detail_id',) def pay(self, payer=None): if not self.payer and not payer: diff --git a/apps/wallet/serializers.py b/apps/wallet/serializers.py index 1b6fb35..715b79f 100755 --- a/apps/wallet/serializers.py +++ b/apps/wallet/serializers.py @@ -11,8 +11,6 @@ class TransactionSerializer(serializers.ModelSerializer): payer = PublicUserSerializer(required=False) payee = PublicUserSerializer(required=False, read_only=True) - # application_client_id = serializers.IntegerField(required=False, read_only=True) - class Meta: model = Transaction fields = ('uuid', @@ -24,10 +22,9 @@ class TransactionSerializer(serializers.ModelSerializer): 'delay', 'last_update', # 'detail_id', - # 'application_client_id' ) - read_only_fields = ['uuid', 'last_update', 'state', 'create'] # , 'detail_id'] #, 'application_client_id'] + read_only_fields = ['uuid', 'last_update', 'state', 'create'] # , 'detail_id'] def create(self, validated_data): # payer = validated_data.get('payer') @@ -59,10 +56,9 @@ class DepositSerializer(serializers.ModelSerializer): 'delay', 'last_update', # 'detail_id', - # 'application_client_id' ) - read_only_fields = ['last_update', 'state', 'create', 'payer'] # , 'detail_id'] #, 'application_client_id'] + read_only_fields = ['last_update', 'state', 'create', 'payer'] # , 'detail_id'] def create(self, validated_data): with atomic(): @@ -90,10 +86,9 @@ class WithdrawSerializer(serializers.ModelSerializer): 'delay', 'last_update', # 'detail_id', - # 'application_client_id' ) - read_only_fields = ['last_update', 'state', 'create', 'payee'] # , 'detail_id'] #, 'application_client_id'] + read_only_fields = ['last_update', 'state', 'create', 'payee'] # , 'detail_id'] def create(self, validated_data): with atomic(): diff --git a/apps/wallet/views.py b/apps/wallet/views.py index 3aa6321..0c4445d 100755 --- a/apps/wallet/views.py +++ b/apps/wallet/views.py @@ -18,17 +18,12 @@ from .forms import TransactionActionForm from django.utils.translation import gettext_lazy as _ - -def get_application_client_id(request): +def get_application(request): try: application = request.auth.application - if application: - application_client_id = application.client_id - else: - application_client_id = request.auth.detail.get('client_id') except: - application_client_id = None - return application_client_id + application = None + return application # TODO: this is user invoice create view # class TransactionList(generics.ListCreateAPIView): @@ -45,7 +40,7 @@ def get_application_client_id(request): # # def perform_create(self, serializer): # user = self.request.user -# serializer.save(payee=user, application_client_id=get_application_client_id(self.request)) +# serializer.save(payee=user, application=get_application(self.request)) # this is application create invoice view @@ -64,7 +59,7 @@ class TransactionList(generics.ListCreateAPIView): def perform_create(self, serializer): payer = self.request.user payee = self.request.auth.application.user - serializer.save(payer=payer, payee=payee, application_client_id=get_application_client_id(self.request)) + serializer.save(payer=payer, payee=payee, application=get_application(self.request)) class TransactionDetail(generics.RetrieveAPIView): @@ -118,12 +113,12 @@ class DepositSubmit(generics.CreateAPIView): required_scopes = ['wallet.deposit:submit'] def get_queryset(self): - application_client_id = get_application_client_id(self.request) - return Transaction.objects.filter(application_client_id=application_client_id).all() + application = get_application(self.request) + return Transaction.objects.filter(application=application).all() def perform_create(self, serializer): payer = self.request.auth.application.user - serializer.save(payer=payer, application_client_id=get_application_client_id(self.request)) + serializer.save(payer=payer, application=get_application(self.request)) class DepositVerify(generics.RetrieveAPIView): @@ -133,8 +128,8 @@ class DepositVerify(generics.RetrieveAPIView): required_scopes = ['wallet.deposit:verify'] def get_queryset(self): - application_client_id = get_application_client_id(self.request) - return Transaction.objects.filter(application_client_id=application_client_id).all() + application = get_application(self.request) + return Transaction.objects.filter(application=application).all() def retrieve(self, request, *args, **kwargs): instance = self.get_object() @@ -151,12 +146,12 @@ class WithdrawSubmit(generics.CreateAPIView): required_scopes = ['wallet.withdraw:submit'] def get_queryset(self): - application_client_id = get_application_client_id(self.request) - return Transaction.objects.filter(application_client_id=application_client_id).all() + application = get_application(self.request) + return Transaction.objects.filter(application=application).all() def perform_create(self, serializer): payee = self.request.auth.application.user - serializer.save(payee=payee, application_client_id=get_application_client_id(self.request)) + serializer.save(payee=payee, application=get_application(self.request)) class WithdrawVerify(generics.RetrieveAPIView): @@ -166,8 +161,8 @@ class WithdrawVerify(generics.RetrieveAPIView): required_scopes = ['wallet.withdraw:verify'] def get_queryset(self): - application_client_id = get_application_client_id(self.request) - return Transaction.objects.filter(application_client_id=application_client_id).all() + application = get_application(self.request) + return Transaction.objects.filter(application=application).all() def retrieve(self, request, *args, **kwargs): instance = self.get_object() diff --git a/templates/registration/login.html b/templates/users/login.html similarity index 100% rename from templates/registration/login.html rename to templates/users/login.html diff --git a/templates/registration/profile.html b/templates/users/profile_detail.html similarity index 100% rename from templates/registration/profile.html rename to templates/users/profile_detail.html diff --git a/templates/users/profile_update.html b/templates/users/profile_update.html new file mode 100644 index 0000000..0af303c --- /dev/null +++ b/templates/users/profile_update.html @@ -0,0 +1,9 @@ +{% extends "base_generic.html" %} + +{% block content %} + +

Homepage

+

Hello {{ user.username }}!

+

your phone number {{ user.phone_number }}!

+ +{% endblock %} \ No newline at end of file