gold eggs backend
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s
This commit is contained in:
0
core/apps/accounts/__init__.py
Normal file
0
core/apps/accounts/__init__.py
Normal file
0
core/apps/accounts/admin/__init__.py
Normal file
0
core/apps/accounts/admin/__init__.py
Normal file
6
core/apps/accounts/apps.py
Normal file
6
core/apps/accounts/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "core.apps.accounts"
|
||||
0
core/apps/accounts/migrations/__init__.py
Normal file
0
core/apps/accounts/migrations/__init__.py
Normal file
0
core/apps/accounts/models/__init__.py
Normal file
0
core/apps/accounts/models/__init__.py
Normal file
1
core/apps/accounts/serializers/__init__.py
Normal file
1
core/apps/accounts/serializers/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .check_password import * # noqa
|
||||
6
core/apps/accounts/serializers/check_password.py
Normal file
6
core/apps/accounts/serializers/check_password.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class ChangePasswordSerializer(serializers.Serializer):
|
||||
old_password = serializers.CharField(required=True)
|
||||
new_password = serializers.CharField(required=True)
|
||||
0
core/apps/accounts/test/__init__.py
Normal file
0
core/apps/accounts/test/__init__.py
Normal file
64
core/apps/accounts/urls.py
Executable file
64
core/apps/accounts/urls.py
Executable file
@@ -0,0 +1,64 @@
|
||||
"""
|
||||
Accounts app urls
|
||||
"""
|
||||
|
||||
from django.urls import path
|
||||
from rest_framework_simplejwt import views as jwt_views
|
||||
|
||||
from core.apps.accounts import views
|
||||
from core.http.views.user import UserAvatarUpdateView, UserUpdateView
|
||||
|
||||
urlpatterns = [
|
||||
path(
|
||||
"auth/token/",
|
||||
jwt_views.TokenObtainPairView.as_view(),
|
||||
name="token_obtain_pair",
|
||||
), # Login view # noqa
|
||||
path(
|
||||
"auth/token/refresh/",
|
||||
jwt_views.TokenRefreshView.as_view(),
|
||||
name="token_refresh",
|
||||
), # Refresh token view # noqa
|
||||
path(
|
||||
"auth/token/verify/",
|
||||
jwt_views.TokenVerifyView.as_view(),
|
||||
name="token_verify",
|
||||
), # Verify token # noqa
|
||||
# path(
|
||||
# "auth/register/", views.RegisterView.as_view(), name="register"
|
||||
# ), # Register # noqa
|
||||
path(
|
||||
"auth/confirm/", views.ConfirmView.as_view(), name="confirm"
|
||||
), # Confirm Otp code view # noqa
|
||||
path(
|
||||
"auth/reset/password/",
|
||||
views.ResetPasswordView.as_view(),
|
||||
name="reset-password",
|
||||
), # Reset password step 1 # noqa
|
||||
path(
|
||||
"auth/confirm/reset/",
|
||||
views.ResetConfirmationCodeView.as_view(),
|
||||
name="reset-confirmation-code",
|
||||
), # noqa
|
||||
# Reset password step 2
|
||||
path(
|
||||
"auth/resend/", views.ResendView.as_view(), name="resend"
|
||||
), # resend otp code # noqa
|
||||
path(
|
||||
"auth/me/", views.MeView.as_view(), name="me"
|
||||
), # get user information # noqa
|
||||
path(
|
||||
"change/password/",
|
||||
views.ChangePasswordView.as_view(),
|
||||
name="avatar-update",
|
||||
), # chamge user password # noqa
|
||||
# Update user information
|
||||
path(
|
||||
"auth/update/", UserUpdateView.as_view(), name="update"
|
||||
), # update user information # noqa
|
||||
path(
|
||||
"auth/avatar/update/",
|
||||
UserAvatarUpdateView.as_view(),
|
||||
name="avatar-update",
|
||||
), # update user avatar # noqa
|
||||
]
|
||||
2
core/apps/accounts/views/__init__.py
Normal file
2
core/apps/accounts/views/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .check_passeord import * # noqa
|
||||
from .sms import * # noqa
|
||||
35
core/apps/accounts/views/check_passeord.py
Normal file
35
core/apps/accounts/views/check_passeord.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from django.contrib.auth.hashers import make_password
|
||||
from rest_framework import exceptions, permissions, status, views
|
||||
from rest_framework.generics import get_object_or_404
|
||||
|
||||
from core.http import views as http_views
|
||||
from core.http.models import User
|
||||
|
||||
from ..serializers import ChangePasswordSerializer
|
||||
|
||||
|
||||
class ChangePasswordView(views.APIView, http_views.ApiResponse):
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
user = request.user
|
||||
|
||||
if user is None:
|
||||
raise exceptions.ValidationError(
|
||||
{"status": False, "message": "Foydalanuvchi topilmadi"}
|
||||
)
|
||||
serializer = ChangePasswordSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
if user.check_password(request.data["old_password"]):
|
||||
user.password = make_password(serializer.data["new_password"])
|
||||
user.save()
|
||||
return http_views.ApiResponse().success(
|
||||
"Parol muvaffaqiyatli o'zgartirildi",
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
return http_views.ApiResponse().error(
|
||||
"Noto'g'ri eski parol", status_code=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
return http_views.ApiResponse().error(
|
||||
serializer.errors, status_code=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
146
core/apps/accounts/views/sms.py
Normal file
146
core/apps/accounts/views/sms.py
Normal file
@@ -0,0 +1,146 @@
|
||||
"""
|
||||
SMS configuration (eskiz.uz)
|
||||
"""
|
||||
|
||||
import typing
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework import permissions
|
||||
from rest_framework import request as rest_request
|
||||
from rest_framework import throttling, views
|
||||
|
||||
from core import enums, exceptions, services, utils
|
||||
from core.http import serializers
|
||||
from core.http import views as http_views
|
||||
from core.http.models import User
|
||||
|
||||
|
||||
class RegisterView(
|
||||
views.APIView, services.UserService, http_views.ApiResponse
|
||||
):
|
||||
"""
|
||||
Register new user
|
||||
"""
|
||||
|
||||
serializer_class = serializers.RegisterSerializer
|
||||
throttle_classes = [throttling.UserRateThrottle]
|
||||
|
||||
def post(self, request: rest_request.Request):
|
||||
ser = self.serializer_class(data=request.data)
|
||||
ser.is_valid(raise_exception=True)
|
||||
data = ser.data
|
||||
phone = data.get("phone")
|
||||
|
||||
# Create pending user
|
||||
self.create_user(
|
||||
phone,
|
||||
data.get("first_name"),
|
||||
data.get("last_name"),
|
||||
data.get("password"),
|
||||
)
|
||||
|
||||
# Send confirmation code for sms eskiz.uz
|
||||
self.send_confirmation(phone)
|
||||
return self.success(_(enums.Messages.SEND_MESSAGE) % {"phone": phone})
|
||||
|
||||
|
||||
class ConfirmView(views.APIView, services.UserService, http_views.ApiResponse):
|
||||
"""Confirm otp code"""
|
||||
|
||||
serializer_class = serializers.ConfirmSerializer
|
||||
|
||||
def post(self, request: rest_request.Request):
|
||||
ser = self.serializer_class(data=request.data)
|
||||
ser.is_valid(raise_exception=True)
|
||||
|
||||
data = ser.data
|
||||
phone, code = data.get("phone"), data.get("code")
|
||||
|
||||
try:
|
||||
# Check Sms confirmation otp code
|
||||
if services.SmsService.check_confirm(phone, code=code):
|
||||
# Create user
|
||||
token = self.validate_user(
|
||||
User.objects.filter(phone=phone).first()
|
||||
)
|
||||
return self.success(
|
||||
_(enums.Messages.OTP_CONFIRMED), token=token
|
||||
)
|
||||
except exceptions.SmsException as e:
|
||||
return utils.ResponseException(e)
|
||||
except Exception as e:
|
||||
return self.error(e)
|
||||
|
||||
|
||||
class ResetConfirmationCodeView(
|
||||
views.APIView, http_views.ApiResponse, services.UserService
|
||||
): # noqa
|
||||
"""
|
||||
Reset confirm otp code
|
||||
"""
|
||||
|
||||
serializer_class = serializers.ResetConfirmationSerializer
|
||||
|
||||
def post(self, request: rest_request.Request):
|
||||
ser = self.serializer_class(data=request.data)
|
||||
ser.is_valid(raise_exception=True)
|
||||
|
||||
data = ser.data
|
||||
code, phone, password = (
|
||||
data.get("code"),
|
||||
data.get("phone"),
|
||||
data.get("password"),
|
||||
) # noqa
|
||||
|
||||
try:
|
||||
res = services.SmsService.check_confirm(phone, code)
|
||||
if res:
|
||||
self.change_password(phone, password)
|
||||
return self.success(_(enums.Messages.CHANGED_PASSWORD))
|
||||
return self.error(_(enums.Messages.INVALID_OTP))
|
||||
except exceptions.SmsException as e:
|
||||
return self.error(e, error_code=enums.Codes.INVALID_OTP_ERROR)
|
||||
except Exception as e:
|
||||
return self.error(e)
|
||||
|
||||
|
||||
class ResendView(http_views.AbstractSendSms):
|
||||
"""
|
||||
Resend Otp Code
|
||||
"""
|
||||
|
||||
serializer_class = serializers.ResendSerializer
|
||||
|
||||
|
||||
class ResetPasswordView(http_views.AbstractSendSms):
|
||||
"""
|
||||
Reset user password
|
||||
"""
|
||||
|
||||
serializer_class: typing.Type[serializers.ResetPasswordSerializer] = (
|
||||
serializers.ResetPasswordSerializer
|
||||
) # noqa
|
||||
|
||||
|
||||
# class MeView(views.APIView, http_views.ApiResponse):
|
||||
# """
|
||||
# Get user information
|
||||
# """
|
||||
# permission_classes = [permissions.IsAuthenticated]
|
||||
#
|
||||
# def get(self, request: rest_request.Request):
|
||||
# user = request.user
|
||||
# serializer = serializers.UserSerializer(user, context={'request': request})
|
||||
# return self.success(data=serializer.data)
|
||||
|
||||
|
||||
class MeView(views.APIView, http_views.ApiResponse):
|
||||
"""
|
||||
Get user information
|
||||
"""
|
||||
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get(self, request: rest_request.Request):
|
||||
user = request.user
|
||||
return self.success(data=serializers.UserSerializer(user).data)
|
||||
Reference in New Issue
Block a user