Skip to content

Commit

Permalink
escape tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak committed Dec 28, 2023
1 parent afe656d commit d034053
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
4 changes: 3 additions & 1 deletion care/facility/api/viewsets/icd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework.viewsets import ViewSet

from care.facility.static_data.icd11 import ICD11
from care.utils.static_data.helpers import query_builder


class ICDViewSet(ViewSet):
Expand All @@ -17,9 +18,10 @@ def list(self, request):
limit = min(int(request.query_params.get("limit")), 20)
except (ValueError, TypeError):
limit = 20

Check warning on line 20 in care/facility/api/viewsets/icd.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/icd.py#L17-L20

Added lines #L17 - L20 were not covered by tests

query = []

Check warning on line 22 in care/facility/api/viewsets/icd.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/icd.py#L22

Added line #L22 was not covered by tests
if q := request.query_params.get("query"):
query = [ICD11.label % f"{'* '.join(q.strip().rsplit(maxsplit=3))}*"]
query.append(ICD11.label % query_builder(q))

Check warning on line 24 in care/facility/api/viewsets/icd.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/icd.py#L24

Added line #L24 was not covered by tests

result = FindQuery(expressions=query, model=ICD11, limit=limit).execute(

Check warning on line 26 in care/facility/api/viewsets/icd.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/icd.py#L26

Added line #L26 was not covered by tests
exhaust_results=False
Expand Down
12 changes: 6 additions & 6 deletions care/facility/api/viewsets/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from care.facility.static_data.medibase import MedibaseMedicine
from care.utils.filters.choicefilter import CareChoiceFilter
from care.utils.queryset.consultation import get_consultation_queryset
from care.utils.static_data.helpers import query_builder, token_escaper


def inverse_choices(choices):
Expand Down Expand Up @@ -163,14 +164,13 @@ def list(self, request):

query = []
if type := request.query_params.get("type"):
query = MedibaseMedicine.type == type
query.append(MedibaseMedicine.type == type)

Check warning on line 167 in care/facility/api/viewsets/prescription.py

View check run for this annotation

Codecov / codecov/patch

care/facility/api/viewsets/prescription.py#L167

Added line #L167 was not covered by tests

if search_query := request.query_params.get("query"):
q = (MedibaseMedicine.name == search_query) | (
MedibaseMedicine.vec
% f"{'* '.join(search_query.strip().rsplit(maxsplit=3))}*"
if q := request.query_params.get("query"):
query.append(
(MedibaseMedicine.name == token_escaper.escape(q))
| (MedibaseMedicine.vec % query_builder(q))
)
query = [query & q if query else q]

result = FindQuery(
expressions=query, model=MedibaseMedicine, limit=limit
Expand Down
3 changes: 2 additions & 1 deletion care/hcx/api/viewsets/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from care.hcx.utils.hcx import Hcx
from care.hcx.utils.hcx.operations import HcxOperations
from care.utils.queryset.communications import get_communications
from care.utils.static_data.helpers import query_builder


class HcxGatewayViewSet(GenericViewSet):
Expand Down Expand Up @@ -332,7 +333,7 @@ def pmjy_packages(self, request):

query = []

Check warning on line 334 in care/hcx/api/viewsets/gateway.py

View check run for this annotation

Codecov / codecov/patch

care/hcx/api/viewsets/gateway.py#L334

Added line #L334 was not covered by tests
if q := request.query_params.get("query"):
query = [PMJYPackage.vec % f"{'* '.join(q.strip().rsplit(maxsplit=3))}*"]
query.append(PMJYPackage.vec % query_builder(q))

Check warning on line 336 in care/hcx/api/viewsets/gateway.py

View check run for this annotation

Codecov / codecov/patch

care/hcx/api/viewsets/gateway.py#L336

Added line #L336 was not covered by tests

results = FindQuery(expressions=query, model=PMJYPackage, limit=limit).execute(

Check warning on line 338 in care/hcx/api/viewsets/gateway.py

View check run for this annotation

Codecov / codecov/patch

care/hcx/api/viewsets/gateway.py#L338

Added line #L338 was not covered by tests
exhaust_results=False
Expand Down
11 changes: 11 additions & 0 deletions care/utils/static_data/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from redis_om.model.token_escaper import TokenEscaper

token_escaper = TokenEscaper()


def query_builder(query: str) -> str:
"""
Builds a query for redis full text search from a given query string.
"""
words = query.strip().rsplit(maxsplit=3)
return f"{'* '.join([token_escaper.escape(word) for word in words])}*"

0 comments on commit d034053

Please sign in to comment.