better naming in serializer
This commit is contained in:
parent
c5add58ee9
commit
c8276e20c3
7 changed files with 24 additions and 23 deletions
|
|
@ -44,6 +44,7 @@ INSTALLED_APPS = [
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
'corsheaders',
|
'corsheaders',
|
||||||
'apps.users',
|
'apps.users',
|
||||||
|
'apps.transactions',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|
@ -78,9 +79,9 @@ OAUTH2_PROVIDER = {
|
||||||
# Tell dot-restrict-scopes which scopes backend it is wrapping
|
# Tell dot-restrict-scopes which scopes backend it is wrapping
|
||||||
# NOTE: oauth2_provider.scopes.SettingsScopes is the default, so this is not
|
# NOTE: oauth2_provider.scopes.SettingsScopes is the default, so this is not
|
||||||
# strictly necessary if you want to use scopes from settings
|
# strictly necessary if you want to use scopes from settings
|
||||||
DOT_RESTRICT_SCOPES = {
|
# DOT_RESTRICT_SCOPES = {
|
||||||
'WRAPPED_SCOPES_BACKEND_CLASS': 'oauth2_provider.scopes.SettingsScopes',
|
# 'WRAPPED_SCOPES_BACKEND_CLASS': 'oauth2_provider.scopes.SettingsScopes',
|
||||||
}
|
# }
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_AUTHENTICATION_CLASSES': (
|
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,4 @@ from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
class UsersConfig(AppConfig):
|
class UsersConfig(AppConfig):
|
||||||
name = 'users'
|
name = 'apps.users'
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
MAX_OTP_TRY = 3
|
MAX_OTP_TRY = 3
|
||||||
DEVELOPMENT_PHONE_NUMBERS = ['+989106853582']
|
DEVELOPMENT_PHONE_NUMBERS = ['+989999999999', '+989999999998']
|
||||||
|
|
@ -9,7 +9,7 @@ from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from rest_framework.exceptions import APIException
|
from rest_framework.exceptions import APIException
|
||||||
|
|
||||||
from apps.users.constans import MAX_OTP_TRY
|
from apps.users.constans import MAX_OTP_TRY, DEVELOPMENT_PHONE_NUMBERS
|
||||||
|
|
||||||
|
|
||||||
class UserManager(BaseUserManager):
|
class UserManager(BaseUserManager):
|
||||||
|
|
@ -24,7 +24,7 @@ class UserManager(BaseUserManager):
|
||||||
# TODO: validate phone number
|
# TODO: validate phone number
|
||||||
user = self.model(phone_number=phone_number, username=username, email=email, **extra_fields)
|
user = self.model(phone_number=phone_number, username=username, email=email, **extra_fields)
|
||||||
user.set_password(password)
|
user.set_password(password)
|
||||||
user.set_otp(with_save=False)
|
user.set_otp()
|
||||||
user.set_code()
|
user.set_code()
|
||||||
user.date_joined = timezone.now()
|
user.date_joined = timezone.now()
|
||||||
user.save(using=self._db)
|
user.save(using=self._db)
|
||||||
|
|
@ -36,7 +36,6 @@ class UserManager(BaseUserManager):
|
||||||
extra_fields.setdefault('is_staff', False)
|
extra_fields.setdefault('is_staff', False)
|
||||||
extra_fields.setdefault('is_superuser', False)
|
extra_fields.setdefault('is_superuser', False)
|
||||||
|
|
||||||
# TODO create OTP
|
|
||||||
return self._create_user(phone_number=phone_number,
|
return self._create_user(phone_number=phone_number,
|
||||||
username=username,
|
username=username,
|
||||||
email=email,
|
email=email,
|
||||||
|
|
@ -102,16 +101,15 @@ class User(AbstractUser):
|
||||||
|
|
||||||
objects = UserManager()
|
objects = UserManager()
|
||||||
|
|
||||||
def set_otp(self, with_save=True):
|
def set_otp(self):
|
||||||
self.otp = '12345'
|
self.otp = '12345'
|
||||||
# if self.phone_number in DEVELOPMENT_PHONE_NUMBERS:
|
if self.phone_number in DEVELOPMENT_PHONE_NUMBERS:
|
||||||
# self.otp = '12345'
|
self.otp = '12345'
|
||||||
# else:
|
else:
|
||||||
# self.otp = ''.join(random.choice('0123456789') for _ in range(5))
|
self.otp = ''.join(random.choice('0123456789') for _ in range(5))
|
||||||
|
|
||||||
self.otp_expire = timezone.now() + timedelta(minutes=5)
|
self.otp_expire = timezone.now() + timedelta(minutes=5)
|
||||||
self.otp_try = 0
|
self.otp_try = 0
|
||||||
if with_save:
|
|
||||||
self.save()
|
|
||||||
|
|
||||||
def set_code(self):
|
def set_code(self):
|
||||||
for code in random.sample(range(10000, 100000), 90000):
|
for code in random.sample(range(10000, 100000), 90000):
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
from django.contrib.auth.models import Group
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from apps.users.models import User
|
from apps.users.models import User
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
# public data
|
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
class PublicUserSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ('avatar', 'name', 'username', 'email', "first_name", "last_name", "code")
|
fields = ('avatar', 'name', 'username', 'email', "first_name", "last_name", "code")
|
||||||
|
|
@ -58,7 +57,8 @@ class RequestOTPSerializer(serializers.ModelSerializer):
|
||||||
user = User.objects.create_user(**validated_data)
|
user = User.objects.create_user(**validated_data)
|
||||||
|
|
||||||
if not user.otp_is_valid():
|
if not user.otp_is_valid():
|
||||||
user.set_otp(with_save=True)
|
user.set_otp()
|
||||||
|
user.save()
|
||||||
|
|
||||||
user.send_otp()
|
user.send_otp()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,20 +4,20 @@ from rest_framework import generics, permissions, status
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from apps.users.models import User
|
from apps.users.models import User
|
||||||
from apps.users.serializers import UserSerializer, AccountSerializer, RequestOTPSerializer, ChangePasswordSerializer
|
from apps.users.serializers import PublicUserSerializer, AccountSerializer, RequestOTPSerializer, ChangePasswordSerializer
|
||||||
|
|
||||||
|
|
||||||
class UserListView(generics.ListAPIView):
|
class UserListView(generics.ListAPIView):
|
||||||
permission_classes = [permissions.IsAuthenticated, IsAuthenticatedOrTokenHasScope]
|
permission_classes = [permissions.IsAuthenticated, IsAuthenticatedOrTokenHasScope]
|
||||||
queryset = User.objects.all()
|
queryset = User.objects.all()
|
||||||
serializer_class = UserSerializer
|
serializer_class = PublicUserSerializer
|
||||||
required_scopes = []
|
required_scopes = []
|
||||||
|
|
||||||
|
|
||||||
class UserDetailView(generics.RetrieveAPIView):
|
class UserDetailView(generics.RetrieveAPIView):
|
||||||
permission_classes = [permissions.IsAuthenticated, IsAuthenticatedOrTokenHasScope]
|
permission_classes = [permissions.IsAuthenticated, IsAuthenticatedOrTokenHasScope]
|
||||||
queryset = User.objects.all()
|
queryset = User.objects.all()
|
||||||
serializer_class = UserSerializer
|
serializer_class = PublicUserSerializer
|
||||||
lookup_field = 'code'
|
lookup_field = 'code'
|
||||||
required_scopes = []
|
required_scopes = []
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1,3 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
# Deployment script
|
# Deployment script
|
||||||
|
sudo apt-get install libmariadbclient-dev
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue