Skip to content

Commit bf17ba3

Browse files
Kandeel4411Aaron Suarez
authored andcommitted
Added 'paid' algolia filtering to search endpoint (#219)
* Added 'paid' algolia filtering to search endpoint * Added tests & documentation for 'paid' filtering in search endpoint
1 parent 1dd0cf2 commit bf17ba3

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

app/api/routes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,22 @@ def search_results():
243243
page = request.args.get('page', 0, int)
244244
page_size = request.args.get('page_size', Config.RESOURCE_PAGINATOR.per_page, int)
245245

246+
# Fetch the filter params from the url, if they were provided.
247+
paid = request.args.get('paid')
248+
filters = ''
249+
250+
# Filter on paid
251+
if isinstance(paid, str):
252+
paid = paid.lower()
253+
# algolia filters boolean attributes with either 0 or 1
254+
if paid == 'true':
255+
filters += 'paid=1'
256+
elif paid == 'false':
257+
filters += 'paid=0'
258+
246259
try:
247260
search_result = index.search(f'{term}', {
261+
'filters': filters,
248262
'page': page,
249263
'hitsPerPage': page_size
250264
})

app/static/openapi.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ paths:
9191
schema:
9292
type: string
9393
required: true
94+
- in: query
95+
name: paid
96+
required: false
97+
description: Whether the data is paid or not. To search for *free* resources, make a `GET` request to `/search?paid=false`.
98+
schema:
99+
type: boolean
94100
responses:
95101
200:
96102
description: Search results
@@ -479,12 +485,9 @@ paths:
479485
- in: query
480486
name: paid
481487
required: false
482-
description: Whether the resource is paid or not. To search for *FREE* resources, make a `GET` request to `/resources?paid=false`.
488+
description: Whether the resource is paid or not. To search for *free* resources, make a `GET` request to `/resources?paid=false`.
483489
schema:
484490
type: boolean
485-
enum:
486-
- true
487-
- false
488491
- in: query
489492
name: language
490493
required: false

tests/unit/test_routes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,22 @@ def test_search(module_client, module_db, fake_auth_from_oc, fake_algolia_save,
594594
assert (result.json['data'][0]['url'] == resource.json['data'].get('url'))
595595

596596

597+
def test_search_paid_filter(module_client, module_db, fake_auth_from_oc, fake_algolia_save, fake_algolia_search):
598+
client = module_client
599+
600+
# Test on the unpaid resources
601+
result = client.get("/api/v1/search?paid=false")
602+
assert (result.status_code == 200)
603+
604+
# Test on the paid resources
605+
result = client.get("/api/v1/search?paid=true")
606+
assert (result.status_code == 200)
607+
608+
# Test with invalid paid attribute given ( defaults to all )
609+
result = client.get("/api/v1/search?paid=something")
610+
assert (result.status_code == 200)
611+
612+
597613
def test_algolia_exception_error(module_client, module_db, fake_auth_from_oc, fake_algolia_exception):
598614
client = module_client
599615
first_term = random_string()

0 commit comments

Comments
 (0)