Merge remote-tracking branch 'origin/main'

This commit is contained in:
marzban-dev
2024-12-15 19:38:46 +03:30
5 changed files with 75 additions and 11 deletions
+6 -3
View File
@@ -1,9 +1,12 @@
from .models import *
from rest_framework import serializers
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
class CustomTokenObtainPairSerializer(TokenObtainPairSerializer):
otp = serializers.CharField(required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
del self.fields['password']
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = User
+1 -1
View File
@@ -3,10 +3,10 @@ from . import views
urlpatterns = [
path('profile', views.ProfileView.as_view()),
path('send_otp', views.SendOTPView.as_view(), name='send-otp-view'),
path('address/create', views.CreateAddressView.as_view(), name='create-address'),
path('address/edit/<int:pk>', views.EditAddressView.as_view(), name='edit-address'),
path('address/delete/<int:pk>', views.DeleteAddressView.as_view(), name='delete-address'),
path('address/list', views.GetUserAddressesView.as_view(), name='list-addresses'),
path('address/<int:pk>', views.GetIDUserAddressView.as_view(), name='get-ID-address'),
]
+63 -3
View File
@@ -2,9 +2,69 @@ from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework import generics, permissions, status
from rest_framework.response import Response
from .serializers import ProfileSerializer, UserAddressSerializer
from .models import UserAddressModel
from rest_framework.permissions import IsAuthenticated
from .serializers import ProfileSerializer, UserAddressSerializer, CustomTokenObtainPairSerializer
from .models import UserAddressModel, User
from rest_framework.permissions import IsAuthenticated, AllowAny
from drf_spectacular.utils import extend_schema, OpenApiParameter
from rest_framework_simplejwt.views import TokenObtainPairView
from django.shortcuts import get_object_or_404
from rest_framework_simplejwt.tokens import RefreshToken
class SendOTPView(APIView):
permission_classes = [AllowAny]
@extend_schema(
request={
"application/json": {
"type": "object",
"properties": {
"phone": {"type": "string", "example": "09123456789"},
},
"required": ["phone"],
}
},
)
def post(self, request):
phone = request.data.get('phone')
try:
user, created = User.objects.get_or_create(phone=phone)
print(created)
print(user.phone)
user.set_otp()
message = f"کد یک بار مصرف : {user.otp}"
print(message)
# send otp
return Response({'detail': 'OTP sent successfully'}, status=status.HTTP_200_OK)
except User.DoesNotExist:
return Response({'detail': 'User not found'}, status=status.HTTP_404_NOT_FOUND)
class CustomTokenObtainPairView(TokenObtainPairView):
serializer_class = CustomTokenObtainPairSerializer
# @extend_schema(
# tags=["Authentication"]
# )
def post(self, request, *args, **kwargs):
phone = request.data.get("phone")
otp = request.data.get("otp")
user = get_object_or_404(User, phone=phone)
if user:
if not otp:
return Response({'detail': 'کد یک بار مصرف ضروری میباشد'}, status=status.HTTP_401_UNAUTHORIZED)
if not user.verify_otp(otp):
return Response({'detail': 'کد یک بار مصرف منقضی شده یا اشتباه است'}, status=status.HTTP_401_UNAUTHORIZED)
user.clear_otp()
refresh = RefreshToken.for_user(user)
return Response({
'refresh': str(refresh),
'access': str(refresh.access_token),
})
return Response({'detail': 'Invalid credentials'}, status=status.HTTP_401_UNAUTHORIZED)
class ProfileView(APIView):
serializer_class = ProfileSerializer
permission_classes = [IsAuthenticated]
-1
View File
@@ -92,7 +92,6 @@ INSTALLED_APPS = [
'rest_framework_simplejwt',
'rest_framework_simplejwt.token_blacklist',
'rest_framework.authtoken',
'djoser',
# custom apps
'product',
'account',
+5 -3
View File
@@ -5,15 +5,17 @@ from drf_spectacular.views import SpectacularSwaggerView, SpectacularAPIView
from django.conf import settings
from rest_framework_simplejwt.views import TokenObtainPairView,TokenRefreshView
from product import views
from account.views import CustomTokenObtainPairView
urlpatterns = [
# djoser
path('auth/', include('djoser.urls')),
path('auth/', include('djoser.urls.jwt')),
# path('auth/', include('djoser.urls')),
# path('auth/', include('djoser.urls.jwt')),
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/', CustomTokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('admin/', admin.site.urls),
path('schema/', SpectacularAPIView.as_view(), name='schema'),