Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add alt field to Category background image
fowczarekb541916ee672
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.pyindex cc11a101d3..6a457313ad 100644--- a/saleor/graphql/product/types.py+++ b/saleor/graphql/product/types.py@@ -99,6 +99,17 @@ def resolve_attribute_value_type(attribute_value):return AttributeValueType.STRING+def resolve_background_image(background_image, alt, size, info):+ if background_image:+ if size:+ url = get_thumbnail(background_image, size, method='thumbnail')+ else:+ url = background_image.url+ url = info.context.build_absolute_uri(url)+ return Image(url, alt)+ return None++class AttributeValue(CountableDjangoObjectType):name = graphene.String(description=AttributeValueDescriptions.NAME)slug = graphene.String(description=AttributeValueDescriptions.SLUG)@@ -417,15 +428,8 @@ class Collection(CountableDjangoObjectType):model = models.Collectiondef resolve_background_image(self, info, size=None, **kwargs):- if self.background_image:- if size:- url = get_thumbnail(- self.background_image, size, method='thumbnail')- else:- url = self.background_image.url- url = info.context.build_absolute_uri(url)- return Image(url, self.background_image_alt)- return None+ return resolve_background_image(+ self.background_image, self.background_image_alt, size, info)def resolve_products(self, info, **kwargs):if hasattr(self, 'prefetched_products'):@@ -447,7 +451,8 @@ class Category(CountableDjangoObjectType):children = PrefetchingConnectionField(lambda: Category,description='List of children of the category.')- background_image = graphene.Field(Image)+ background_image = graphene.Field(+ Image, size=graphene.Int(description='Size of the image'))class Meta:description = dedent("""Represents a single category of products.@@ -463,8 +468,9 @@ class Category(CountableDjangoObjectType):qs = self.get_ancestors()return gql_optimizer.query(qs, info)- def resolve_background_image(self, info, **kwargs):- return self.background_image or None+ def resolve_background_image(self, info, size=None, **kwargs):+ return resolve_background_image(+ self.background_image, self.background_image_alt, size, info)def resolve_children(self, info, **kwargs):qs = self.children.all()diff --git a/saleor/product/models.py b/saleor/product/models.pyindex c119932a6e..a4a058a14e 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -39,6 +39,7 @@ class Category(MPTTModel, SeoModel):on_delete=models.CASCADE)background_image = VersatileImageField(upload_to='category-backgrounds', blank=True, null=True)+ background_image_alt = models.CharField(max_length=128, blank=True)objects = models.Manager()tree = TreeManager()diff --git a/tests/api/test_category.py b/tests/api/test_category.pyindex 2e3a7bc3a8..6559996b6e 100644--- a/tests/api/test_category.py+++ b/tests/api/test_category.py@@ -330,8 +330,9 @@ FETCH_CATEGORY_QUERY = """query fetchCategory($id: ID!){category(id: $id) {name- backgroundImage {- url(size: 120)+ backgroundImage(size: 120) {+ url+ alt}}}@@ -339,9 +340,11 @@ query fetchCategory($id: ID!){def test_category_image_query(user_api_client, non_default_category):+ alt_text = 'Alt text for an image.'category = non_default_categoryimage_file, image_name = create_image()category.background_image = image_file+ category.background_image_alt = alt_textcategory.save()category_id = graphene.Node.to_global_id('Category', category.pk)variables = {'id': category_id}@@ -350,6 +353,7 @@ def test_category_image_query(user_api_client, non_default_category):data = content['data']['category']thumbnail_url = category.background_image.thumbnail['120x120'].urlassert thumbnail_url in data['backgroundImage']['url']+ assert data['backgroundImage']['alt'] == alt_textdef test_category_image_query_without_associated_file(