Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #144 -- Improve DRM serialization performance by filtering source and ratio #148

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions pictures/contrib/rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,45 @@
class PictureField(serializers.ReadOnlyField):
"""Read-only field for all aspect ratios and sizes of the image."""

def __init__(self, **kwargs):
self.aspect_ratio = kwargs.pop("aspect_ratio", None)
self.image_source = kwargs.pop("image_source", None)
amureki marked this conversation as resolved.
Show resolved Hide resolved
super().__init__(**kwargs)

def to_representation(self, obj: PictureFieldFile):
if not obj:
return None
payload = {
"url": obj.url,
"width": obj.width,
"height": obj.height,
"ratios": {
ratio: {
"sources": {
f"image/{file_type.lower()}": sizes
for file_type, sizes in sources.items()
},
}
for ratio, sources in obj.aspect_ratios.items()
},
}
if self.aspect_ratio and self.image_source:
codingjoe marked this conversation as resolved.
Show resolved Hide resolved
payload = {

Check warning on line 31 in pictures/contrib/rest_framework.py

View check run for this annotation

Codecov / codecov/patch

pictures/contrib/rest_framework.py#L31

Added line #L31 was not covered by tests
"url": obj.url,
"width": obj.width,
"height": obj.height,
amureki marked this conversation as resolved.
Show resolved Hide resolved
"ratios": {
self.aspect_ratio: {
"sources": {
f"image/{self.image_source.lower()}": obj.aspect_ratios[
self.aspect_ratio
][self.image_source]
}
}
},
}
else:
payload = {
"url": obj.url,
"width": obj.width,
"height": obj.height,
"ratios": {
ratio: {
"sources": {
f"image/{file_type.lower()}": sizes
for file_type, sizes in sources.items()
},
}
for ratio, sources in obj.aspect_ratios.items()
},
}

try:
query_params: QueryDict = self.context["request"].GET
except KeyError:
Expand Down