check size of the image

This commit is contained in:
jain
2023-07-13 13:50:40 +05:30
parent ab06962b79
commit 1ac6c7c01d
5 changed files with 44 additions and 18 deletions

View File

@ -5,6 +5,7 @@ from rest_framework import viewsets, status, views
from rest_framework.decorators import action
import random
import logging
from PIL import Image
from django.views.decorators.csrf import csrf_exempt
from django.utils import timezone
import jwt
@ -24,6 +25,7 @@ from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSeriali
UserNotificationSerializer, UpdateUserNotificationSerializer, UserPhoneOtpSerializer)
from rest_framework_simplejwt.tokens import RefreshToken
from base.messages import ERROR_CODE, SUCCESS_CODE
from base.constants import NUMBER
from guardian.tasks import generate_otp
from account.utils import send_otp_email, send_support_email, custom_response, custom_error_response
from rest_framework.permissions import IsAuthenticated
@ -165,26 +167,29 @@ class UpdateProfileImage(views.APIView):
"""Update profile image"""
permission_classes = [IsAuthenticated]
def put(self, request, format=None):
if str(request.data['user_type']) == '1':
junior_query = Junior.objects.filter(auth=request.user).last()
try:
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}"
image_url = upload_image_to_alibaba(image, filename)
image_data = image_url
serializer = UpdateJuniorProfileImageSerializer(junior_query,
data={'image':image_data}, partial=True)
if str(request.data['user_type']) == '2':
guardian_query = Guardian.objects.filter(user=request.user).last()
image = request.data['image']
filename = f"images/{image.name}"
image_url = upload_image_to_alibaba(image, filename)
image_data = image_url
serializer = UpdateGuardianImageSerializer(guardian_query,
data={'image':image_data}, partial=True)
if serializer.is_valid():
serializer.save()
return custom_response(SUCCESS_CODE['3017'], serializer.data, response_status=status.HTTP_200_OK)
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
if str(request.data['user_type']) == '1':
junior_query = Junior.objects.filter(auth=request.user).last()
serializer = UpdateJuniorProfileImageSerializer(junior_query,
data={'image':image_data}, partial=True)
elif str(request.data['user_type']) == '2':
guardian_query = Guardian.objects.filter(user=request.user).last()
serializer = UpdateGuardianImageSerializer(guardian_query,
data={'image':image_data}, partial=True)
if serializer.is_valid():
serializer.save()
return custom_response(SUCCESS_CODE['3017'], serializer.data, response_status=status.HTTP_200_OK)
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
return custom_error_response(ERROR_CODE['2036'],response_status=status.HTTP_400_BAD_REQUEST)
class ChangePasswordAPIView(views.APIView):
"""change password"""
@ -430,6 +435,10 @@ class UploadImageAPIViewSet(viewsets.ModelViewSet):
"""profile view"""
image_data = request.data['image_url']
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_data = image
request.data['image_url'] = image_data