Skip to content

Commit

Permalink
Merge e8657e4 into b75c7fa
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinIsTheBird committed Jan 31, 2017
2 parents b75c7fa + e8657e4 commit 0207dd9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
7 changes: 3 additions & 4 deletions opentreemap/treemap/js/src/lib/otmTypeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ var create = exports.create = function(options) {
url: options.remote,
wildcard: '%Q%'
},
datumTokenizer: function(datum) {
return datum.tokens;
},
datumTokenizer: Bloodhound.tokenizers.nonword,
queryTokenizer: Bloodhound.tokenizers.nonword
// No sorter: the backend sorts it already
});
Expand Down Expand Up @@ -286,7 +284,8 @@ var create = exports.create = function(options) {
idStream.onValue($hidden_input, 'val');
$hidden_input.on('restore', function(event, value) {
$hidden_input.val(value || '');
$input.val(value || '');
var displayValue = $input.data('display-value');
setTypeahead($input, displayValue || value || '');
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
placeholder="{{ field.placeholder }}"
data-qualifier="{{ field.qualifier }}"
data-display="{{ field.display }}"
data-display-value="{{ field.display_value }}"
data-remote="{{ field.typeahead_url }}"/>
<a class="btn btn-default search-button" id="{{ field.qualifier }}-perform-search"><i class="icon-search"></i></a>
<input name="{{ field.identifier }}_id" type="hidden" id="{{ field.id }}-value" {{ extra|default:"" }}/>
<input name="{{ field.identifier }}_id"
type="hidden"
id="{{ field.id }}-value"
{{ extra|default:"" }}/>
</div>
52 changes: 40 additions & 12 deletions opentreemap/treemap/templatetags/form_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
'MultiPolygonField': 'multipolygon',
}

FOREIGN_KEY_PREDICATE = 'IS'

VALID_FIELD_KEYS = ','.join(FIELD_MAPPINGS.keys())


Expand Down Expand Up @@ -399,7 +401,10 @@ def _field_value(model, field_name, data_type):
if units != '':
units = get_unit_abbreviation(units)

if field_value is None:
if data_type == 'foreign_key':
# rendered clientside
display_val = ''
elif field_value is None:
display_val = None
elif data_type in ['date', 'datetime']:
fmt = (model.instance.short_date_format if model.instance
Expand All @@ -417,9 +422,6 @@ def _field_value(model, field_name, data_type):
# there's no meaningful intermediate value to send
# without rendering the same markup server-side.
display_val = None
elif data_type == 'foreign_key:':
# also rendered clientside
display_val = ''
elif choices:
display_vals = [choice['display_value'] for choice in choices
if choice['value'] == field_value]
Expand All @@ -440,7 +442,8 @@ def _field_value(model, field_name, data_type):
'is_editable': is_editable,
'choices': choices,
}
self.get_additional_context(context['field'], model, field_name)
self.get_additional_context(
context['field'], model, field_name, context.get('q', ''))

return field_template.render(context)

Expand Down Expand Up @@ -468,7 +471,23 @@ def resolve_label_and_identifier(self, context):

return label, identifier

def _fill_in_typeahead(self, field, field_name, model):
def _fill_in_typeahead(self, field, field_name, model, search_query):
def get_search_query_value(column, display, identifier,
related_class, search_query):
if not search_query:
return ''
search_map = json.loads(search_query) or {}
model_name, field_name = tuple(dotted_split(
identifier, 2, maxsplit=1))
search_field = '{}.{}'.format(model_name, column)
pred = search_map.get(search_field)
if not pred:
return ''

query_value = pred[FOREIGN_KEY_PREDICATE]
related_model = related_class.objects.get(id=query_value)
return getattr(related_model, display)

relation_lookup_infos = {
'treemap.models.User': {
# /<instance>/users/?q=<query> returns JSON
Expand All @@ -488,16 +507,25 @@ def _fill_in_typeahead(self, field, field_name, model):
'display': 'username'
}
}
related_model = model._meta.get_field(field_name).related_model
model_name = related_model.__module__ + '.' + related_model.__name__
field_instance = model._meta.get_field(field_name)
related_model = field_instance.related_model
model_name = '{}.{}'.format(
related_model.__module__, related_model.__name__)
info = relation_lookup_infos.get(model_name)
if info:
display = info['display']
field['typeahead_url'] = info['url']
field['placeholder'] = info['placeholder']
field['display'] = info['display']
field['display'] = display
field['qualifier'] = field_name
field['column'] = field_instance.column

if search_query:
field['display_value'] = get_search_query_value(
field_instance.column, display,
field['identifier'], related_model, search_query)

def get_additional_context(self, field, model, field_name):
def get_additional_context(self, field, model, field_name, search_query):
def update_field(settings):
# Identifier is lower-cased above to match the calling convention
# of update endpoints, so we shouldn't overwrite it :(
Expand All @@ -517,8 +545,8 @@ def update_field(settings):
elif data_type in {'long_string', 'string', 'multichoice'}:
field['search_type'] = 'LIKE'
elif data_type == 'foreign_key':
field['search_type'] = 'IS'
self._fill_in_typeahead(field, field_name, model)
field['search_type'] = FOREIGN_KEY_PREDICATE
self._fill_in_typeahead(field, field_name, model, search_query)
else:
field['search_type'] = 'IS'

Expand Down
1 change: 1 addition & 0 deletions opentreemap/treemap/views/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def get_map_view_context(request, instance):
],
'resource_classes': resource_classes,
'only_one_resource_class': len(resource_classes) == 1,
'q': request.GET.get('q'),
}
add_map_info_to_context(context, instance)
return context
Expand Down

0 comments on commit 0207dd9

Please sign in to comment.