complex book filters

This commit is contained in:
Chris Scoggins
2022-01-20 15:58:58 -06:00
parent 5e36bd0c27
commit d0549bcb6d
5 changed files with 160 additions and 81 deletions

View File

@@ -44,7 +44,12 @@ class RecipeSearch():
'or_not': self._params.get('foods_or_not', None),
'and_not': self._params.get('foods_and_not', None)
}
self._books = self._params.get('books', None)
self._books = {
'or': self._params.get('books_or', None),
'and': self._params.get('books_and', None),
'or_not': self._params.get('books_or_not', None),
'and_not': self._params.get('books_and_not', None)
}
self._steps = self._params.get('steps', None)
self._units = self._params.get('units', None)
# TODO add created by
@@ -99,7 +104,7 @@ class RecipeSearch():
# self._last_cooked()
self.keyword_filters(**self._keywords)
self.food_filters(**self._foods)
self.book_filters(books=self._books, operator=self._books_or)
self.book_filters(**self._books)
self.rating_filter(rating=self._rating)
self.internal_filter()
self.step_filters(steps=self._steps)
@@ -269,16 +274,30 @@ class RecipeSearch():
def internal_filter(self):
self._queryset = self._queryset.filter(internal=True)
def book_filters(self, books=None, operator=True):
if not books:
def book_filters(self, **kwargs):
if all([kwargs[x] is None for x in kwargs]):
return
if not isinstance(books, list):
books = [books]
if operator == True:
self._queryset = self._queryset.filter(recipebookentry__book__id__in=books)
else:
for k in books:
self._queryset = self._queryset.filter(recipebookentry__book__id=k)
for bk_filter in kwargs:
if not kwargs[bk_filter]:
continue
if not isinstance(kwargs[bk_filter], list):
kwargs[bk_filter] = [kwargs[bk_filter]]
if 'or' in bk_filter:
f = Q(recipebookentry__book__id__in=kwargs[bk_filter])
if 'not' in bk_filter:
self._queryset = self._queryset.exclude(f)
else:
self._queryset = self._queryset.filter(f)
elif 'and' in bk_filter:
recipes = Recipe.objects.all()
for book in kwargs[bk_filter]:
if 'not' in bk_filter:
recipes = recipes.filter(recipebookentry__book__id=book)
else:
self._queryset = self._queryset.filter(recipebookentry__book__id=book)
if 'not' in bk_filter:
self._queryset = self._queryset.exclude(id__in=recipes.values('id'))
def step_filters(self, steps=None, operator=True):
if operator != True:

View File

@@ -634,19 +634,22 @@ class RecipeViewSet(viewsets.ModelViewSet):
query_params = [
QueryParam(name='query', description=_('Query string matched (fuzzy) against recipe name. In the future also fulltext search.')),
QueryParam(name='keywords', description=_('ID of keyword a recipe should have. For multiple repeat parameter. Equivalent to keywords_or'), qtype='int'),
QueryParam(name='keywords_or', description=_('Keyword IDs, repeat for multiple Return recipes with any of the keywords'), qtype='int'),
QueryParam(name='keywords_and', description=_('Keyword IDs, repeat for multiple Return recipes with all of the keywords.'), qtype='int'),
QueryParam(name='keywords_or_not', description=_('Keyword IDs, repeat for multiple Exclude recipes with any of the keywords.'), qtype='int'),
QueryParam(name='keywords_and_not', description=_('Keyword IDs, repeat for multiple Exclude recipes with all of the keywords.'), qtype='int'),
QueryParam(name='keywords_or', description=_('Keyword IDs, repeat for multiple. Return recipes with any of the keywords'), qtype='int'),
QueryParam(name='keywords_and', description=_('Keyword IDs, repeat for multiple. Return recipes with all of the keywords.'), qtype='int'),
QueryParam(name='keywords_or_not', description=_('Keyword IDs, repeat for multiple. Exclude recipes with any of the keywords.'), qtype='int'),
QueryParam(name='keywords_and_not', description=_('Keyword IDs, repeat for multiple. Exclude recipes with all of the keywords.'), qtype='int'),
QueryParam(name='foods', description=_('ID of food a recipe should have. For multiple repeat parameter.'), qtype='int'),
QueryParam(name='foods_or', description=_('Food IDs, repeat for multiple Return recipes with any of the foods'), qtype='int'),
QueryParam(name='foods_and', description=_('Food IDs, repeat for multiple Return recipes with all of the foods.'), qtype='int'),
QueryParam(name='foods_or_not', description=_('Food IDs, repeat for multiple Exclude recipes with any of the foods.'), qtype='int'),
QueryParam(name='foods_and_not', description=_('Food IDs, repeat for multiple Exclude recipes with all of the foods.'), qtype='int'),
QueryParam(name='foods_or', description=_('Food IDs, repeat for multiple. Return recipes with any of the foods'), qtype='int'),
QueryParam(name='foods_and', description=_('Food IDs, repeat for multiple. Return recipes with all of the foods.'), qtype='int'),
QueryParam(name='foods_or_not', description=_('Food IDs, repeat for multiple. Exclude recipes with any of the foods.'), qtype='int'),
QueryParam(name='foods_and_not', description=_('Food IDs, repeat for multiple. Exclude recipes with all of the foods.'), qtype='int'),
QueryParam(name='units', description=_('ID of unit a recipe should have.'), qtype='int'),
QueryParam(name='rating', description=_('Rating a recipe should have. [0 - 5]'), qtype='int'),
QueryParam(name='books', description=_('ID of book a recipe should be in. For multiple repeat parameter.')),
QueryParam(name='books_or', description=_('If recipe should be in all (AND=''false'') or any (OR=''<b>true</b>'') of the provided books.')),
QueryParam(name='books_or', description=_('Book IDs, repeat for multiple. Return recipes with any of the books'), qtype='int'),
QueryParam(name='books_and', description=_('Book IDs, repeat for multiple. Return recipes with all of the books.'), qtype='int'),
QueryParam(name='books_or_not', description=_('Book IDs, repeat for multiple. Exclude recipes with any of the books.'), qtype='int'),
QueryParam(name='books_and_not', description=_('Book IDs, repeat for multiple. Exclude recipes with all of the books.'), qtype='int'),
QueryParam(name='internal', description=_('If only internal recipes should be returned. [''true''/''<b>false</b>'']')),
QueryParam(name='random', description=_('Returns the results in randomized order. [''true''/''<b>false</b>'']')),
QueryParam(name='new', description=_('Returns new results first in search results. [''true''/''<b>false</b>'']')),