resolved loading custom filter when not in expert mode

This commit is contained in:
smilerz
2022-02-18 09:08:36 -06:00
parent 6b5a099ba0
commit 2b5aec5d0a
2 changed files with 32 additions and 49 deletions

View File

@ -74,13 +74,16 @@ class RecipeSearch():
self._createdon = self._params.get('createdon', None) self._createdon = self._params.get('createdon', None)
self._updatedon = self._params.get('updatedon', None) self._updatedon = self._params.get('updatedon', None)
self._viewedon = self._params.get('viewedon', None) self._viewedon = self._params.get('viewedon', None)
self._makenow = self._params.get('makenow', None)
# this supports hidden feature to find recipes missing X ingredients # this supports hidden feature to find recipes missing X ingredients
try: if type(self._makenow) == bool and self._makenow == True:
self._makenow = int(makenow := self._params.get('makenow', None)) self._makenow = 0
except (ValueError, TypeError): elif type(self._makenow) == str and self._makenow in ["yes", "true"]:
if str2bool(makenow):
self._makenow = 0 self._makenow = 0
else: else:
try:
self._makenow = int(self._makenow)
except (ValueError, TypeError):
self._makenow = None self._makenow = None
self._search_type = self._search_prefs.search or 'plain' self._search_type = self._search_prefs.search or 'plain'
@ -495,21 +498,23 @@ class RecipeSearch():
self._filters += [Q(pk__in=self._fuzzy_match.values('pk'))] self._filters += [Q(pk__in=self._fuzzy_match.values('pk'))]
def _makenow_filter(self, missing=None): def _makenow_filter(self, missing=None):
if missing is None: if missing is None or (type(missing) == bool and missing == False):
return return
shopping_users = [*self._request.user.get_shopping_share(), self._request.user] shopping_users = [*self._request.user.get_shopping_share(), self._request.user]
onhand_filter = ( onhand_filter = (
Q(steps__ingredients__food__onhand_users__in=shopping_users, steps__ingredients__food__ignore_shopping=False) # food onhand Q(steps__ingredients__food__onhand_users__in=shopping_users) # food onhand
| Q(steps__ingredients__food__substitute__onhand_users__in=shopping_users, steps__ingredients__food__substitute__ignore_shopping=False) # or substitute food onhand | Q(steps__ingredients__food__substitute__onhand_users__in=shopping_users) # or substitute food onhand
| Q(steps__ingredients__food__in=self.__children_substitute_filter(shopping_users)) | Q(steps__ingredients__food__in=self.__children_substitute_filter(shopping_users))
| Q(steps__ingredients__food__in=self.__sibling_substitute_filter(shopping_users)) | Q(steps__ingredients__food__in=self.__sibling_substitute_filter(shopping_users))
) )
makenow_recipes = Recipe.objects.annotate( makenow_recipes = Recipe.objects.annotate(
count_food=Count('steps__ingredients__food', filter=Q(steps__ingredients__food__ignore_shopping=False, steps__ingredients__food__isnull=False), distinct=True), count_food=Count('steps__ingredients__food__pk', filter=Q(steps__ingredients__food__isnull=False), distinct=True),
count_onhand=Count('pk', filter=onhand_filter) count_onhand=Count('steps__ingredients__food__pk', filter=onhand_filter, distinct=True),
).annotate(missingfood=F('count_food')-F('count_onhand')).filter(missingfood=missing) count_ignore_shopping=Count('steps__ingredients__food__pk', filter=Q(steps__ingredients__food__ignore_shopping=True,
self._queryset = self._queryset.filter(id__in=makenow_recipes.values('id')) steps__ingredients__food__recipe__isnull=True), distinct=True),
).annotate(missingfood=F('count_food')-F('count_onhand')-F('count_ignore_shopping')).filter(missingfood=missing)
self._queryset = self._queryset.distinct().filter(id__in=makenow_recipes.values('id'))
@staticmethod @staticmethod
def __children_substitute_filter(shopping_users=None): def __children_substitute_filter(shopping_users=None):
@ -520,7 +525,7 @@ class RecipeSearch():
).annotate(child_onhand=Count((Substr(OuterRef('path'), 1, Food.steplen*OuterRef('depth'))), distinct=True)).values('child_onhand') ).annotate(child_onhand=Count((Substr(OuterRef('path'), 1, Food.steplen*OuterRef('depth'))), distinct=True)).values('child_onhand')
return Food.objects.exclude( # list of foods that are onhand and children of: foods that are not onhand and are set to use children as substitutes return Food.objects.exclude( # list of foods that are onhand and children of: foods that are not onhand and are set to use children as substitutes
Q(onhand_users__in=shopping_users) Q(onhand_users__in=shopping_users)
| Q(ignore_shopping=True) | Q(ignore_shopping=True, recipe__isnull=True)
| Q(substitute__onhand_users__in=shopping_users) | Q(substitute__onhand_users__in=shopping_users)
).exclude(depth=1, numchild=0).filter(substitute_children=True ).exclude(depth=1, numchild=0).filter(substitute_children=True
).annotate(child_onhand=Coalesce(Subquery(children_onhand_subquery), 0)) ).annotate(child_onhand=Coalesce(Subquery(children_onhand_subquery), 0))
@ -534,7 +539,7 @@ class RecipeSearch():
).annotate(sibling_onhand=Count(Substr(OuterRef('path'), 1, Food.steplen*(OuterRef('depth')-1)), distinct=True)).values('sibling_onhand') ).annotate(sibling_onhand=Count(Substr(OuterRef('path'), 1, Food.steplen*(OuterRef('depth')-1)), distinct=True)).values('sibling_onhand')
return Food.objects.exclude( # list of foods that are onhand and siblings of: foods that are not onhand and are set to use siblings as substitutes return Food.objects.exclude( # list of foods that are onhand and siblings of: foods that are not onhand and are set to use siblings as substitutes
Q(onhand_users__in=shopping_users) Q(onhand_users__in=shopping_users)
| Q(ignore_shopping=True) | Q(ignore_shopping=True, recipe__isnull=True)
| Q(substitute__onhand_users__in=shopping_users) | Q(substitute__onhand_users__in=shopping_users)
).exclude(depth=1, numchild=0).filter(substitute_siblings=True ).exclude(depth=1, numchild=0).filter(substitute_siblings=True
).annotate(sibling_onhand=Coalesce(Subquery(sibling_onhand_subquery), 0)) ).annotate(sibling_onhand=Coalesce(Subquery(sibling_onhand_subquery), 0))

