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',
|
||||
'corsheaders',
|
||||
'apps.users',
|
||||
'apps.transactions',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
|
@ -78,9 +79,9 @@ OAUTH2_PROVIDER = {
|
|||
# Tell dot-restrict-scopes which scopes backend it is wrapping
|
||||
# NOTE: oauth2_provider.scopes.SettingsScopes is the default, so this is not
|
||||
# strictly necessary if you want to use scopes from settings
|
||||
DOT_RESTRICT_SCOPES = {
|
||||
'WRAPPED_SCOPES_BACKEND_CLASS': 'oauth2_provider.scopes.SettingsScopes',
|
||||
}
|
||||
# DOT_RESTRICT_SCOPES = {
|
||||
# 'WRAPPED_SCOPES_BACKEND_CLASS': 'oauth2_provider.scopes.SettingsScopes',
|
||||
# }
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||
|
|
|
|||
|
|
@ -2,4 +2,4 @@ from django.apps import AppConfig
|
|||
|
||||
|
||||
class UsersConfig(AppConfig):
|
||||
name = 'users'
|
||||
name = 'apps.users'
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
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 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):
|
||||
|
|
@ -24,7 +24,7 @@ class UserManager(BaseUserManager):
|
|||
# TODO: validate phone number
|
||||
user = self.model(phone_number=phone_number, username=username, email=email, **extra_fields)
|
||||
user.set_password(password)
|
||||
user.set_otp(with_save=False)
|
||||
user.set_otp()
|
||||
user.set_code()
|
||||
user.date_joined = timezone.now()
|
||||
user.save(using=self._db)
|
||||
|
|
@ -36,7 +36,6 @@ class UserManager(BaseUserManager):
|
|||
extra_fields.setdefault('is_staff', False)
|
||||
extra_fields.setdefault('is_superuser', False)
|
||||
|
||||
# TODO create OTP
|
||||
return self._create_user(phone_number=phone_number,
|
||||
username=username,
|
||||
email=email,
|
||||
|
|
@ -102,16 +101,15 @@ class User(AbstractUser):
|
|||
|
||||
objects = UserManager()
|
||||
|
||||
def set_otp(self, with_save=True):
|
||||
def set_otp(self):
|
||||
self.otp = '12345'
|
||||
# if self.phone_number in DEVELOPMENT_PHONE_NUMBERS:
|
||||
# self.otp = '12345'
|
||||
# else:
|
||||
# self.otp = ''.join(random.choice('0123456789') for _ in range(5))
|
||||
if self.phone_number in DEVELOPMENT_PHONE_NUMBERS:
|
||||
self.otp = '12345'
|
||||
else:
|
||||
self.otp = ''.join(random.choice('0123456789') for _ in range(5))
|
||||
|
||||
self.otp_expire = timezone.now() + timedelta(minutes=5)
|
||||
self.otp_try = 0
|
||||
if with_save:
|
||||
self.save()
|
||||
|
||||
def set_code(self):
|
||||
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 apps.users.models import User
|
||||
from django.utils import timezone
|
||||
|
||||
# public data
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
|
||||
class PublicUserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
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)
|
||||
|
||||
if not user.otp_is_valid():
|
||||
user.set_otp(with_save=True)
|
||||
user.set_otp()
|
||||
user.save()
|
||||
|
||||
user.send_otp()
|
||||
|
||||
|
|
|
|||
|
|
@ -4,20 +4,20 @@ from rest_framework import generics, permissions, status
|
|||
from rest_framework.response import Response
|
||||
|
||||
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):
|
||||
permission_classes = [permissions.IsAuthenticated, IsAuthenticatedOrTokenHasScope]
|
||||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
||||
serializer_class = PublicUserSerializer
|
||||
required_scopes = []
|
||||
|
||||
|
||||
class UserDetailView(generics.RetrieveAPIView):
|
||||
permission_classes = [permissions.IsAuthenticated, IsAuthenticatedOrTokenHasScope]
|
||||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
||||
serializer_class = PublicUserSerializer
|
||||
lookup_field = 'code'
|
||||
required_scopes = []
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +1,3 @@
|
|||
#!/usr/bin/env bash
|
||||
# Deployment script
|
||||
sudo apt-get install libmariadbclient-dev
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue