This commit is contained in:
Chris Scoggins
2022-01-19 15:56:39 -06:00
parent 0ac369423c
commit bb226a221e
3 changed files with 43 additions and 18 deletions

View File

@ -222,13 +222,16 @@ class RecipeSearch():
def rating_filter(self, rating=None): def rating_filter(self, rating=None):
if rating is None: if rating is None:
return return
rating = int(rating) lessthan = '-' in rating
# TODO make ratings a settings user-only vs all-users # TODO make ratings a settings user-only vs all-users
self._queryset = self._queryset.annotate(rating=Round(Avg(Case(When(cooklog__created_by=self._request.user, then='cooklog__rating'), default=Value(0))))) self._queryset = self._queryset.annotate(rating=Round(Avg(Case(When(cooklog__created_by=self._request.user, then='cooklog__rating'), default=Value(0)))))
if rating == 0: if rating == 0:
self._queryset = self._queryset.filter(rating=0) self._queryset = self._queryset.filter(rating=0)
elif lessthan:
self._queryset = self._queryset.filter(rating__lte=int(rating[1:]))
else: else:
self._queryset = self._queryset.filter(rating__gte=rating) self._queryset = self._queryset.filter(rating__gte=int(rating))
def internal_filter(self): def internal_filter(self):
self._queryset = self._queryset.filter(internal=True) self._queryset = self._queryset.filter(internal=True)

View File

