Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add alt field to collection background image
fowczarekba1189a81222
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.pyindex 36229b0850..cc11a101d3 100644--- a/saleor/graphql/product/types.py+++ b/saleor/graphql/product/types.py@@ -245,19 +245,12 @@ class ProductAvailability(graphene.ObjectType):class Image(graphene.ObjectType):url = graphene.String(required=True,- description='The URL of the image.',- size=graphene.Int(description='Size of the image'))+ description='The URL of the image.')+ alt = graphene.String(description='Alt text for an image.')class Meta:description = 'Represents an image.'- def resolve_url(self, info, size=None):- if size:- url = get_thumbnail(self, size, method='thumbnail')- else:- url = self.url- return info.context.build_absolute_uri(url)-class Product(CountableDjangoObjectType):url = graphene.String(@@ -413,16 +406,26 @@ class Collection(CountableDjangoObjectType):PrefetchingConnectionField(Product, description='List of products in this collection.'),prefetch_related=prefetch_products)- background_image = graphene.Field(Image)+ background_image = graphene.Field(+ Image, size=graphene.Int(description='Size of the image'))class Meta:description = "Represents a collection of products."- exclude_fields = ['voucher_set', 'sale_set', 'menuitem_set']+ exclude_fields = [+ 'voucher_set', 'sale_set', 'menuitem_set', 'background_image_alt']interfaces = [relay.Node]model = models.Collection- def resolve_background_image(self, info, **kwargs):- return self.background_image or None+ def 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 Nonedef resolve_products(self, info, **kwargs):if hasattr(self, 'prefetched_products'):diff --git a/saleor/product/models.py b/saleor/product/models.pyindex 5391cbd34d..c119932a6e 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -452,6 +452,7 @@ class Collection(SeoModel):Product, blank=True, related_name='collections')background_image = VersatileImageField(upload_to='collection-backgrounds', blank=True, null=True)+ background_image_alt = models.CharField(max_length=128, blank=True)is_published = models.BooleanField(default=False)description = models.TextField(blank=True)diff --git a/tests/api/test_collection.py b/tests/api/test_collection.pyindex 49721e5044..9644149b29 100644--- a/tests/api/test_collection.py+++ b/tests/api/test_collection.py@@ -323,8 +323,9 @@ FETCH_COLLECTION_QUERY = """query fetchCollection($id: ID!){collection(id: $id) {name- backgroundImage {+ backgroundImage(size: 120) {url+ alt}}}@@ -332,42 +333,26 @@ FETCH_COLLECTION_QUERY = """def test_collection_image_query(user_api_client, collection):- query = """- query fetchCollection($id: ID!){- collection(id: $id) {- backgroundImage {- url(size: 120)- }- }- }- """+ alt_text = 'Alt text for an image.'image_file, image_name = create_image()collection.background_image = image_file+ collection.background_image_alt = alt_textcollection.save()collection_id = graphene.Node.to_global_id('Collection', collection.pk)variables = {'id': collection_id}- response = user_api_client.post_graphql(query, variables)+ response = user_api_client.post_graphql(FETCH_COLLECTION_QUERY, variables)content = get_graphql_content(response)data = content['data']['collection']thumbnail_url = collection.background_image.thumbnail['120x120'].urlassert thumbnail_url in data['backgroundImage']['url']+ assert data['backgroundImage']['alt'] == alt_textdef test_collection_image_query_without_associated_file(user_api_client, collection):- query = """- query fetchCollection($id: ID!){- collection(id: $id) {- name- backgroundImage {- url- }- }- }- """collection_id = graphene.Node.to_global_id('Collection', collection.pk)variables = {'id': collection_id}- response = user_api_client.post_graphql(query, variables)+ response = user_api_client.post_graphql(FETCH_COLLECTION_QUERY, variables)content = get_graphql_content(response)data = content['data']['collection']assert data['name'] == collection.name