mirror of
https://github.com/Dadechin/Dashboard-XRoom.git
synced 2025-07-04 17:24:34 +00:00
set template to json response
This commit is contained in:
parent
320a52e081
commit
c9331d490a
|
@ -50,6 +50,9 @@ INSTALLED_APPS = [
|
||||||
|
|
||||||
|
|
||||||
]
|
]
|
||||||
|
REST_FRAMEWORK = {
|
||||||
|
'EXCEPTION_HANDLER': 'core.utils.custom_exception_handler',
|
||||||
|
}
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
'django.middleware.security.SecurityMiddleware',
|
'django.middleware.security.SecurityMiddleware',
|
||||||
|
|
|
@ -17,6 +17,7 @@ urlpatterns = [
|
||||||
|
|
||||||
re_path('signup', userView.signup , name="signup"),
|
re_path('signup', userView.signup , name="signup"),
|
||||||
re_path('login', userView.login),
|
re_path('login', userView.login),
|
||||||
|
re_path('test_token', userView.test_token),
|
||||||
|
|
||||||
re_path('editProfile/', userView.editProfile, name='edit-profile'),
|
re_path('editProfile/', userView.editProfile, name='edit-profile'),
|
||||||
re_path('resetPassword/', userView.resetPassword, name='reset-password'),
|
re_path('resetPassword/', userView.resetPassword, name='reset-password'),
|
||||||
|
|
15
core/utils.py
Normal file
15
core/utils.py
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# in your_project/utils.py or a similar file
|
||||||
|
|
||||||
|
from rest_framework.views import exception_handler
|
||||||
|
|
||||||
|
def custom_exception_handler(exc, context):
|
||||||
|
response = exception_handler(exc, context)
|
||||||
|
|
||||||
|
if response is not None:
|
||||||
|
response.data = {
|
||||||
|
"status": response.status_code,
|
||||||
|
"data": {},
|
||||||
|
"message": str(exc.detail) if hasattr(exc, 'detail') else str(exc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response
|
|
@ -171,21 +171,51 @@ def sendCodeAndNewPassword(request):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# @api_view(['POST'])
|
||||||
|
# def login(request):
|
||||||
|
# try:
|
||||||
|
# customer = Customer.objects.get(mobile_number=request.data['mobile_number'])
|
||||||
|
# except get_user_model().DoesNotExist:
|
||||||
|
# return Response("User not found", status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
# if not customer.user.check_password(request.data['password']):
|
||||||
|
# return Response("Invalid password", status=status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
|
# token, created = Token.objects.get_or_create(user=customer.user)
|
||||||
|
# serializer = UserSerializer(customer.user)
|
||||||
|
# return Response({'token': token.key, 'user': serializer.data})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
def login(request):
|
def login(request):
|
||||||
try:
|
try:
|
||||||
customer = Customer.objects.get(mobile_number=request.data['mobile_number'])
|
customer = Customer.objects.get(mobile_number=request.data['mobile_number'])
|
||||||
except get_user_model().DoesNotExist:
|
except Customer.DoesNotExist:
|
||||||
return Response("User not found", status=status.HTTP_404_NOT_FOUND)
|
return Response({
|
||||||
|
"status": 401,
|
||||||
|
"data": {},
|
||||||
|
"message": "موبایل یا پسورد غلط است"
|
||||||
|
}, status=status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
if not customer.user.check_password(request.data['password']):
|
if not customer.user.check_password(request.data['password']):
|
||||||
return Response("Invalid password", status=status.HTTP_401_UNAUTHORIZED)
|
return Response({
|
||||||
|
"status": 401,
|
||||||
|
"data": {},
|
||||||
|
"message": "موبایل یا پسورد غلط است"
|
||||||
|
}, status=status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
token, created = Token.objects.get_or_create(user=customer.user)
|
token, created = Token.objects.get_or_create(user=customer.user)
|
||||||
serializer = UserSerializer(customer.user)
|
serializer = UserSerializer(customer.user)
|
||||||
return Response({'token': token.key, 'user': serializer.data})
|
|
||||||
|
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
"status": 200,
|
||||||
|
"data": {
|
||||||
|
"token": token.key,
|
||||||
|
"user": serializer.data
|
||||||
|
},
|
||||||
|
"message": "ورود با موفقیت انجام شد"
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -231,49 +261,47 @@ def sendSmsVerification(request):
|
||||||
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
def getInfo(request):
|
def getInfo(request):
|
||||||
# Retrieve the current user
|
|
||||||
user = request.user
|
user = request.user
|
||||||
|
|
||||||
# Assuming a OneToOneField relation between User and Customer
|
|
||||||
try:
|
try:
|
||||||
# Retrieve the associated customer object
|
|
||||||
customer = Customer.objects.get(user=user)
|
customer = Customer.objects.get(user=user)
|
||||||
|
|
||||||
# Get all images uploaded by this user
|
|
||||||
user_images = Image.objects.filter(user=user).order_by('-created_at')
|
user_images = Image.objects.filter(user=user).order_by('-created_at')
|
||||||
user_pdfs = Pdf.objects.filter(user=user).order_by('-created_at')
|
user_pdfs = Pdf.objects.filter(user=user).order_by('-created_at')
|
||||||
user_videos = Video.objects.filter(user=user).order_by('-created_at')
|
user_videos = Video.objects.filter(user=user).order_by('-created_at')
|
||||||
user_glb = Glb.objects.filter(user=user).order_by('-created_at')
|
user_glbs = Glb.objects.filter(user=user).order_by('-created_at')
|
||||||
|
|
||||||
|
customer_serializer = CustomerSerializer(customer)
|
||||||
image_serializer = ImageSerializer(user_images, many=True)
|
image_serializer = ImageSerializer(user_images, many=True)
|
||||||
pdf_serializer = PdfSerializer(user_pdfs, many=True)
|
pdf_serializer = PdfSerializer(user_pdfs, many=True)
|
||||||
Video_serializer = VideoSerializer(user_videos, many=True)
|
video_serializer = VideoSerializer(user_videos, many=True)
|
||||||
glb_serializer = GlbSerializer(user_glb, many=True)
|
glb_serializer = GlbSerializer(user_glbs, many=True)
|
||||||
|
|
||||||
# Serialize the customer data
|
|
||||||
customer_serializer = CustomerSerializer(customer)
|
|
||||||
|
|
||||||
# Manually select user fields to return
|
|
||||||
user_data = {
|
user_data = {
|
||||||
'id': user.id,
|
'id': user.id,
|
||||||
'first_name': user.first_name,
|
'first_name': user.first_name,
|
||||||
'last_name': user.last_name,
|
'last_name': user.last_name,
|
||||||
# add any other fields you need
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response({
|
return Response({
|
||||||
|
'status': 200,
|
||||||
|
'data': {
|
||||||
'customer': customer_serializer.data,
|
'customer': customer_serializer.data,
|
||||||
'user': user_data,
|
'user': user_data,
|
||||||
'images': image_serializer.data , # Add images to response
|
'images': image_serializer.data,
|
||||||
'pdfs': pdf_serializer.data , # Add images to response
|
'pdfs': pdf_serializer.data,
|
||||||
'videos': Video_serializer.data ,# Add images to response
|
'videos': video_serializer.data,
|
||||||
'glbs': glb_serializer.data , # Add images to response
|
'glbs': glb_serializer.data,
|
||||||
})
|
},
|
||||||
|
'message': 'موفق'
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
except Customer.DoesNotExist:
|
except Customer.DoesNotExist:
|
||||||
# If no customer object exists for the user, return an error response
|
return Response({
|
||||||
return Response({'error': 'No customer data found for this user'}, status=404)
|
'status': 404,
|
||||||
|
'data': {},
|
||||||
|
'message': 'اطلاعات مشتری یافت نشد'
|
||||||
|
}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -395,14 +423,42 @@ def submitSmsVerification(request):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# @api_view(['GET'])
|
||||||
|
# @authentication_classes([SessionAuthentication, TokenAuthentication])
|
||||||
|
# @permission_classes([IsAuthenticated])
|
||||||
|
# def test_token(request):
|
||||||
|
# if not user_has_role(request.user, 'admin'):
|
||||||
|
# return Response({'message': 'No access'}, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
# return Response({'message': 'User has admin role'})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
def test_token(request):
|
def test_token(request):
|
||||||
if not user_has_role(request.user, 'admin'):
|
if not user_has_role(request.user, 'admin'):
|
||||||
return Response({'message': 'No access'}, status=status.HTTP_403_FORBIDDEN)
|
return Response({
|
||||||
|
"status": 403,
|
||||||
|
"data": {},
|
||||||
|
"message": "دسترسی غیرمجاز"
|
||||||
|
}, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
"status": 200,
|
||||||
|
"data": {
|
||||||
|
"username": request.user.username
|
||||||
|
},
|
||||||
|
"message": "موفق"
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return Response({'message': 'User has admin role'})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ Content-Type: application/json
|
||||||
POST http://127.0.0.1:8000/login
|
POST http://127.0.0.1:8000/login
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{ "mobile_number":"09140086509", "password": "123456" }
|
{ "mobile_number":"09140086509", "password": "12345678" }
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ Authorization: token d3f1b03996140c8f7561d67221953ff704b482cb
|
||||||
|
|
||||||
GET http://127.0.0.1:8000/test_token
|
GET http://127.0.0.1:8000/test_token
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
Authorization: token c362581117e209735d412226e54596867e370892
|
Authorization: token 224648840820ad0948ddee5b00d53ae67c040c49
|
||||||
# Authorization: token 53e2b003a92e22aca85c95088a438ece8d9a5dfb
|
# Authorization: token 53e2b003a92e22aca85c95088a438ece8d9a5dfb
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ Authorization: token c362581117e209735d412226e54596867e370892
|
||||||
|
|
||||||
GET http://127.0.0.1:8000/getInfo
|
GET http://127.0.0.1:8000/getInfo
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
Authorization: token 3d5ab31449b6a075e3967559526d5e31977431a1
|
Authorization: token 224648840820ad0948ddee5b00d53ae67c040c49
|
||||||
# Authorization: token 53e2b003a92e22aca85c95088a438ece8d9a5dfb
|
# Authorization: token 53e2b003a92e22aca85c95088a438ece8d9a5dfb
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user