Skip to content

Commit 7d1cca4

Browse files
Added extra small image in api
1 parent e9f9aac commit 7d1cca4

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

nxtbn/product/admin_types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ class ProductVariantNonPaginatedType(DjangoObjectType):
1212
display_name = graphene.String()
1313
humanize_price = graphene.String()
1414
variant_thumbnail = graphene.String()
15+
variant_thumbnail_xs = graphene.String()
1516

1617
def resolve_humanize_price(self, info):
1718
return self.humanize_total_price()
1819

1920
def resolve_variant_thumbnail(self, info):
2021
return self.variant_thumbnail(info.context)
22+
23+
def resolve_variant_thumbnail_xs(self, info):
24+
return self.variant_thumbnail_xs(info.context)
2125
class Meta:
2226
model = ProductVariant
2327
fields = (
@@ -46,12 +50,16 @@ class ProductGraphType(DjangoObjectType):
4650
db_id = graphene.Int(source="id")
4751
all_variants = graphene.List(ProductVariantNonPaginatedType)
4852
product_thumbnail = graphene.String()
53+
product_thumbnail_xs = graphene.String()
4954

5055
def resolve_all_variants(self, info):
5156
return self.variants.all()
5257

5358
def resolve_product_thumbnail(self, info):
5459
return self.product_thumbnail(info.context)
60+
61+
def resolve_product_thumbnail_xs(self, info):
62+
return self.product_thumbnail_xs(info.context)
5563
class Meta:
5664
model = Product
5765
fields = (

nxtbn/product/models.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,18 @@ def product_thumbnail(self, request):
221221
full_url = request.build_absolute_uri(image_url)
222222
return full_url
223223
return None
224-
224+
225+
def product_thumbnail_xs(self, request):
226+
"""
227+
Returns the URL of the first image associated with the product.
228+
If no image is available, returns None.
229+
"""
230+
first_image = self.images.first() # Get the first image if it exists
231+
if first_image and hasattr(first_image, 'image_xs') and first_image.image_xs:
232+
image_url = first_image.image_xs.url
233+
full_url = request.build_absolute_uri(image_url)
234+
return full_url
235+
return None
225236

226237

227238

@@ -385,6 +396,24 @@ def variant_thumbnail(self, request):
385396
full_url = request.build_absolute_uri(image_url)
386397
return
387398
return None
399+
400+
def variant_thumbnail_xs(self, request):
401+
"""
402+
Returns the URL of the first image associated with the product variant.
403+
If no image is available, returns None.
404+
"""
405+
if self.image and hasattr(self.image, 'image_xs') and self.image.image_xs:
406+
image_url = self.image.image_xs.url
407+
full_url = request.build_absolute_uri(image_url)
408+
return full_url
409+
410+
if self.product.images.exists():
411+
first_image = self.product.images.first()
412+
if first_image and hasattr(first_image, 'image_xs') and first_image.image_xs:
413+
image_url = first_image.image_xs.url
414+
full_url = request.build_absolute_uri(image_url)
415+
return
416+
return None
388417

389418

390419
def get_valid_stock(self): # stocks that available for sell

0 commit comments

Comments
 (0)