View File

@ -1029,7 +1029,7 @@ export default {
} }
// if selecting a filter, reset all current selections // if selecting a filter, reset all current selections
if (obj.var === "search_filter") { if (obj.var === "search_filter") {
this.resetSearch(this.search.search_filter) this.resetSearch(true)
} else { } else {
this.refreshData(false) this.refreshData(false)
} }
@ -1057,36 +1057,14 @@ export default {
this.search.createdon = undefined this.search.createdon = undefined
this.search.updatedon = undefined this.search.updatedon = undefined
let fieldnum = { this.search.keywords_fields = 1
keywords: 1, this.search.foods_fields = 1
foods: 1, this.search.books_fields = 1
books: 1,
}
if (filter) { if (!filter) {
// this can be simplified by calling recipe API {query: {filter: filter_id}} but you lose loading all of the params into the UI
filter = JSON.parse(filter.search)
let fields = ["keywords", "foods", "books"]
let operators = ["_or", "_and", "_or_not", "_and_not"]
fields.forEach((field) => {
let x = 0
operators.forEach((operator) => {
if (filter[`${field}${operator}`].length > 0) {
this.search[`search_${field}`][x].items = filter[`${field}${operator}`]
this.search[`search_${field}`][x].operator = operator.includes("or")
this.search[`search_${field}`][x].not = operator.includes("not")
x = x + 1
}
})
fieldnum[field] = fieldnum[field] + x
})
} else {
this.search.search_filter = undefined this.search.search_filter = undefined
} }
this.search.keywords_fields = fieldnum["keywords"]
this.search.foods_fields = fieldnum["foods"]
this.search.books_fields = fieldnum["books"]
// this.search.rating_fields = 1 // this.search.rating_fields = 1
// this.search.units_fields = 1 // this.search.units_fields = 1
this.refreshData(false) this.refreshData(false)
@ -1150,6 +1128,11 @@ export default {
return return
}, },
buildParams: function (random) { buildParams: function (random) {
let params = { options: { query: {} }, page: this.search.pagination_page, pageSize: this.ui.page_size }
if (this.search.search_filter) {
params.options.query.filter = this.search.search_filter.id
return params
}
this.random_search = random this.random_search = random
let rating = this.search.search_rating let rating = this.search.search_rating
if (rating !== undefined && !this.search.search_rating_gte) { if (rating !== undefined && !this.search.search_rating_gte) {
@ -1178,8 +1161,8 @@ export default {
timescooked = timescooked * -1 timescooked = timescooked * -1
} }
// when a filter is selected - added search params will be added to the filter // when a filter is selected - added search params will be added to the filter
let params = { params = {
options: { query: {} }, ...params,
...this.addFields("keywords"), ...this.addFields("keywords"),
...this.addFields("foods"), ...this.addFields("foods"),
...this.addFields("books"), ...this.addFields("books"),
@ -1194,8 +1177,6 @@ export default {
createdon: createdon, createdon: createdon,
updatedon: updatedon, updatedon: updatedon,
viewedon: viewedon, viewedon: viewedon,
page: this.search.pagination_page,
pageSize: this.ui.page_size,
} }
params.options.query = { params.options.query = {
@ -1206,9 +1187,6 @@ export default {
params.options.query.num_recent = this.ui.recently_viewed //TODO refactor as num_recent params.options.query.num_recent = this.ui.recently_viewed //TODO refactor as num_recent
params._new = this.ui.sort_by_new params._new = this.ui.sort_by_new
} }
if (this.search.search_filter) {
params.options.query.filter = this.search.search_filter.id
}
return params return params
}, },
searchFiltered: function (ignore_string = false) { searchFiltered: function (ignore_string = false) {