refactor(Profile):changed profile serializer
This commit is contained in:
@@ -141,15 +141,9 @@ from phonenumbers import parse, phonenumberutil, NumberParseException
|
||||
|
||||
class ProfileSerializer(serializers.ModelSerializer):
|
||||
profile_photo = serializers.ImageField(required=False)
|
||||
email = serializers.CharField(read_only=True)
|
||||
invite_count = serializers.SerializerMethodField(read_only=True)
|
||||
principal_type_name = serializers.SerializerMethodField(read_only=True)
|
||||
has_active_subscription = serializers.SerializerMethodField(read_only=True)
|
||||
has_preferences = serializers.SerializerMethodField(read_only=True)
|
||||
register_complete = serializers.BooleanField(read_only=True)
|
||||
email = serializers.CharField(read_only=True)
|
||||
is_active = serializers.BooleanField(read_only=True)
|
||||
going_events_count = serializers.SerializerMethodField(read_only=True)
|
||||
interested_events_count = serializers.SerializerMethodField(read_only=True)
|
||||
phone_no = serializers.CharField(required=True)
|
||||
|
||||
class Meta:
|
||||
@@ -163,18 +157,12 @@ class ProfileSerializer(serializers.ModelSerializer):
|
||||
"last_name",
|
||||
"phone_no",
|
||||
"email",
|
||||
"invite_count",
|
||||
"register_complete",
|
||||
"has_active_subscription",
|
||||
"has_preferences",
|
||||
"linkedin_profile",
|
||||
"youtube_profile",
|
||||
"facebook_profile",
|
||||
"instagram_profile",
|
||||
"website",
|
||||
"is_active",
|
||||
"going_events_count",
|
||||
"interested_events_count",
|
||||
]
|
||||
|
||||
# def validate_phone_no(self, value):
|
||||
@@ -196,71 +184,14 @@ class ProfileSerializer(serializers.ModelSerializer):
|
||||
instance.last_name = validated_data.get("last_name", instance.last_name)
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
def get_going_events_count(self, obj):
|
||||
return EventPrincipalInteraction.objects.filter(
|
||||
principal=obj, status="going"
|
||||
).count()
|
||||
|
||||
def get_interested_events_count(self, obj):
|
||||
return EventPrincipalInteraction.objects.filter(
|
||||
principal=obj, status="interested"
|
||||
).count()
|
||||
|
||||
def get_invite_count(self, obj):
|
||||
if obj:
|
||||
return ReferralRecord.get_invite_count(obj)
|
||||
return 0
|
||||
|
||||
def get_principal_type_name(self, obj):
|
||||
return obj.principal_type.name if obj.principal_type else None
|
||||
|
||||
def get_has_preferences(self, obj):
|
||||
return PrincipalPreference.objects.filter(principal=obj).exists()
|
||||
|
||||
def get_image_url(self, obj, field_name, request):
|
||||
image_field = getattr(obj, field_name)
|
||||
if image_field:
|
||||
return request.build_absolute_uri(image_field.url)
|
||||
return ""
|
||||
|
||||
def get_has_active_subscription(self, obj):
|
||||
subscription_status = {
|
||||
"has_active_subscription": False,
|
||||
"in_grace_period": False,
|
||||
"grace_period_end_date": None,
|
||||
}
|
||||
today = timezone.now().date()
|
||||
|
||||
# Attempt to find the active subscription with the furthest grace_period_end_date
|
||||
latest_subscription = (
|
||||
PrincipalSubscription.objects.filter(
|
||||
principal=obj,
|
||||
is_paid=True,
|
||||
cancelled=False,
|
||||
deleted=False,
|
||||
active=True,
|
||||
status=SubscriptionStatus.ACTIVE,
|
||||
)
|
||||
.order_by("-grace_period_end_date")
|
||||
.first()
|
||||
) # Order by descending grace_period_end_date and take the first
|
||||
|
||||
if latest_subscription:
|
||||
# Check if we're within the grace period
|
||||
if today <= latest_subscription.grace_period_end_date:
|
||||
subscription_status["has_active_subscription"] = (
|
||||
today <= latest_subscription.end_date
|
||||
)
|
||||
subscription_status["in_grace_period"] = (
|
||||
latest_subscription.end_date
|
||||
< today
|
||||
<= latest_subscription.grace_period_end_date
|
||||
)
|
||||
subscription_status["grace_period_end_date"] = (
|
||||
latest_subscription.grace_period_end_date
|
||||
)
|
||||
|
||||
return subscription_status
|
||||
def get_principal_type_name(self, obj):
|
||||
return obj.principal_type.name if obj.principal_type else None
|
||||
|
||||
def to_representation(self, instance):
|
||||
data = super().to_representation(instance)
|
||||
@@ -274,7 +205,6 @@ class ProfileExtendedDataSerializer(serializers.ModelSerializer):
|
||||
has_active_subscription = serializers.SerializerMethodField(read_only=True)
|
||||
preference = serializers.SerializerMethodField(read_only=True)
|
||||
register_complete = serializers.BooleanField(read_only=True)
|
||||
is_active = serializers.BooleanField(read_only=True)
|
||||
going_events_count = serializers.SerializerMethodField(read_only=True)
|
||||
interested_events_count = serializers.SerializerMethodField(read_only=True)
|
||||
feature_limit = serializers.SerializerMethodField(read_only=True)
|
||||
@@ -287,7 +217,6 @@ class ProfileExtendedDataSerializer(serializers.ModelSerializer):
|
||||
"register_complete",
|
||||
"has_active_subscription",
|
||||
"preference",
|
||||
"is_active",
|
||||
"going_events_count",
|
||||
"interested_events_count",
|
||||
"feature_limit"
|
||||
|
||||
Reference in New Issue
Block a user