Skip to content
Snippets Groups Projects

Autocomplete

Closed Alan requested to merge autocomplete into staging
12 unresolved threads

Merge request reports

Checking pipeline status.

Closed by Ian HallworthIan Hallworth 4 years ago (Apr 14, 2020 3:16pm UTC)

Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
7
8
9 class HIDAutoCompleteField(AutoCompleteField):
10 pass
11
12
13 class HIDAutoCompleteWidget(AutoCompleteWidget):
14 pass
15
16
17 class FiltersForm(forms.Form):
18 tags = HIDAutoCompleteField(
19 'tags',
20 required=False,
21 help_text=None,
22 label=_("TAGS"),
  • 1 from ajax_select.lookup_channel import LookupChannel
    2 from ajax_select.registry import register
    3
    4 from taxonomies.models import Term
    5
    6
    7 @register('tags')
    8 class TagsLookup(LookupChannel):
    • We should use the API instead of direct database access.

      I think what we need is a change to get_queryset() in TermViewSet in rest_api/views.py in addition to the exact match filters we have can have "name_istartswith" or "name_begins" or something (we called the one in ItemViewSet "search" but this matches the search string anywhere in the field). Then it's just another couple of lines to filter the queryset as we are already doing.

              name_begins = self.request.query_params.get('name_begins', None)
              if name_begins is not None:
                  items = items.filter(name_istartswith=name)

      (please also rename the variable 'items' to 'terms' in this method)

      Then in the lookups.py we need to call transport.terms.list(taxonomy="tags", name_istartswith="the search string")

      Edited by Martin Burchell
    • Please register or sign in to reply
  • 11 def get_query(self, q, request):
    12 q = q.split(',')[-1].strip()
    13 if q == '':
    14 return self.model.objects.none()
    15 return self.model.objects.filter(
    16 taxonomy__name='tags'
    17 ).filter(name__istartswith=q)
    18
    19 def format_match(self, obj):
    20 return obj.name
    21
    22 def get_result(self, obj, q):
    23 if ',' not in q:
    24 return obj.name
    25 q = q.rsplit(',', 1)[0].strip()
    26 return q + ', ' + obj.name
  • 261 262 feedback_type_options = self._get_feedback_type_options()
    262 263 age_range_options = self._get_age_range_options()
    263 264 selected_age_ranges = request.GET.getlist('age_range')
    265 filter_form = FiltersForm(data=request.GET)
  • 1 {% load crispy_forms_field %}
    2
    3 {% if field.is_hidden %}
    4 {{ field }}
    5 {% else %}
    6 {% if field|is_checkbox %}
  • 1 1 {% load bootstrap3 %}
    2 <form action="" method="get">
    3 <div class="row filter-container">
    4 <div class="filters form-group">
    5 {% for filter in dynamic_filters %}
    6 {% with "hid/tabs/filters/"|add:filter|add:".html" as filter_template %}
  • 3 from hid.lookups import TagsLookup
    4 from taxonomies.tests.factories import TaxonomyFactory, TermFactory
    5
    6
    7 @pytest.mark.django_db
    8 def test_lookup_query_only_contains_tags():
    9 tags_tax = TaxonomyFactory(name='tags')
    10 other_tax = TaxonomyFactory()
    11
    12 expected_terms = [TermFactory(name='hello', taxonomy=tags_tax)]
    13 TermFactory(name='hello bye', taxonomy=other_tax)
    14
    15 tags = TagsLookup()
    16 actual_terms = list(tags.get_query('hello', None))
    17
    18 assert expected_terms == actual_terms
  • 46
    47
    48 def test_only_term_name_displayed_in_dropdown():
    49 term = TermFactory.build(name='hello')
    50
    51 tags = TagsLookup()
    52 result = tags.format_match(term)
    53
    54 assert 'hello' == result
    55
    56
    57 def test_only_term_name_displayed_in_text_box():
    58 term = TermFactory.build(name='hello')
    59
    60 tags = TagsLookup()
    61 result = tags.get_result(term)
  • 1 from ajax_select.lookup_channel import LookupChannel
    2 from ajax_select.registry import register
    3
    4 from taxonomies.models import Term
    5
    6
    7 @register('tags')
    8 class TagsLookup(LookupChannel):
    9 model = Term
    10
    11 def get_query(self, q, request):
    12 q = q.split(',')[-1].strip()
  • 1 import json
    2
    3 from django.http import HttpResponse
    4 from django.utils.encoding import force_text
    5
    6 from ajax_select import registry
    7
    8
    9 # Copy and pasted this whole method from Django-ajax-select views.py in its entirety in order
    10 # to change one line, so we can call lookup.get_result and pass it the query.
    11 # This is in order to cope with multiple, comma separated tags.
    12 def hid_ajax_lookup(request, channel):
  • 1 from django import forms
    2 from django.utils.translation import gettext as _
    3
    4 from ajax_select.fields import AutoCompleteField, AutoCompleteWidget
    5 from crispy_forms.helper import FormHelper
    6 from crispy_forms.layout import HTML, Div, Layout
    7
    8
    9 class HIDAutoCompleteField(AutoCompleteField):
  • 22 label=_("TAGS"),
    23 widget=HIDAutoCompleteWidget(
    24 'tags',
    25 attrs={'placeholder': _('All Tags'), 'class': 'form-control'}
    26 )
    27 )
    28
    29 def __init__(self, *args, **kwargs):
    30 super().__init__(*args, **kwargs)
    31 self.helper = FormHelper(self)
    32 self.helper.form_action = ''
    33 self.helper.form_method = 'GET'
    34 self.helper.layout = Layout(
    35 Div(
    36 Div(
    37 HTML("{% include 'hid/tabs/filters/time_range.html' %}"),
  • Alan added 1 commit

    added 1 commit

    • 2bf8a6d9 - Refactor to store type ahead query on the lookups object and so avoid having...

    Compare with previous version

  • closed

  • Please register or sign in to reply
    Loading