Skip to content

Commit

Permalink
The image and file fields where nolonger working. (#337)
Browse files Browse the repository at this point in the history
* The image and file fields where nolonger working.

This PR restores that functionality.

* nergens voor
  • Loading branch information
specialunderwear authored Mar 18, 2024
1 parent 8ea9bf5 commit f3f041e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions oscarapi/serializers/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ def to_representation(self, value):
return value.value.option
elif obj_type == value.attribute.MULTI_OPTION:
return value.value.values_list("option", flat=True)
elif obj_type == value.attribute.FILE:
return value.value.url
elif obj_type == value.attribute.IMAGE:
return value.value.url
elif obj_type in [value.attribute.FILE, value.attribute.IMAGE]:
url = value.value.url
request = self.context.get("request", None)
if request is not None:
url = request.build_absolute_uri(url)
return url
elif obj_type == value.attribute.ENTITY:
if hasattr(value.value, "json"):
return value.value.json()
Expand Down
2 changes: 2 additions & 0 deletions oscarapi/serializers/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ def shortcut_to_internal_value(self, data, productclass, attributes):
ProductAttribute.DATE,
ProductAttribute.DATETIME,
ProductAttribute.ENTITY,
ProductAttribute.FILE,
ProductAttribute.IMAGE,
]
)
}
Expand Down
6 changes: 6 additions & 0 deletions oscarapi/utils/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework.fields import MISSING_ERROR_MESSAGE
from rest_framework.exceptions import ErrorDetail
from oscarapi.utils.loading import get_api_class
from oscarapi.serializers import fields as oscarapi_fields

attribute_details = operator.itemgetter("code", "value")
entity_internal_value = get_api_class("serializers.hooks", "entity_internal_value")
Expand Down Expand Up @@ -55,6 +56,11 @@ def to_attribute_type_value(self, attribute, code, value):
internal_value = date_field.to_internal_value(value)
elif attribute.type == attribute.ENTITY:
internal_value = entity_internal_value(attribute, value)
elif attribute.type in [attribute.IMAGE, attribute.FILE]:
image_field = oscarapi_fields.ImageUrlField()
# pylint: disable=protected-access
image_field._context = self.context
internal_value = image_field.to_internal_value(value)

return internal_value

Expand Down

0 comments on commit f3f041e

Please sign in to comment.