@ -143,10 +143,10 @@
<!-- keywords filter --> <!-- keywords filter -->
<div class="row" v-if="ui.show_keywords"> <div class="row" v-if="ui.show_keywords">
<div class="col-12"> <div class="col-12">
<b-input-group class="mt-2"> <b-input-group class="mt-2" v-for="(x, i) in search.keyword_fields" :key="i">
<treeselect <treeselect
v-if="ui.tree_select" v-if="ui.tree_select"
v-model="search.search_keywords" v-model="search.search_keywords[i].items"
:options="facets.Keywords" :options="facets.Keywords"
:load-options="loadKeywordChildren" :load-options="loadKeywordChildren"
:multiple="true" :multiple="true"
@ -161,8 +161,8 @@
<generic-multiselect <generic-multiselect
@change="genericSelectChanged" @change="genericSelectChanged"
v-if="!ui.tree_select" v-if="!ui.tree_select"
parent_variable="search_keywords" :parent_variable="`search_keywords::${i}`"
:initial_selection="search.search_keywords" :initial_selection="search.search_keywords[i].items"
:model="Models.KEYWORD" :model="Models.KEYWORD"
style="flex-grow: 1; flex-shrink: 1; flex-basis: 0" style="flex-grow: 1; flex-shrink: 1; flex-basis: 0"
:placeholder="$t('Keywords')" :placeholder="$t('Keywords')"
@ -170,7 +170,7 @@
></generic-multiselect> ></generic-multiselect>
<b-input-group-append> <b-input-group-append>
<b-input-group-text> <b-input-group-text>
<b-form-checkbox v-model="search.search_keywords_or" name="check-button" @change="refreshData(false)" class="shadow-none" switch> <b-form-checkbox v-model="search.search_keywords[i].operator" name="check-button" @change="refreshData(false)" class="shadow-none" switch>
<span class="text-uppercase" v-if="search.search_keywords_or">{{ $t("or") }}</span> <span class="text-uppercase" v-if="search.search_keywords_or">{{ $t("or") }}</span>
<span class="text-uppercase" v-else>{{ $t("and") }}</span> <span class="text-uppercase" v-else>{{ $t("and") }}</span>
</b-form-checkbox> </b-form-checkbox>
@ -365,17 +365,24 @@ export default {
advanced_search_visible: false, advanced_search_visible: false,
search_input: "", search_input: "",
search_internal: false, search_internal: false,
search_keywords: [], search_keywords: [
{ items: [], operator: true },
{ items: [], operator: true },
{ items: [], operator: true },
{ items: [], operator: true },
],
search_foods: [], search_foods: [],
search_books: [], search_books: [],
search_units: [], search_units: [],
search_ratings: undefined, search_ratings: undefined,
search_rating_gte: true,
search_keywords_or: true, search_keywords_or: true,
search_foods_or: true, search_foods_or: true,
search_books_or: true, search_books_or: true,
search_units_or: true, search_units_or: true,
pagination_page: 1, pagination_page: 1,
expert_mode: false, expert_mode: false,
keyword_fields: 1,
}, },
ui: { ui: {
show_meal_plan: true, show_meal_plan: true,
@ -412,6 +419,12 @@ export default {
return ` (${x})` return ` (${x})`
} }
} }
let label = ""
if (this.search.search_rating_gte) {
label = this.$t("and_up")
} else {
label = this.$t("and_down")
}
return [ return [
{ id: 5, label: "⭐⭐⭐⭐⭐" + ratingCount(this.facets.Ratings?.["5.0"] ?? 0) }, { id: 5, label: "⭐⭐⭐⭐⭐" + ratingCount(this.facets.Ratings?.["5.0"] ?? 0) },
{ id: 4, label: "⭐⭐⭐⭐ " + this.$t("and_up") + ratingCount(this.facets.Ratings?.["4.0"] ?? 0) }, { id: 4, label: "⭐⭐⭐⭐ " + this.$t("and_up") + ratingCount(this.facets.Ratings?.["4.0"] ?? 0) },
@ -436,7 +449,7 @@ export default {
let urlParams = new URLSearchParams(window.location.search) let urlParams = new URLSearchParams(window.location.search)
if (urlParams.has("keyword")) { if (urlParams.has("keyword")) {
this.search.search_keywords = [] this.search.search_keywords[0].items = []
this.facets.Keywords = [] this.facets.Keywords = []
for (let x of urlParams.getAll("keyword")) { for (let x of urlParams.getAll("keyword")) {
let initial_keyword = { id: Number.parseInt(x), name: "loading..." } let initial_keyword = { id: Number.parseInt(x), name: "loading..." }
@ -503,8 +516,7 @@ export default {
this.refreshData(false) this.refreshData(false)
}, },
"ui.tree_select": function () { "ui.tree_select": function () {
if (this.ui.tree_select && !this.facets?.Keywords && !this.facets?.Foods) { if (this.ui.tree_select && (!this.facets?.Keywords || !this.facets?.Foods)) {
console.log("i changed to true")
this.getFacets(this.facets?.hash) this.getFacets(this.facets?.hash)
} }
}, },
@ -520,10 +532,11 @@ export default {
methods: { methods: {
// this.genericAPI inherited from ApiMixin // this.genericAPI inherited from ApiMixin
refreshData: function (random) { refreshData: function (random) {
console.log(this.search.search_keywords)
this.random_search = random this.random_search = random
let params = { let params = {
query: this.search.search_input, query: this.search.search_input,
keywords: this.search.search_keywords.map(function (A) { keywords: this.search.search_keywords[0].items.map(function (A) {
return A?.["id"] ?? A return A?.["id"] ?? A
}), }),
foods: this.search.search_foods.map(function (A) { foods: this.search.search_foods.map(function (A) {
@ -549,6 +562,7 @@ export default {
if (!this.searchFiltered) { if (!this.searchFiltered) {
params.options = { query: { last_viewed: this.ui.recently_viewed } } params.options = { query: { last_viewed: this.ui.recently_viewed } }
} }
// console.log(params, this.search.search_keywords[0], this.search.search_keywords[0].items)
this.genericAPI(this.Models.RECIPE, this.Actions.LIST, params) this.genericAPI(this.Models.RECIPE, this.Actions.LIST, params)
.then((result) => { .then((result) => {
window.scrollTo(0, 0) window.scrollTo(0, 0)
@ -593,13 +607,19 @@ export default {
} }
}, },
genericSelectChanged: function (obj) { genericSelectChanged: function (obj) {
this.search[obj.var] = obj.val if (obj.var.includes("::")) {
let x = obj.var.split("::")
this.search[x[0]][x[1]].items = obj.val
} else {
this.search[obj.var] = obj.val
}
this.refreshData(false) this.refreshData(false)
}, },
resetSearch: function () { resetSearch: function () {
this.search.search_input = "" this.search.search_input = ""
this.search.search_internal = false this.search.search_internal = false
this.search.search_keywords = [] this.search.search_keywords[0].items = []
this.search.search_foods = [] this.search.search_foods = []
this.search.search_books = [] this.search.search_books = []
this.search.search_units = [] this.search.search_units = []
@ -665,7 +685,7 @@ export default {
buildParams: function () { buildParams: function () {
let params = { let params = {
query: this.search.search_input, query: this.search.search_input,
keywords: this.search.search_keywords, keywords: this.search.search_keywords[0].items,
foods: this.search.search_foods, foods: this.search.search_foods,
rating: this.search.search_ratings, rating: this.search.search_ratings,
books: this.search.search_books.map(function (A) { books: this.search.search_books.map(function (A) {
@ -686,16 +706,17 @@ export default {
}, },
searchFiltered: function (ignore_string = false) { searchFiltered: function (ignore_string = false) {
let filtered = let filtered =
this.search?.search_keywords?.length === 0 && this.search?.search_keywords[0].items?.length === 0 &&
this.search?.search_foods?.length === 0 && this.search?.search_foods?.length === 0 &&
this.search?.search_books?.length === 0 && this.search?.search_books?.length === 0 &&
// this.settings?.pagination_page === 1 && // this.settings?.pagination_page === 1 &&
!this.random_search && !this.random_search &&
this.search?.search_ratings === undefined this.search?.search_ratings === undefined
if (ignore_string) { if (ignore_string) {
return filtered return !filtered
} else { } else {
return filtered && this.search?.search_input === "" return !filtered && this.search?.search_input !== ""
} }
}, },
}, },

View File

@ -165,6 +165,7 @@
"Create_New_Unit": "Add New Unit", "Create_New_Unit": "Add New Unit",
"Create_New_Meal_Type": "Add New Meal Type", "Create_New_Meal_Type": "Add New Meal Type",
"and_up": "& Up", "and_up": "& Up",
"and_down": "& Down",
"Instructions": "Instructions", "Instructions": "Instructions",
"Unrated": "Unrated", "Unrated": "Unrated",
"Automate": "Automate", "Automate": "Automate",