mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 13:49:40 +00:00
@ -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
|
||||
|
@ -6,6 +6,55 @@ import os
|
||||
# GOOGLE_URL used for interact with google server to verify user existence.
|
||||
#GOOGLE_URL = "https://www.googleapis.com/plus/v1/"
|
||||
|
||||
NUMBER = {
|
||||
'point_zero': 0.0,
|
||||
'zero': 0,
|
||||
'one': 1,
|
||||
'two': 2,
|
||||
'three': 3,
|
||||
'four': 4,
|
||||
'five': 5,
|
||||
'six': 6,
|
||||
'seven': 7,
|
||||
'eight': 8,
|
||||
'nine': 9,
|
||||
'ten': 10,
|
||||
'eleven': 11,
|
||||
'twelve': 12,
|
||||
'thirteen': 13,
|
||||
'fourteen': 14,
|
||||
'fifteen': 15,
|
||||
'sixteen': 16,
|
||||
'seventeen': 17,
|
||||
'eighteen': 18,
|
||||
'nineteen': 19,
|
||||
'twenty_four': 24,
|
||||
'twenty_one': 21,
|
||||
'twenty_two': 22,
|
||||
'twenty_five': 25,
|
||||
'thirty': 30,
|
||||
'thirty_five': 35,
|
||||
'thirty_six': 36,
|
||||
'forty': 40,
|
||||
'fifty': 50,
|
||||
'fifty_nine': 59,
|
||||
'sixty': 60,
|
||||
'seventy_five': 75,
|
||||
'eighty': 80,
|
||||
'ninty_five': 95,
|
||||
'ninty_six': 96,
|
||||
'ninety_nine': 99,
|
||||
'hundred': 100,
|
||||
'one_one_nine': 119,
|
||||
'one_twenty': 120,
|
||||
'four_zero_four': 404,
|
||||
'five_hundred': 500,
|
||||
'minus_one': -1,
|
||||
'point_three': 0.3,
|
||||
'point_seven': 0.7
|
||||
}
|
||||
|
||||
|
||||
|
||||
# Super Admin string constant for 'role'
|
||||
SUPER_ADMIN = "Super Admin"
|
||||
|
@ -57,7 +57,9 @@ ERROR_CODE = {
|
||||
"2031": "Invalid password",
|
||||
"2032": "Failed to send email",
|
||||
"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 for password
|
||||
|
@ -12,7 +12,7 @@ from account.models import UserProfile, UserEmailOtp, UserNotification
|
||||
from account.serializers import JuniorSerializer
|
||||
from junior.serializers import JuniorDetailSerializer
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from .utils import upload_image_to_alibaba
|
||||
from base.constants import NUMBER
|
||||
from junior.models import Junior, JuniorPoints
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
"""User serializer"""
|
||||
@ -117,9 +117,9 @@ class CreateGuardianSerializer(serializers.ModelSerializer):
|
||||
guardian.referral_code_used = validated_data.get('referral_code_used', guardian.referral_code_used)
|
||||
guardian.image = validated_data.get('image', guardian.image)
|
||||
"""Complete profile of the junior if below all data are filled"""
|
||||
complete_profile_field = all([guardian.phone, guardian.gender, guardian.country_name,
|
||||
guardian.dob, guardian.country_code, user.first_name, user.last_name,
|
||||
user.email, guardian.image])
|
||||
complete_profile_field = all([guardian.gender, guardian.country_name,
|
||||
guardian.dob, user.first_name,
|
||||
guardian.image])
|
||||
guardian.is_complete_profile = False
|
||||
if complete_profile_field:
|
||||
guardian.is_complete_profile = True
|
||||
@ -224,12 +224,12 @@ class GuardianProfileSerializer(serializers.ModelSerializer):
|
||||
|
||||
def get_total_count(self, obj):
|
||||
"""total fields count"""
|
||||
return 9
|
||||
return NUMBER['five']
|
||||
|
||||
def get_complete_field_count(self, obj):
|
||||
"""total filled fields count"""
|
||||
total_field_list = [obj.user.first_name, obj.user.last_name, obj.user.email, obj.country_name, obj.country_code,
|
||||
obj.phone, obj.gender, obj.dob, obj.image]
|
||||
total_field_list = [obj.user.first_name, obj.country_name,
|
||||
obj.gender, obj.dob, obj.image]
|
||||
total_complete_field = [data for data in total_field_list if data != '' and data is not None]
|
||||
return len(total_complete_field)
|
||||
|
||||
|
@ -5,6 +5,7 @@ from rest_framework import viewsets, status
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from django.contrib.auth.models import User
|
||||
from django.utils import timezone
|
||||
from PIL import Image
|
||||
from datetime import datetime, timedelta
|
||||
"""Import Django app"""
|
||||
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 custom_response, custom_error_response
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import NUMBER
|
||||
from .utils import upload_image_to_alibaba
|
||||
from django.db.models import Sum
|
||||
# Create your views here.
|
||||
@ -54,6 +56,10 @@ class UpdateGuardianProfile(viewsets.ViewSet):
|
||||
data = request.data
|
||||
image = request.data.get('image')
|
||||
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:
|
||||
filename = f"images/{image.name}"
|
||||
image_url = upload_image_to_alibaba(image, filename)
|
||||
@ -114,6 +120,10 @@ class CreateTaskAPIView(viewsets.ModelViewSet):
|
||||
image_data = image
|
||||
else:
|
||||
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_data = image_url
|
||||
data.pop('default_image')
|
||||
|
@ -12,7 +12,7 @@ from account.utils import send_otp_email
|
||||
from junior.models import Junior, JuniorPoints
|
||||
from guardian.tasks import generate_otp
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED
|
||||
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED, NUMBER
|
||||
from guardian.models import Guardian, JuniorTask
|
||||
from account.models import UserEmailOtp
|
||||
from junior.utils import junior_notification_email, junior_approval_mail
|
||||
@ -98,9 +98,8 @@ class CreateJuniorSerializer(serializers.ModelSerializer):
|
||||
junior.referral_code_used = validated_data.get('referral_code_used', junior.referral_code_used)
|
||||
junior.image = validated_data.get('image', junior.image)
|
||||
"""Complete profile of the junior if below all data are filled"""
|
||||
complete_profile_field = all([junior.phone, junior.gender, junior.country_name, junior.image,
|
||||
junior.dob, junior.country_code, user.first_name, user.last_name,
|
||||
user.email])
|
||||
complete_profile_field = all([junior.gender, junior.country_name, junior.image,
|
||||
junior.dob, user.first_name])
|
||||
junior.is_complete_profile = False
|
||||
if complete_profile_field:
|
||||
junior.is_complete_profile = True
|
||||
@ -231,12 +230,12 @@ class JuniorProfileSerializer(serializers.ModelSerializer):
|
||||
|
||||
def get_total_count(self, obj):
|
||||
"""total fields count"""
|
||||
return 9
|
||||
return NUMBER['five']
|
||||
|
||||
def get_complete_field_count(self, obj):
|
||||
"""total filled fields count"""
|
||||
field_list = [obj.auth.first_name, obj.auth.last_name, obj.auth.email, obj.country_name, obj.country_code,
|
||||
obj.phone, obj.gender, obj.dob, obj.image]
|
||||
field_list = [obj.auth.first_name, obj.country_name,
|
||||
obj.gender, obj.dob, obj.image]
|
||||
complete_field = [data for data in field_list if data is not None and data != '']
|
||||
return len(complete_field)
|
||||
class Meta(object):
|
||||
|
@ -3,12 +3,14 @@ from rest_framework import viewsets, status, generics,views
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from rest_framework.response import Response
|
||||
from PIL import Image
|
||||
"""Django app import"""
|
||||
from junior.models import Junior
|
||||
from .serializers import (CreateJuniorSerializer, JuniorDetailListSerializer, AddJuniorSerializer,\
|
||||
RemoveJuniorSerializer)
|
||||
from guardian.models import Guardian
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from base.constants import NUMBER
|
||||
from account.utils import custom_response, custom_error_response
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
# Create your views here.
|
||||
@ -23,6 +25,10 @@ class UpdateJuniorProfile(viewsets.ViewSet):
|
||||
request_data = request.data
|
||||
image = request.data.get('image')
|
||||
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:
|
||||
filename = f"images/{image.name}"
|
||||
image_url = upload_image_to_alibaba(image, filename)
|
||||
|
Reference in New Issue
Block a user