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

Additional case handling for color parameter in search API #22692

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions src/olympia/search/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,13 @@ def convert_to_hsl(self, hexvalue):
hexvalue = ''.join(2 * c for c in hexvalue)
try:
rgb = tuple(bytearray.fromhex(hexvalue))
except ValueError:
rgb = (0, 0, 0)
except ValueError as err:
raise ValueError(gettext('Expected a hex string color code.')) from err
return colorgram.colorgram.hsl(*rgb)

def get_value(self):
color = self.query_data.get(self.query_param, '')
return self.convert_to_hsl(color.upper().lstrip('#'))
return self.convert_to_hsl(color.upper().lstrip('#')[:6]) if color else None

def get_es_query(self):
# Thresholds for saturation & luminosity that dictate which query to
Expand All @@ -457,6 +457,9 @@ def get_es_query(self):
HIGH_LUMINOSITY = 255 * 98 / 100.0

hsl = self.get_value()
if not hsl:
return []

if hsl[1] <= LOW_SATURATION:
# If we're given a color with a very low saturation, the user is
# searching for a black/white/grey and we need to take saturation
Expand Down
17 changes: 17 additions & 0 deletions src/olympia/search/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,18 @@ def test_search_by_color(self):
{'range': {'colors.ratio': {'gte': 0.25}}},
]

qs = self._filter(data={'color': '#00ffffalotofjunk'})
filter_ = qs['query']['bool']['filter']
assert len(filter_) == 1
inner = filter_[0]['nested']['query']['bool']['filter']
assert len(inner) == 4
assert inner == [
{'range': {'colors.s': {'gt': 6.375}}},
{'range': {'colors.l': {'gt': 12.75, 'lt': 249.9}}},
{'range': {'colors.h': {'gte': 101, 'lte': 153}}},
{'range': {'colors.ratio': {'gte': 0.25}}},
]

def test_search_by_color_grey(self):
qs = self._filter(data={'color': '#f6f6f6'})
filter_ = qs['query']['bool']['filter']
Expand All @@ -1059,6 +1071,11 @@ def test_search_by_color_grey(self):
{'range': {'colors.ratio': {'gte': 0.25}}},
]

def test_search_by_color_invalid(self):
with self.assertRaises(serializers.ValidationError) as context:
self._filter(data={'color': '#gggggg'})
assert context.exception.detail == ['Expected a hex string color code.']

def test_search_by_color_luminosity_extremes(self):
qs = self._filter(data={'color': '080603'})
filter_ = qs['query']['bool']['filter']
Expand Down
Loading