mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 16:44:54 +00:00
list of the task and task table
This commit is contained in:
@ -6,9 +6,13 @@ from rest_framework import serializers
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
from django.db import transaction
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.validators import URLValidator
|
||||
from django.core.exceptions import ValidationError
|
||||
"""Import Django app"""
|
||||
from .models import Guardian
|
||||
from .models import Guardian, JuniorTask
|
||||
from account.models import UserProfile, UserEmailOtp
|
||||
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 junior.models import Junior
|
||||
@ -134,3 +138,81 @@ class CreateGuardianSerializer(serializers.ModelSerializer):
|
||||
with transaction.atomic():
|
||||
instance = super().save(**kwargs)
|
||||
return instance
|
||||
|
||||
|
||||
|
||||
|
||||
class ImageField(serializers.Field):
|
||||
def to_representation(self, value):
|
||||
return value
|
||||
def to_internal_value(self, data):
|
||||
# If data is a valid URL, return it as is
|
||||
if validators.URLValidator()(data):
|
||||
return data
|
||||
# else:
|
||||
# raise serializers.ValidationError("Enter a valid URL.")
|
||||
|
||||
|
||||
|
||||
class TaskSerializer(serializers.ModelSerializer):
|
||||
class Meta(object):
|
||||
model = JuniorTask
|
||||
fields = ['task_name','task_description','points', 'due_date', 'junior', 'image']
|
||||
def validate_image(self, value):
|
||||
if 'http' in str(value):
|
||||
print("999999999999")
|
||||
return value
|
||||
else:
|
||||
print("00000000000000000")
|
||||
filename = f"images/{value.name}"
|
||||
image_url = upload_image_to_alibaba(value, filename)
|
||||
return image_url
|
||||
def create(self, validated_data):
|
||||
print("validated_data NOW===>", validated_data, '===>', type(validated_data))
|
||||
validated_data['guardian'] = Guardian.objects.filter(user=self.context['user']).last()
|
||||
# images = self.context['image']
|
||||
# if 'http' in str(images):
|
||||
# print("999999999999")
|
||||
# validated_data['image'] = images
|
||||
# else:
|
||||
# print("00000000000000000")
|
||||
# filename = f"images/{images.name}"
|
||||
# image_url = upload_image_to_alibaba(images, filename)
|
||||
# validated_data['image'] = image_url
|
||||
|
||||
print("validated_data NOW===>", validated_data, '===>', type(validated_data))
|
||||
instance = JuniorTask.objects.create(**validated_data)
|
||||
return instance
|
||||
|
||||
class GuardianDetailSerializer(serializers.ModelSerializer):
|
||||
"""junior serializer"""
|
||||
|
||||
email = serializers.SerializerMethodField('get_auth')
|
||||
first_name = serializers.SerializerMethodField('get_first_name')
|
||||
last_name = serializers.SerializerMethodField('get_last_name')
|
||||
|
||||
def get_auth(self, obj):
|
||||
"""user email address"""
|
||||
return obj.user.username
|
||||
|
||||
def get_first_name(self, obj):
|
||||
"""user first name"""
|
||||
return obj.user.first_name
|
||||
|
||||
def get_last_name(self, obj):
|
||||
"""user last name"""
|
||||
return obj.user.last_name
|
||||
class Meta(object):
|
||||
"""Meta info"""
|
||||
model = Guardian
|
||||
fields = ['id', 'email', 'first_name', 'last_name', 'country_code', 'phone', 'gender', 'dob',
|
||||
'guardian_code', 'referral_code','is_active', 'is_complete_profile', 'created_at', 'image',
|
||||
'updated_at']
|
||||
class TaskDetailsSerializer(serializers.ModelSerializer):
|
||||
|
||||
guardian = GuardianDetailSerializer()
|
||||
junior = JuniorDetailSerializer()
|
||||
class Meta(object):
|
||||
model = JuniorTask
|
||||
fields = ['id', 'guardian', 'task_name', 'task_description', 'points', 'due_date', 'image','junior',
|
||||
'task_status', 'is_active']
|
||||
|
||||
Reference in New Issue
Block a user