mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-16 18:36:18 +00:00
check size of the image
This commit is contained in:
@ -5,6 +5,7 @@ from rest_framework import viewsets, status, views
|
|||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
import random
|
import random
|
||||||
import logging
|
import logging
|
||||||
|
from PIL import Image
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
import jwt
|
import jwt
|
||||||
@ -24,6 +25,7 @@ from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSeriali
|
|||||||
UserNotificationSerializer, UpdateUserNotificationSerializer, UserPhoneOtpSerializer)
|
UserNotificationSerializer, UpdateUserNotificationSerializer, UserPhoneOtpSerializer)
|
||||||
from rest_framework_simplejwt.tokens import RefreshToken
|
from rest_framework_simplejwt.tokens import RefreshToken
|
||||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||||
|
from base.constants import NUMBER
|
||||||
from guardian.tasks import generate_otp
|
from guardian.tasks import generate_otp
|
||||||
from account.utils import send_otp_email, send_support_email, custom_response, custom_error_response
|
from account.utils import send_otp_email, send_support_email, custom_response, custom_error_response
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
@ -165,26 +167,29 @@ class UpdateProfileImage(views.APIView):
|
|||||||
"""Update profile image"""
|
"""Update profile image"""
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
def put(self, request, format=None):
|
def put(self, request, format=None):
|
||||||
if str(request.data['user_type']) == '1':
|
try:
|
||||||
junior_query = Junior.objects.filter(auth=request.user).last()
|
|
||||||
image = request.data['image']
|
image = request.data['image']
|
||||||
|
img = Image.open(image)
|
||||||
|
width, height = img.size
|
||||||
|
if width == NUMBER['zero'] or height == NUMBER['zero']:
|
||||||
|
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
filename = f"images/{image.name}"
|
filename = f"images/{image.name}"
|
||||||
image_url = upload_image_to_alibaba(image, filename)
|
image_url = upload_image_to_alibaba(image, filename)
|
||||||
image_data = image_url
|
image_data = image_url
|
||||||
serializer = UpdateJuniorProfileImageSerializer(junior_query,
|
if str(request.data['user_type']) == '1':
|
||||||
data={'image':image_data}, partial=True)
|
junior_query = Junior.objects.filter(auth=request.user).last()
|
||||||
if str(request.data['user_type']) == '2':
|
serializer = UpdateJuniorProfileImageSerializer(junior_query,
|
||||||
guardian_query = Guardian.objects.filter(user=request.user).last()
|
data={'image':image_data}, partial=True)
|
||||||
image = request.data['image']
|
elif str(request.data['user_type']) == '2':
|
||||||
filename = f"images/{image.name}"
|
guardian_query = Guardian.objects.filter(user=request.user).last()
|
||||||
image_url = upload_image_to_alibaba(image, filename)
|
serializer = UpdateGuardianImageSerializer(guardian_query,
|
||||||
image_data = image_url
|
data={'image':image_data}, partial=True)
|
||||||
serializer = UpdateGuardianImageSerializer(guardian_query,
|
if serializer.is_valid():
|
||||||
data={'image':image_data}, partial=True)
|
serializer.save()
|
||||||
if serializer.is_valid():
|
return custom_response(SUCCESS_CODE['3017'], serializer.data, response_status=status.HTTP_200_OK)
|
||||||
serializer.save()
|
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
return custom_response(SUCCESS_CODE['3017'], serializer.data, response_status=status.HTTP_200_OK)
|
except Exception as e:
|
||||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
return custom_error_response(ERROR_CODE['2036'],response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
class ChangePasswordAPIView(views.APIView):
|
class ChangePasswordAPIView(views.APIView):
|
||||||
"""change password"""
|
"""change password"""
|
||||||
@ -430,6 +435,10 @@ class UploadImageAPIViewSet(viewsets.ModelViewSet):
|
|||||||
"""profile view"""
|
"""profile view"""
|
||||||
image_data = request.data['image_url']
|
image_data = request.data['image_url']
|
||||||
filename = f"default_task_images/{image_data.name}"
|
filename = f"default_task_images/{image_data.name}"
|
||||||
|
img = Image.open(image_data)
|
||||||
|
width, height = img.size
|
||||||
|
if width == NUMBER['zero'] or height == NUMBER['zero']:
|
||||||
|
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
image = upload_image_to_alibaba(image_data, filename)
|
image = upload_image_to_alibaba(image_data, filename)
|
||||||
image_data = image
|
image_data = image
|
||||||
request.data['image_url'] = image_data
|
request.data['image_url'] = image_data
|
||||||
|
@ -57,7 +57,9 @@ ERROR_CODE = {
|
|||||||
"2031": "Invalid password",
|
"2031": "Invalid password",
|
||||||
"2032": "Failed to send email",
|
"2032": "Failed to send email",
|
||||||
"2033": "Missing required fields",
|
"2033": "Missing required fields",
|
||||||
"2034": "Junior is not associated"
|
"2034": "Junior is not associated",
|
||||||
|
"2035": "Image should not be 0 kb",
|
||||||
|
"2036": "Choose valid user"
|
||||||
}
|
}
|
||||||
SUCCESS_CODE = {
|
SUCCESS_CODE = {
|
||||||
# Success code for password
|
# Success code for password
|
||||||
|
@ -13,7 +13,6 @@ from account.serializers import JuniorSerializer
|
|||||||
from junior.serializers import JuniorDetailSerializer
|
from junior.serializers import JuniorDetailSerializer
|
||||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||||
from base.constants import NUMBER
|
from base.constants import NUMBER
|
||||||
from .utils import upload_image_to_alibaba
|
|
||||||
from junior.models import Junior, JuniorPoints
|
from junior.models import Junior, JuniorPoints
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
"""User serializer"""
|
"""User serializer"""
|
||||||
|
@ -5,6 +5,7 @@ from rest_framework import viewsets, status
|
|||||||
from rest_framework.pagination import PageNumberPagination
|
from rest_framework.pagination import PageNumberPagination
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from PIL import Image
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
"""Import Django app"""
|
"""Import Django app"""
|
||||||
from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer,
|
from .serializers import (UserSerializer, CreateGuardianSerializer, TaskSerializer, TaskDetailsSerializer,
|
||||||
@ -17,6 +18,7 @@ from .tasks import generate_otp
|
|||||||
from account.utils import send_otp_email
|
from account.utils import send_otp_email
|
||||||
from account.utils import custom_response, custom_error_response
|
from account.utils import custom_response, custom_error_response
|
||||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||||
|
from base.constants import NUMBER
|
||||||
from .utils import upload_image_to_alibaba
|
from .utils import upload_image_to_alibaba
|
||||||
from django.db.models import Sum
|
from django.db.models import Sum
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@ -54,6 +56,10 @@ class UpdateGuardianProfile(viewsets.ViewSet):
|
|||||||
data = request.data
|
data = request.data
|
||||||
image = request.data.get('image')
|
image = request.data.get('image')
|
||||||
image_url = ''
|
image_url = ''
|
||||||
|
img = Image.open(image)
|
||||||
|
width, height = img.size
|
||||||
|
if width == NUMBER['zero'] or height == NUMBER['zero']:
|
||||||
|
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
if image:
|
if image:
|
||||||
filename = f"images/{image.name}"
|
filename = f"images/{image.name}"
|
||||||
image_url = upload_image_to_alibaba(image, filename)
|
image_url = upload_image_to_alibaba(image, filename)
|
||||||
@ -114,6 +120,10 @@ class CreateTaskAPIView(viewsets.ModelViewSet):
|
|||||||
image_data = image
|
image_data = image
|
||||||
else:
|
else:
|
||||||
filename = f"images/{image}"
|
filename = f"images/{image}"
|
||||||
|
img = Image.open(image)
|
||||||
|
width, height = img.size
|
||||||
|
if width == NUMBER['zero'] or height == NUMBER['zero']:
|
||||||
|
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
image_url = upload_image_to_alibaba(image, filename)
|
image_url = upload_image_to_alibaba(image, filename)
|
||||||
image_data = image_url
|
image_data = image_url
|
||||||
data.pop('default_image')
|
data.pop('default_image')
|
||||||
|
@ -3,12 +3,14 @@ from rest_framework import viewsets, status, generics,views
|
|||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.pagination import PageNumberPagination
|
from rest_framework.pagination import PageNumberPagination
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from PIL import Image
|
||||||
"""Django app import"""
|
"""Django app import"""
|
||||||
from junior.models import Junior
|
from junior.models import Junior
|
||||||
from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, AddJuniorSerializer,\
|
from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, AddJuniorSerializer,\
|
||||||
RemoveJuniorSerializer)
|
RemoveJuniorSerializer)
|
||||||
from guardian.models import Guardian
|
from guardian.models import Guardian
|
||||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||||
|
from base.constants import NUMBER
|
||||||
from account.utils import custom_response, custom_error_response
|
from account.utils import custom_response, custom_error_response
|
||||||
from guardian.utils import upload_image_to_alibaba
|
from guardian.utils import upload_image_to_alibaba
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
@ -23,6 +25,10 @@ class UpdateJuniorProfile(viewsets.ViewSet):
|
|||||||
request_data = request.data
|
request_data = request.data
|
||||||
image = request.data.get('image')
|
image = request.data.get('image')
|
||||||
image_url = ''
|
image_url = ''
|
||||||
|
img = Image.open(image)
|
||||||
|
width, height = img.size
|
||||||
|
if width == NUMBER['zero'] or height == NUMBER['zero']:
|
||||||
|
return custom_error_response(ERROR_CODE['2035'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
if image:
|
if image:
|
||||||
filename = f"images/{image.name}"
|
filename = f"images/{image.name}"
|
||||||
image_url = upload_image_to_alibaba(image, filename)
|
image_url = upload_image_to_alibaba(image, filename)
|
||||||
|
Reference in New Issue
Block a user