mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-16 02:16:16 +00:00
@ -17,6 +17,8 @@ from guardian.models import Guardian
|
||||
# multiple devices only
|
||||
# user can login in single
|
||||
# device at a time"""
|
||||
# force update
|
||||
# use 308 status code for force update
|
||||
|
||||
def custom_response(custom_error, response_status = status.HTTP_404_NOT_FOUND):
|
||||
"""custom response"""
|
||||
|
@ -123,7 +123,7 @@ class ChangePasswordSerializer(serializers.Serializer):
|
||||
|
||||
def create(self, validated_data):
|
||||
"""
|
||||
|
||||
change password
|
||||
"""
|
||||
new_password = validated_data.pop('new_password')
|
||||
current_password = validated_data.pop('current_password')
|
||||
@ -392,7 +392,8 @@ class UserPhoneOtpSerializer(serializers.ModelSerializer):
|
||||
fields = '__all__'
|
||||
|
||||
class ForceUpdateSerializer(serializers.ModelSerializer):
|
||||
# ForceUpdate Serializer
|
||||
""" ForceUpdate Serializer
|
||||
"""
|
||||
|
||||
class Meta(object):
|
||||
""" meta info """
|
||||
|
@ -101,13 +101,17 @@ ERROR_CODE = {
|
||||
"2072": "You can not approve or reject this task because junior does not exist in the system",
|
||||
"2073": "You can not approve or reject this junior because junior does not exist in the system",
|
||||
"2074": "You can not complete this task because you does not exist in the system",
|
||||
# deactivate account
|
||||
"2075": "Your account is deactivated. Please contact with admin",
|
||||
"2076": "This junior already associate with you",
|
||||
"2077": "You can not add guardian",
|
||||
"2078": "This junior is not associate with you",
|
||||
# force update
|
||||
"2079": "Please update your app version for enjoying uninterrupted services",
|
||||
"2080": "Can not add App version",
|
||||
"2081": "You can not add more than 3 guardian"
|
||||
"2081": "You can not add more than 3 guardian",
|
||||
# guardian code not exist
|
||||
"2082": "Guardian code does not exist"
|
||||
|
||||
}
|
||||
"""Success message code"""
|
||||
|
@ -36,6 +36,7 @@ from django.utils.translation import gettext as _
|
||||
|
||||
# In this serializer file
|
||||
# define user serializer,
|
||||
# define password validation
|
||||
# create guardian serializer,
|
||||
# task serializer,
|
||||
# guardian serializer,
|
||||
@ -47,6 +48,7 @@ from django.utils.translation import gettext as _
|
||||
from rest_framework import serializers
|
||||
|
||||
class PasswordValidator:
|
||||
"""Password validation"""
|
||||
def __init__(self, min_length=8, max_length=None, require_uppercase=True, require_numbers=True):
|
||||
self.min_length = min_length
|
||||
self.max_length = max_length
|
||||
@ -57,6 +59,7 @@ class PasswordValidator:
|
||||
self.enforce_password_policy(value)
|
||||
|
||||
def enforce_password_policy(self, password):
|
||||
# add validation for password
|
||||
special_characters = "!@#$%^&*()_-+=<>?/[]{}|"
|
||||
if len(password) < self.min_length:
|
||||
raise serializers.ValidationError(
|
||||
@ -64,16 +67,20 @@ class PasswordValidator:
|
||||
)
|
||||
|
||||
if self.max_length is not None and len(password) > self.max_length:
|
||||
# must be 8 character
|
||||
raise serializers.ValidationError(
|
||||
_("Password must be at most %(max_length)d characters long.") % {'max_length': self.max_length}
|
||||
)
|
||||
|
||||
if self.require_uppercase and not any(char.isupper() for char in password):
|
||||
# must contain upper case letter
|
||||
raise serializers.ValidationError(_("Password must contain at least one uppercase letter."))
|
||||
|
||||
if self.require_numbers and not any(char.isdigit() for char in password):
|
||||
# must contain digit
|
||||
raise serializers.ValidationError(_("Password must contain at least one digit."))
|
||||
if self.require_numbers and not any(char in special_characters for char in password):
|
||||
# must contain special character
|
||||
raise serializers.ValidationError(_("Password must contain at least one special character."))
|
||||
|
||||
|
||||
|
@ -127,7 +127,7 @@ def update_expired_task_status(data=None):
|
||||
Update task of the status if due date is in past
|
||||
"""
|
||||
try:
|
||||
task_status = [str(NUMBER['one']), str(NUMBER['two']), str(NUMBER['four'])]
|
||||
task_status = [str(NUMBER['one']), str(NUMBER['two'])]
|
||||
JuniorTask.objects.filter(due_date__lt=datetime.today().date(),
|
||||
task_status__in=task_status).update(task_status=str(NUMBER['six']))
|
||||
except ObjectDoesNotExist as e:
|
||||
|
@ -292,7 +292,8 @@ class ApproveJuniorAPIView(viewsets.ModelViewSet):
|
||||
if request.data['action'] == '1':
|
||||
# use ApproveJuniorSerializer serializer
|
||||
serializer = ApproveJuniorSerializer(context={"guardian_code": guardian.guardian_code,
|
||||
"junior": junior_queryset, "action": request.data['action']},
|
||||
"junior": junior_queryset,
|
||||
"action": request.data['action']},
|
||||
data=request.data)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
|
@ -504,24 +504,33 @@ class RemoveGuardianCodeSerializer(serializers.ModelSerializer):
|
||||
model = Junior
|
||||
fields = ('id', )
|
||||
def update(self, instance, validated_data):
|
||||
instance.guardian_code = None
|
||||
instance.guardian_code_status = str(NUMBER['one'])
|
||||
guardian_code = self.context['guardian_code']
|
||||
if guardian_code in instance.guardian_code:
|
||||
instance.guardian_code.remove(guardian_code)
|
||||
else:
|
||||
raise serializers.ValidationError({"error":ERROR_CODE['2082'],"code":"400", "status":"failed"})
|
||||
if not instance.guardian_code:
|
||||
instance.guardian_code_status = str(NUMBER['one'])
|
||||
elif instance.guardian_code and (len(instance.guardian_code) == 1 and '-' in instance.guardian_code):
|
||||
instance.guardian_code_status = str(NUMBER['one'])
|
||||
else:
|
||||
instance.guardian_code_status = str(NUMBER['two'])
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
class FAQSerializer(serializers.ModelSerializer):
|
||||
# FAQ Serializer
|
||||
"""FAQ Serializer"""
|
||||
|
||||
class Meta(object):
|
||||
# meta info
|
||||
"""meta info"""
|
||||
model = FAQ
|
||||
fields = ('id', 'question', 'description')
|
||||
|
||||
class CreateArticleCardSerializer(serializers.ModelSerializer):
|
||||
# Article card Serializer
|
||||
"""Article card Serializer"""
|
||||
|
||||
class Meta(object):
|
||||
# meta info
|
||||
"""meta info"""
|
||||
model = ArticleCard
|
||||
fields = ('id', 'article')
|
||||
|
||||
|
@ -207,13 +207,15 @@ class AddJuniorAPIView(viewsets.ModelViewSet):
|
||||
def associate_guardian(self, user):
|
||||
junior = Junior.objects.filter(auth__email=self.request.data['email']).first()
|
||||
guardian = Guardian.objects.filter(user=self.request.user).first()
|
||||
if junior.guardian_code and ('-' in junior.guardian_code):
|
||||
junior.guardian_code.remove('-')
|
||||
if not junior:
|
||||
return none
|
||||
if junior.guardian_code and (guardian.guardian_code in junior.guardian_code):
|
||||
return False
|
||||
if not junior.guardian_code:
|
||||
junior.guardian_code = [guardian.guardian_code]
|
||||
if type(junior.guardian_code) is list and len(junior.guardian_code) < 4:
|
||||
if type(junior.guardian_code) is list and len(junior.guardian_code) < 3:
|
||||
junior.guardian_code.append(guardian.guardian_code)
|
||||
else:
|
||||
return "Max"
|
||||
@ -264,10 +266,10 @@ class FilterJuniorAPIView(viewsets.ModelViewSet):
|
||||
manual_parameters=[
|
||||
# Example of a query parameter
|
||||
openapi.Parameter(
|
||||
'title', # Query parameter name
|
||||
openapi.IN_QUERY, # Parameter location
|
||||
'title',
|
||||
openapi.IN_QUERY,
|
||||
description='title of the name',
|
||||
type=openapi.TYPE_STRING, # Parameter type
|
||||
type=openapi.TYPE_STRING,
|
||||
),
|
||||
# Add more parameters as needed
|
||||
]
|
||||
@ -724,16 +726,21 @@ class CreateArticleCardAPIView(viewsets.ModelViewSet):
|
||||
|
||||
class RemoveGuardianCodeAPIView(views.APIView):
|
||||
"""Remove guardian code request API
|
||||
No Payload"""
|
||||
Payload
|
||||
{"guardian_code"
|
||||
:"GRD037"
|
||||
}"""
|
||||
serializer_class = RemoveGuardianCodeSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def put(self, request, format=None):
|
||||
try:
|
||||
guardian_code = self.request.data.get("guardian_code")
|
||||
junior_queryset = Junior.objects.filter(auth=self.request.user).last()
|
||||
if junior_queryset:
|
||||
# use RemoveGuardianCodeSerializer serializer
|
||||
serializer = RemoveGuardianCodeSerializer(junior_queryset, data=request.data, partial=True)
|
||||
serializer = RemoveGuardianCodeSerializer(junior_queryset, context = {"guardian_code":guardian_code},
|
||||
data=request.data, partial=True)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
serializer.save()
|
||||
@ -743,7 +750,8 @@ class RemoveGuardianCodeAPIView(views.APIView):
|
||||
# task in another state
|
||||
return custom_error_response(ERROR_CODE['2047'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST)
|
||||
error_detail = e.detail.get('error', None)
|
||||
return custom_error_response(error_detail, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class FAQViewSet(GenericViewSet, mixins.CreateModelMixin,
|
||||
|
Reference in New Issue
Block a user