Skip to content

Commit

Permalink
custom profile image field
Browse files Browse the repository at this point in the history
  • Loading branch information
Lvyshnevska committed Jun 23, 2024
1 parent 69dc59d commit c52ef82
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 70 deletions.
93 changes: 39 additions & 54 deletions BackEnd/profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
ViewedCompany,
Region,
)
from images.models import ProfileImage
from utils.regions_ukr_names import get_regions_ukr_names_as_string


Expand All @@ -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
Expand All @@ -57,8 +73,8 @@ class Meta:
"founded",
"categories",
"activities",
"banner_approved_image",
"logo_approved_image",
"banner",
"logo",
"is_saved",
)

Expand All @@ -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
Expand All @@ -112,8 +124,8 @@ class Meta:
"activities",
"service_info",
"product_info",
"banner_approved_image",
"logo_approved_image",
"banner",
"logo",
"is_saved",
)
read_only_fields = (
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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 = (
Expand All @@ -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",
)

Expand All @@ -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
Expand Down Expand Up @@ -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",)
Expand Down
44 changes: 28 additions & 16 deletions BackEnd/search/serializers.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -33,8 +49,8 @@ class Meta:
"regions_ukr_display",
"founded",
"address",
"banner_approved_image",
"logo_approved_image",
"banner",
"logo",
"person",
"is_saved",
)
Expand All @@ -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
Expand All @@ -76,8 +88,8 @@ class Meta:
"product_info",
"founded",
"address",
"banner_approved_image",
"logo_approved_image",
"banner",
"logo",
"person",
)

Expand Down

0 comments on commit c52ef82

Please sign in to comment.