Skip to content

Commit

Permalink
Enhanced product serializer with a to_representation method to fetch …
Browse files Browse the repository at this point in the history
…Cloudinary URLs
  • Loading branch information
Nathaniel81 committed May 15, 2024
1 parent d87da8d commit 8ecfe50
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
35 changes: 24 additions & 11 deletions backend/store/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from rest_framework import serializers
from store.models import Product, Review, Order, OrderItem, ShippingAddress, Category
from accounts.models import User
from cloudinary.utils import cloudinary_url



class ReviewSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -13,23 +16,26 @@ class Meta:
fields = '__all__'

class ProductSerializer(serializers.ModelSerializer):
additional_images = serializers.SerializerMethodField()
reviews = serializers.SerializerMethodField(read_only=True)
category = serializers.StringRelatedField()

class Meta:
model = Product
exclude = ['image_1', 'image_2', 'image_3']

def get_additional_images(self, obj):
additional_images = [
obj.main_image.url if obj.main_image and obj.main_image.url != '/media/no-image.png' else None,
obj.image_1.url if obj.image_1 and obj.image_1.url != '/media/no-image.png' else None,
obj.image_2.url if obj.image_2 and obj.image_2.url != '/media/no-image.png' else None,
obj.image_3.url if obj.image_3 and obj.image_3.url != '/media/no-image.png' else None,
]
exclude = ['main_image', 'image_1', 'image_2', 'image_3']

return list(filter(None, additional_images))
def to_representation(self, instance):
representation = super().to_representation(instance)
images = []
if instance.main_image:
images.append(cloudinary_url(instance.main_image.public_id)[0])
if instance.image_1:
images.append(cloudinary_url(instance.image_1.public_id)[0])
if instance.image_2:
images.append(cloudinary_url(instance.image_2.public_id)[0])
if instance.image_3:
images.append(cloudinary_url(instance.image_3.public_id)[0])
representation['images'] = images
return representation

def get_reviews(self, obj):
reviews = obj.reviews.all()
Expand All @@ -39,6 +45,7 @@ def get_reviews(self, obj):
class UserSerializer(serializers.ModelSerializer):
name = serializers.SerializerMethodField(read_only=True)
isAdmin = serializers.SerializerMethodField(read_only=True)

class Meta:
model = User
fields = ['id', 'username', 'email', 'name', 'isAdmin']
Expand All @@ -59,6 +66,12 @@ class OrderItemSerializer(serializers.ModelSerializer):
class Meta:
model = OrderItem
fields = '__all__'

def to_representation(self, instance):
representation = super().to_representation(instance)
if instance.image:
representation['image'] = cloudinary_url(instance.image.public_id)[0]
return representation

class OrderSerializer(serializers.ModelSerializer):
orderItems = serializers.SerializerMethodField(read_only=True)
Expand Down
31 changes: 6 additions & 25 deletions backend/store/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class CategoryTestCase(APITestCase):
def setUp(self):
self.category = Category.objects.create(name='Electronics', image='electronics-img.png')
self.category = Category.objects.create(name='Electronics')
self.assertEqual(Category.objects.count(), 1)

def test_category_str_method(self):
Expand All @@ -19,7 +19,7 @@ def test_category_str_method(self):
@override_settings(MEDIA_URL='http://testserver')
class ProductTestCase(APITestCase):
def setUp(self):
self.category = Category.objects.create(name='Electronics', image='electronics-img.png')
self.category = Category.objects.create(name='Electronics')
self.assertEqual(Category.objects.count(), 1)
self.product = Product.objects.create(
name='SmartPhone',
Expand Down Expand Up @@ -48,7 +48,6 @@ def test_product_str_method(self):
def test_product_list_view(self):
response = self.client.get(reverse('products'), {'keyword': 'Product 1'})
self.assertEqual(response.status_code, 200)
# self.assertEqual(response.data['products'], ProductSerializer([self.product2, self.product, self.product1, ], many=True).data)

def test_product_list_view_with_search(self):
response = self.client.get(reverse('products'), {'keyword': 'Product 1'})
Expand Down Expand Up @@ -151,16 +150,6 @@ def test_create_duplicate_product_review(self):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data, {'detail': 'Product already reviewed'})

# def test_create_invalid_rating_product_review(self):
# self.client.force_authenticate(user=self.admin_user)
# url = reverse("create-review", kwargs={'pk': self.product1.id})
# data = {
# 'rating': 0,
# 'comment': 'Invalid rating',
# }
# response = self.client.post(url, data)
# self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
# self.assertEqual(response.data, {'detail': 'Please select a rating'})

class ReviewTestCase(APITestCase):
def setUp(self):
Expand All @@ -171,7 +160,7 @@ def setUp(self):
last_name='Doe',
address='2436 Naples Avenue, Panama City FL 32405'
)
self.category = Category.objects.create(name='Electronics', image='electronics-img.png')
self.category = Category.objects.create(name='Electronics')
self.product = Product.objects.create(
name='SmartPhone',
main_image='smartphone-img.png',
Expand Down Expand Up @@ -216,7 +205,7 @@ def test_order_str_method(self):

class OrderItemTestCase(APITestCase):
def setUp(self):
self.category = Category.objects.create(name='Electronics', image='electronics-img.png')
self.category = Category.objects.create(name='Electronics')
self.user = User.objects.create(
username='Jhon',
password='jhon'
Expand Down Expand Up @@ -336,7 +325,7 @@ def test_shipping_address_str_method(self):

class ProductSerializerTestCase(APITestCase):
def setUp(self):
self.category = Category.objects.create(name='Electronics', image='electronics-img.png')
self.category = Category.objects.create(name='Electronics')
self.user = User.objects.create(
username='Jhon',
password='jhonD#3'
Expand All @@ -357,15 +346,7 @@ def setUp(self):
)
self.review = Review.objects.create(user=self.user, product=self.product, rating=5, comment='Great product!')
self.serializer = ProductSerializer(instance=self.product)

def test_get_additional_images(self):
expected_images = [
self.product.main_image.url,
self.product.image_1.url,
self.product.image_2.url,
self.product.image_3.url,
]
self.assertEqual(self.serializer.get_additional_images(self.product), expected_images)


def test_get_reviews(self):
expected_reviews_data = ReviewSerializer(self.product.reviews.all(), many=True).data
Expand Down

0 comments on commit 8ecfe50

Please sign in to comment.