Skip to content

Commit

Permalink
Merge branch 'fix-many-to-many-select' into v0.8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
fredkingham committed May 26, 2017
2 parents 25fa990 + f4de141 commit 77684f2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
14 changes: 13 additions & 1 deletion opal/core/search/templates/extract.html
Expand Up @@ -152,7 +152,7 @@ <h4>Your Criteria</h4>
tooltip-trigger="focus"
ng-keypress="$event.keyCode == 13 && search()"
tooltip-placement="top" uib-tooltip="4. Select one of the below">
<option ng-repeat="i in symptom_list track by $index">[[ i ]]</option>
<option ng-repeat="i in query.lookup_list track by $index">[[ i ]]</option>
</select>
</span>
</div>
Expand Down Expand Up @@ -198,6 +198,18 @@ <h4>Your Criteria</h4>
<span ng-show="isBoolean(selectedInfo.column, selectedInfo.field)">
[True/False]
</span>
<span ng-show="isSelect(selectedInfo.column, selectedInfo.field)">

<div ng-show="selectedInfo.lookup_list && selectedInfo.lookup_list.length">
[a
<a href=""
class="orange-link"
ng-click="open_modal('LookupListReferenceCtrl', '/templates/modals/lookuplist_reference.html', {lookuplist_name: findField(selectedInfo.column, selectedInfo.field).lookup_list, lookuplist: selectedInfo.lookup_list })">
[[ findField(selectedInfo.column, selectedInfo.field).lookup_list | underscoreToSpaces ]]
</a>]
</div>
</span>
<span ng-show
<span ng-show="isText(selectedInfo.column, selectedInfo.field)">
[Text]
<div ng-show="findField(selectedInfo.column, selectedInfo.field).lookup_list">
Expand Down
9 changes: 7 additions & 2 deletions opal/models.py
Expand Up @@ -260,11 +260,16 @@ def get_field_enum(cls, name):
@classmethod
def get_lookup_list_api_name(cls, field_name):
lookup_list = None
if cls._get_field_type(field_name) == ForeignKeyOrFreeText:
field_type = cls._get_field_type(field_name)
if field_type == ForeignKeyOrFreeText:
fld = getattr(cls, field_name)
lookup_list = camelcase_to_underscore(
fld.foreign_model.__name__
fld.foreign_model.get_api_name()
)
elif field_type == models.fields.related.ManyToManyField:
related_model = getattr(cls, field_name).field.related_model
if issubclass(related_model, lookuplists.LookupList):
return related_model.get_api_name()
return lookup_list

@classmethod
Expand Down
12 changes: 3 additions & 9 deletions opal/templatetags/forms.py
Expand Up @@ -78,16 +78,10 @@ def infer_from_subrecord_field_path(subRecordFieldPath):

if enum:
ctx["lookuplist"] = json.dumps(enum)
elif hasattr(field, "foreign_model"):
ctx["lookuplist"] = "{}_list".format(
field.foreign_model.get_api_name()
)
else:
related_model = field.related_model
if related_model:
ctx["lookuplist"] = "{}_list".format(
field.related_model.get_api_name()
)
lookuplist_api_name = model.get_lookup_list_api_name(field_name)
if lookuplist_api_name:
ctx["lookuplist"] = "{}_list".format(lookuplist_api_name)

if hasattr(field, "formfield"):
# TODO remove the blankable condition and make sure
Expand Down
1 change: 0 additions & 1 deletion opal/tests/test_models.py
Expand Up @@ -97,7 +97,6 @@ def test_bulk_update_patient_subrecords_respects_order(self):
self.assertEqual(colours[0].name, "green")
self.assertEqual(colours[1].name, "purple")


def test_bulk_update_with_existing_patient_episode(self):
original_patient = models.Patient()
original_patient.save()
Expand Down
17 changes: 17 additions & 0 deletions opal/tests/test_models_mixins.py
Expand Up @@ -161,6 +161,23 @@ def test_get_lookup_list_api_name(self):
result = test_models.HoundOwner.get_lookup_list_api_name("dog")
self.assertEqual(result, "dog")

def test_get_lookup_list_many_to_many_api_name(self):
result = test_models.HatWearer.get_lookup_list_api_name("hats")
self.assertEqual(result, "hat")

def test_get_lookup_list_uses_api_name(self):
with patch.object(test_models.Dog, "get_api_name") as get_api_name:
get_api_name.return_value = "hound"
result = test_models.HoundOwner.get_lookup_list_api_name("dog")
self.assertEqual(result, "hound")
get_api_name.assert_called_once_with()

def test_get_lookup_list_api_name_when_none(self):
# when we're not in a M2M field or a FK or FT field then
# then we should just return None
result = test_models.HoundOwner.get_lookup_list_api_name("name")
self.assertIsNone(result)


class ToDictMixinTestCase(OpalTestCase):
def setUp(self):
Expand Down

0 comments on commit 77684f2

Please sign in to comment.