diff --git a/cookbook/helper/dal.py b/cookbook/helper/dal.py index 04dd5b2d6..12411cec0 100644 --- a/cookbook/helper/dal.py +++ b/cookbook/helper/dal.py @@ -1,6 +1,6 @@ from dal import autocomplete -from cookbook.models import Keyword +from cookbook.models import Keyword, RecipeIngredients class KeywordAutocomplete(autocomplete.Select2QuerySetView): @@ -14,3 +14,16 @@ class KeywordAutocomplete(autocomplete.Select2QuerySetView): qs = qs.filter(name__istartswith=self.q) return qs + + +class IngredientsAutocomplete(autocomplete.Select2QuerySetView): + def get_queryset(self): + if not self.request.user.is_authenticated: + return RecipeIngredients.objects.none() + + qs = RecipeIngredients.objects.all() + + if self.q: + qs = qs.filter(name__istartswith=self.q) + + return qs diff --git a/cookbook/urls.py b/cookbook/urls.py index 9c314d53c..d1ac1a6ea 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -50,4 +50,5 @@ urlpatterns = [ path('api/sync_all/', api.sync_all, name='api_sync'), path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'), + path('dal/ingredient/', dal.IngredientsAutocomplete.as_view(), name='dal_ingredient'), ]