Skip to content

Commit

Permalink
The image and file fields where nolonger working.
Browse files Browse the repository at this point in the history
This PR restores that functionality.
  • Loading branch information
specialunderwear committed Mar 18, 2024
1 parent 8ea9bf5 commit 425469c
Show file tree
Hide file tree
Showing 3 changed files with 15 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
7 changes: 7 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 All @@ -63,6 +69,7 @@ class AttributeConverter(AttributeFieldBase):
def __init__(self, context):
self.context = context
self.errors = []
self.parent = None

def fail(self, key, **kwargs):
"""
Expand Down

0 comments on commit 425469c

Please sign in to comment.