add units to search fields

This commit is contained in:
Chris Scoggins 2022-01-31 14:41:18 -06:00
parent 5b3a0a6e29
commit f45e09a5a5
No known key found for this signature in database
GPG Key ID: 41617A4206CCBAC6
4 changed files with 27 additions and 2 deletions

View File

@ -0,0 +1,20 @@
from django.db import migrations
from cookbook.models import SearchFields
def create_searchfields(apps, schema_editor):
SearchFields.objects.create(name='Units', field='steps__ingredients__unit__name')
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0167_userpreference_left_handed'),
]
operations = [
migrations.RunPython(
create_searchfields
),
]

View File

@ -153,11 +153,15 @@ class FuzzyFilterMixin(ViewSetMixin, ExtendedRecipeMixin):
)
else:
# TODO have this check unaccent search settings or other search preferences?
filter = Q(name__icontains=query)
if any([self.model.__name__.lower() in x for x in self.request.user.searchpreference.unaccent.values_list('field', flat=True)]):
filter |= Q(name__unaccent__icontains=query)
self.queryset = (
self.queryset
.annotate(starts=Case(When(name__istartswith=query, then=(Value(100))),
default=Value(0))) # put exact matches at the top of the result set
.filter(name__icontains=query).order_by('-starts', 'name')
.filter(filter).order_by('-starts', 'name')
)
updated_at = self.request.query_params.get('updated_at', None)

View File

@ -161,7 +161,7 @@
:flat="true"
:auto-load-root-options="false"
searchNested
:placeholder="$t('Ingredients')"
:placeholder="$t('Foods')"
:normalizer="normalizer"
@input="refreshData(false)"
style="flex-grow: 1; flex-shrink: 1; flex-basis: 0"

View File

@ -117,6 +117,7 @@ export default {
limit: this.limit,
}
this.genericAPI(this.model, this.Actions.LIST, options).then((result) => {
console.log(result)
this.objects = this.sticky_options.concat(result.data?.results ?? result.data)
if (this.nothingSelected && this.objects.length > 0) {
this.objects.forEach((item) => {