Skip to content

Commit

Permalink
Merge pull request #1727 from openhealthcare/search-field-setting
Browse files Browse the repository at this point in the history
Add a new setting OPAL_DEFAULT_SEARCH_FIELDS
  • Loading branch information
davidmiller authored May 20, 2019
2 parents 27e26f9 + ffec0dd commit 430a796
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 0.15.0 (Major Release)

Adds an optional setting OPAL_DEFAULT_SEARCH_FIELDS that specifies the fields used to
search in when a criteria isn't specified.

### 0.14.2 (Minor Release)

Documentation fix, we're on python 3.4 now.
Expand Down
12 changes: 12 additions & 0 deletions doc/docs/reference/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ The human readable form of your application name.
Displayed in the header by default.
Scaffolded applications start with whatever is passed in to `opal startproject`.

## OPAL_DEFAULT_SEARCH_FIELDS

Search fields that we look in by default when a user enters a search term.
Defaults to
```python
[
"demographics__hospital_number",
"demographics__first_name",
"demographics__surname"
]
```

## OPAL_LOG_OUT_DURATION

Opal will log users out if they have been inactive for greater than this value.
Expand Down
15 changes: 13 additions & 2 deletions opal/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections import defaultdict
import operator

from django.conf import settings
from django.db import models
from django.db.models import Q

Expand All @@ -13,21 +14,31 @@
from opal.core.fields import ForeignKeyOrFreeText
from functools import reduce

DEFAULT_SEARCH_FIELDS = [
"demographics__hospital_number",
"demographics__first_name",
"demographics__surname"
]


class PatientQueryset(models.QuerySet):
def search(self, some_query):
"""
splits a string by space and queries
first name, last name and hospital number
"""
fields = ["hospital_number", "first_name", "surname"]
fields = getattr(
settings,
'OPAL_DEFAULT_SEARCH_FIELDS',
DEFAULT_SEARCH_FIELDS
)

query_values = some_query.split(" ")
qs = self
for query_value in query_values:
q_objects = []
for field in fields:
model_field = "demographics__{}__icontains".format(field)
model_field = "{}__icontains".format(field)
q_objects.append(Q(**{model_field: query_value}))
qs = qs.filter(reduce(operator.or_, q_objects))
return qs
Expand Down
6 changes: 6 additions & 0 deletions opal/scaffolding/scaffold/app/settings.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ OPAL_LOG_OUT_DURATION = OPAL_LOG_OUT_MINUTES*60*1000
# Enable/Disable autocomplete from navbar search
OPAL_AUTOCOMPLETE_SEARCH = False

OPAL_DEFAULT_SEARCH_FIELDS = [
"demographics__hospital_number",
"demographics__first_name",
"demographics__surname"
]

INTEGRATING = False

# OPAL-required Django settings you should edit
Expand Down
10 changes: 10 additions & 0 deletions opal/tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ def test_combination(self):
query = Patient.objects.search('je rien')
self.assertEqual(query.get(), self.patient_1)

def test_overriding_search_fields(self):
patient = Patient.objects.create()
words = patient.famouslastwords_set.get()
words.words = "This is no way to live!"
words.save()
with self.settings(OPAL_DEFAULT_SEARCH_FIELDS=['famouslastwords__words']):
p = Patient.objects.search('no way to live').get()
self.assertEqual(patient, p)



class EpisodeManagerTestCase(OpalTestCase):

Expand Down

0 comments on commit 430a796

Please sign in to comment.