mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 16:44:54 +00:00
task assign to multiple junior
This commit is contained in:
@ -218,7 +218,7 @@ class TaskSerializer(serializers.ModelSerializer):
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = JuniorTask
|
||||
fields = ['id', 'task_name','task_description','points', 'due_date', 'junior', 'default_image']
|
||||
fields = ['id', 'task_name','task_description','points', 'due_date','default_image']
|
||||
|
||||
def validate_due_date(self, value):
|
||||
"""validation on due date"""
|
||||
@ -229,11 +229,27 @@ class TaskSerializer(serializers.ModelSerializer):
|
||||
return value
|
||||
def create(self, validated_data):
|
||||
"""create default task image data"""
|
||||
validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last()
|
||||
guardian = Guardian.objects.filter(user=self.context['user']).last()
|
||||
# update image of the task
|
||||
images = self.context['image']
|
||||
validated_data['default_image'] = images
|
||||
instance = JuniorTask.objects.create(**validated_data)
|
||||
junior_ids = self.context['junior_data']
|
||||
print("junior_ids==>", junior_ids, '==>', type(junior_ids))
|
||||
print()
|
||||
junior_data = junior_ids[0].split(',')
|
||||
print("junior_data[0==>", junior_data, '==>', type(junior_data))
|
||||
tasks_created = []
|
||||
|
||||
for junior_id in junior_data:
|
||||
print("junior_id==>",junior_id,'==>',type(junior_id))
|
||||
task_data = validated_data.copy()
|
||||
task_data['guardian'] = guardian
|
||||
task_data['default_image'] = images
|
||||
task_data['junior'] = Junior.objects.filter(id=junior_id).last()
|
||||
print("task_data===>", task_data, '===>', type(task_data))
|
||||
print("task_data['junior']===>", task_data['junior'], '===>', type(task_data['junior']))
|
||||
instance = JuniorTask.objects.create(**task_data)
|
||||
tasks_created.append(instance)
|
||||
print("tasks_created==>", tasks_created, '==>', type(tasks_created))
|
||||
return instance
|
||||
|
||||
class GuardianDetailSerializer(serializers.ModelSerializer):
|
||||
|
||||
@ -177,42 +177,55 @@ class CreateTaskAPIView(viewsets.ModelViewSet):
|
||||
"""
|
||||
try:
|
||||
image = request.data['default_image']
|
||||
junior = request.data['junior']
|
||||
junior_id = Junior.objects.filter(id=junior).last()
|
||||
if junior_id:
|
||||
guardian_data = Guardian.objects.filter(user=request.user).last()
|
||||
index = junior_id.guardian_code.index(guardian_data.guardian_code)
|
||||
status_index = junior_id.guardian_code_status[index]
|
||||
if status_index == str(NUMBER['three']):
|
||||
return custom_error_response(ERROR_CODE['2078'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
allowed_extensions = ['.jpg', '.jpeg', '.png']
|
||||
if not any(extension in str(image) for extension in allowed_extensions):
|
||||
return custom_error_response(ERROR_CODE['2048'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
if not junior.isnumeric():
|
||||
"""junior value must be integer"""
|
||||
return custom_error_response(ERROR_CODE['2047'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
data = request.data
|
||||
if 'https' in str(image):
|
||||
image_data = image
|
||||
else:
|
||||
filename = f"images/{image}"
|
||||
if image and image.size == 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')
|
||||
# use TaskSerializer serializer
|
||||
serializer = TaskSerializer(context={"user":request.user, "image":image_data}, data=data)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
task = serializer.save()
|
||||
juniors = request.data['junior'].split(',')
|
||||
print("juniors===>", juniors, '===>', type(juniors))
|
||||
print()
|
||||
|
||||
send_notification.delay(TASK_ASSIGNED, request.auth.payload['user_id'], GUARDIAN,
|
||||
junior_id.auth.id, {'task_id': task.id})
|
||||
return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
return custom_error_response(ERROR_CODE['2047'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
for junior in juniors:
|
||||
print("junior===>", junior, '===>', type(junior))
|
||||
junior_id = Junior.objects.filter(id=junior).last()
|
||||
print("junior_id===>", junior_id, '===>', type(junior_id))
|
||||
if junior_id:
|
||||
guardian_data = Guardian.objects.filter(user=request.user).last()
|
||||
index = junior_id.guardian_code.index(guardian_data.guardian_code)
|
||||
status_index = junior_id.guardian_code_status[index]
|
||||
if status_index == str(NUMBER['three']):
|
||||
return custom_error_response(ERROR_CODE['2078'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
allowed_extensions = ['.jpg', '.jpeg', '.png']
|
||||
if not any(extension in str(image) for extension in allowed_extensions):
|
||||
return custom_error_response(ERROR_CODE['2048'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
if not junior.isnumeric():
|
||||
"""junior value must be integer"""
|
||||
return custom_error_response(ERROR_CODE['2047'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||
print("request.data===>", request.data, '===>', type(request.data))
|
||||
data = request.data
|
||||
if 'https' in str(image):
|
||||
image_data = image
|
||||
else:
|
||||
filename = f"images/{image}"
|
||||
if image and image.size == 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')
|
||||
print("data===>",data,'===>',type(data))
|
||||
junior_data = data.pop('junior')
|
||||
print()
|
||||
print("data===>", data, '===>', type(data))
|
||||
print("junior_data===>", junior_data, '===>', type(junior_data))
|
||||
# use TaskSerializer serializer
|
||||
serializer = TaskSerializer(context={"user":request.user, "image":image_data,
|
||||
"junior_data":junior_data}, data=data)
|
||||
if serializer.is_valid():
|
||||
# save serializer
|
||||
task = serializer.save()
|
||||
|
||||
send_notification.delay(TASK_ASSIGNED, request.auth.payload['user_id'], GUARDIAN,
|
||||
junior_id.auth.id, {'task_id': task.id})
|
||||
return custom_response(SUCCESS_CODE['3018'], serializer.data, response_status=status.HTTP_200_OK)
|
||||
return custom_error_response(serializer.errors, response_status=status.HTTP_400_BAD_REQUEST)
|
||||
else:
|
||||
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)
|
||||
|
||||
|
||||
@ -181,32 +181,32 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
# database query logs settings
|
||||
# Allows us to check db hits
|
||||
# useful to optimize db query and hit
|
||||
LOGGING = {
|
||||
"version": 1,
|
||||
"filters": {
|
||||
"require_debug_true": {
|
||||
"()": "django.utils.log.RequireDebugTrue"
|
||||
}
|
||||
},
|
||||
"handlers": {
|
||||
"console": {
|
||||
"level": "DEBUG",
|
||||
"filters": [
|
||||
"require_debug_true"
|
||||
],
|
||||
"class": "logging.StreamHandler"
|
||||
}
|
||||
},
|
||||
# database logger
|
||||
"loggers": {
|
||||
"django.db.backends": {
|
||||
"level": "DEBUG",
|
||||
"handlers": [
|
||||
"console"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
# LOGGING = {
|
||||
# "version": 1,
|
||||
# "filters": {
|
||||
# "require_debug_true": {
|
||||
# "()": "django.utils.log.RequireDebugTrue"
|
||||
# }
|
||||
# },
|
||||
# "handlers": {
|
||||
# "console": {
|
||||
# "level": "DEBUG",
|
||||
# "filters": [
|
||||
# "require_debug_true"
|
||||
# ],
|
||||
# "class": "logging.StreamHandler"
|
||||
# }
|
||||
# },
|
||||
# # database logger
|
||||
# "loggers": {
|
||||
# "django.db.backends": {
|
||||
# "level": "DEBUG",
|
||||
# "handlers": [
|
||||
# "console"
|
||||
# ]
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/3.0/topics/i18n/
|
||||
|
||||
Reference in New Issue
Block a user