Skip to content

Commit 9eb8742

Browse files
ashutoshkrrissherwinski
authored andcommitted
feat(urlbuilder): customize target device pixel ratios
1 parent eaac447 commit 9eb8742

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

imgix/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ class WidthToleranceError(Error):
2727

2828
class VariableQualitiesError(Error):
2929
"""Exception raised for an invalid variable qualities."""
30+
31+
32+
class DevicePixelRatiosError(Error):
33+
"""Exception raised for an invalid device pixel ratios."""

imgix/urlbuilder.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from base64 import urlsafe_b64encode
1010
from urllib.parse import quote_plus, quote
1111
from .validators import (
12+
validate_device_pixel_ratios,
1213
validate_min_max_tol,
1314
validate_variable_qualities,
1415
validate_widths,
@@ -230,7 +231,7 @@ def create_srcset(self, path, params={}, options={}, **kwargs):
230231
options: dict, optional
231232
Options that will be used to generate the srcset,
232233
including 'disable_path_encoding', 'variable_qualities',
233-
{} by default.
234+
'device pixel ratios', {} by default.
234235
start : int, optional
235236
Starting minimum width value, MIN_WIDTH by default.
236237
stop : int, optional
@@ -335,6 +336,11 @@ def _build_srcset_DPR(
335336

336337
qualities = {**DPR_QUALITIES, **variable_qualities}
337338

339+
device_pixel_ratios = options.get("device_pixel_ratios", [])
340+
if device_pixel_ratios:
341+
validate_device_pixel_ratios(device_pixel_ratios)
342+
targets = device_pixel_ratios
343+
338344
for dpr in targets:
339345
srcset_params["dpr"] = dpr
340346

imgix/validators.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .errors import (
2+
DevicePixelRatiosError,
23
VariableQualitiesError,
34
WidthRangeError,
45
WidthToleranceError,
@@ -208,3 +209,31 @@ def validate_variable_qualities(variable_qualities):
208209
"""
209210
if not isinstance(variable_qualities, dict):
210211
raise VariableQualitiesError("`variable_qualities` must be a `dict`")
212+
213+
214+
def validate_device_pixel_ratios(device_pixel_ratios):
215+
"""Validate a list of `device_pixel_ratios`.
216+
217+
This function checks whether `device_pixel_ratios` is a list.
218+
219+
220+
Parameters
221+
----------
222+
device_pixel_ratios : list
223+
A list of device pixel ratios.
224+
225+
Raises
226+
------
227+
DevicePixelRatiosError
228+
If `device_pixel_ratios` is not a list, raise.
229+
"""
230+
if not isinstance(device_pixel_ratios, list):
231+
raise DevicePixelRatiosError("`device_pixel_ratios` must be a `list`")
232+
else:
233+
all_valid_dpr = False
234+
for dpr in device_pixel_ratios:
235+
all_valid_dpr = isinstance(dpr, int) and dpr >= 1 and dpr <= 5
236+
237+
if not all_valid_dpr:
238+
raise DevicePixelRatiosError("`device_pixel_ratios` can only \
239+
contain positive integer values between 1 and 5")

0 commit comments

Comments
 (0)