From c52ef825d739a3214fcd82afe472cdf284b53adc Mon Sep 17 00:00:00 2001 From: Lvyshnevska Date: Sun, 23 Jun 2024 15:16:10 +0200 Subject: [PATCH] custom profile image field --- BackEnd/profiles/serializers.py | 93 ++++++++++++++------------------- BackEnd/search/serializers.py | 44 ++++++++++------ 2 files changed, 67 insertions(+), 70 deletions(-) diff --git a/BackEnd/profiles/serializers.py b/BackEnd/profiles/serializers.py index 4478fa484..1e66de416 100644 --- a/BackEnd/profiles/serializers.py +++ b/BackEnd/profiles/serializers.py @@ -7,6 +7,7 @@ ViewedCompany, Region, ) +from images.models import ProfileImage from utils.regions_ukr_names import get_regions_ukr_names_as_string @@ -28,18 +29,33 @@ class Meta: fields = "__all__" +class ProfileImageField(serializers.Field): + + def to_representation(self, value): + if value.is_deleted == False: + return { + "uuid": value.uuid, + "path": self.context["request"].build_absolute_uri( + value.image_path.url + ), + } + return None + + def to_internal_value(self, data): + profile_image = ProfileImage.objects.filter( + uuid=data, is_deleted=False + ).first() + return profile_image + + class ProfileListSerializer(serializers.ModelSerializer): activities = ActivitySerializer(many=True, read_only=True) categories = CategorySerializer(many=True, read_only=True) is_saved = serializers.SerializerMethodField() regions = RegionSerializer(many=True, read_only=True) regions_ukr_display = serializers.SerializerMethodField() - banner_approved_image = serializers.ImageField( - source="banner_approved.image_path", read_only=True, required=False - ) - logo_approved_image = serializers.ImageField( - source="logo_approved.image_path", read_only=True, required=False - ) + banner = ProfileImageField(source="banner_approved", read_only=True) + logo = ProfileImageField(source="logo_approved", read_only=True) class Meta: model = Profile @@ -57,8 +73,8 @@ class Meta: "founded", "categories", "activities", - "banner_approved_image", - "logo_approved_image", + "banner", + "logo", "is_saved", ) @@ -84,12 +100,8 @@ class ProfileDetailSerializer(serializers.ModelSerializer): is_saved = serializers.SerializerMethodField() regions = RegionSerializer(many=True, read_only=True) regions_ukr_display = serializers.SerializerMethodField() - banner_approved_image = serializers.ImageField( - source="banner_approved.image_path", required=False - ) - logo_approved_image = serializers.ImageField( - source="logo_approved.image_path", required=False - ) + banner = ProfileImageField(source="banner_approved", read_only=True) + logo = ProfileImageField(source="logo_approved", read_only=True) class Meta: model = Profile @@ -112,8 +124,8 @@ class Meta: "activities", "service_info", "product_info", - "banner_approved_image", - "logo_approved_image", + "banner", + "logo", "is_saved", ) read_only_fields = ( @@ -135,8 +147,8 @@ class Meta: "activities", "service_info", "product_info", - "banner_approved_image", - "logo_approved_image", + "banner", + "logo", ) def get_is_saved(self, obj) -> bool: @@ -150,23 +162,14 @@ def get_regions_ukr_display(self, obj) -> str: class ProfileOwnerDetailViewSerializer(serializers.ModelSerializer): + email = serializers.ReadOnlyField(source="person.email") activities = ActivitySerializer(many=True, read_only=True) categories = CategorySerializer(many=True, read_only=True) email = serializers.ReadOnlyField(source="person.email") regions = RegionSerializer(many=True, read_only=True) regions_ukr_display = serializers.SerializerMethodField() - banner_image = serializers.ImageField( - source="banner.image_path", required=False - ) - logo_image = serializers.ImageField( - source="logo.image_path", required=False - ) - banner_approved_image = serializers.ImageField( - source="banner_approved.image_path", required=False - ) - logo_approved_image = serializers.ImageField( - source="logo_approved.image_path", required=False - ) + banner = ProfileImageField() + logo = ProfileImageField() class Meta: model = Profile @@ -193,10 +196,8 @@ class Meta: "product_info", "address", "startup_idea", - "banner_image", - "logo_image", - "banner_approved_image", - "logo_approved_image", + "banner", + "logo", "is_deleted", ) read_only_fields = ( @@ -222,10 +223,8 @@ class Meta: "product_info", "address", "startup_idea", - "banner_image", - "logo_image", - "banner_approved_image", - "logo_approved_image", + "banner", + "logo", "is_deleted", ) @@ -235,18 +234,8 @@ def get_regions_ukr_display(self, obj) -> str: class ProfileOwnerDetailEditSerializer(serializers.ModelSerializer): email = serializers.ReadOnlyField(source="person.email") - banner_image = serializers.ImageField( - source="banner.image_path", required=False - ) - logo_image = serializers.ImageField( - source="logo.image_path", required=False - ) - banner_approved_image = serializers.ImageField( - source="banner_approved.image_path", required=False - ) - logo_approved_image = serializers.ImageField( - source="logo_approved.image_path", required=False - ) + banner = ProfileImageField() + logo = ProfileImageField() class Meta: model = Profile @@ -274,10 +263,6 @@ class Meta: "startup_idea", "banner", "logo", - "banner_image", - "banner_approved_image", - "logo_image", - "logo_approved_image", "is_deleted", ) read_only_fields = ("person",) diff --git a/BackEnd/search/serializers.py b/BackEnd/search/serializers.py index d4fcfe329..33b666a08 100644 --- a/BackEnd/search/serializers.py +++ b/BackEnd/search/serializers.py @@ -1,6 +1,7 @@ from rest_framework import serializers from profiles.models import Profile +from images.models import ProfileImage from profiles.serializers import ( CategorySerializer, ActivitySerializer, @@ -9,18 +10,33 @@ from utils.regions_ukr_names import get_regions_ukr_names_as_string +class ProfileImageField(serializers.Field): + + def to_representation(self, value): + if value.is_deleted == False: + return { + "uuid": value.uuid, + "path": self.context["request"].build_absolute_uri( + value.image_path.url + ), + } + return None + + def to_internal_value(self, data): + profile_image = ProfileImage.objects.filter( + uuid=data, is_deleted=False + ).first() + return profile_image + + class CompanySerializers(serializers.ModelSerializer): categories = CategorySerializer(many=True, read_only=True) activities = ActivitySerializer(many=True, read_only=True) is_saved = serializers.SerializerMethodField() regions = RegionSerializer(many=True, read_only=True) regions_ukr_display = serializers.SerializerMethodField() - banner_approved_image = serializers.ImageField( - source="banner_approved.image_path", required=False, read_only=True - ) - logo_approved_image = serializers.ImageField( - source="logo_approved.image_path", required=False, read_only=True - ) + banner = ProfileImageField(source="banner_approved", read_only=True) + logo = ProfileImageField(source="logo_approved", read_only=True) class Meta: model = Profile @@ -33,8 +49,8 @@ class Meta: "regions_ukr_display", "founded", "address", - "banner_approved_image", - "logo_approved_image", + "banner", + "logo", "person", "is_saved", ) @@ -54,12 +70,8 @@ class CompanyAdvancedSerializers(serializers.ModelSerializer): activities = ActivitySerializer(many=True, read_only=True) regions = RegionSerializer(many=True, read_only=True) regions_ukr_display = serializers.SerializerMethodField() - banner_approved_image = serializers.ImageField( - source="banner_approved.image_path", required=False, read_only=True - ) - logo_approved_image = serializers.ImageField( - source="logo_approved.image_path", required=False, read_only=True - ) + banner = ProfileImageField(source="banner_approved", read_only=True) + logo = ProfileImageField(source="logo_approved", read_only=True) class Meta: model = Profile @@ -76,8 +88,8 @@ class Meta: "product_info", "founded", "address", - "banner_approved_image", - "logo_approved_image", + "banner", + "logo", "person", )