From 1eec108db1dbdd9f1a40f6bf6131deee75da2f40 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Sun, 19 Apr 2020 17:07:29 +0200 Subject: [PATCH] Add email state search filter --- search/engine.py | 20 +++++++++++++++----- search/forms.py | 13 +++++++++++++ search/views.py | 4 +++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/search/engine.py b/search/engine.py index 3dfd0680..e0ed1018 100644 --- a/search/engine.py +++ b/search/engine.py @@ -166,7 +166,8 @@ def contains_filter(attribute, word, case_sensitive=False): def search_single_word(word, filters, user, start, end, - user_state, aff, case_sensitive=False): + user_state, email_state, aff, + case_sensitive=False): """ Construct the correct filters to match differents fields of some models with the given query according to the given filters. The match field are either CharField or IntegerField that will be displayed @@ -199,6 +200,9 @@ def search_single_word(word, filters, user, start, end, filter_clubs &= Q(state__in=user_state) filter_users &= Q(state__in=user_state) + filter_clubs &= Q(email_state__in=email_state) + filter_users &= Q(email_state__in=email_state) + filters["users"] |= filter_users filters["clubs"] |= filter_clubs @@ -207,7 +211,8 @@ def search_single_word(word, filters, user, start, end, filter_machines = ( contains_filter("name", word, case_sensitive) | (contains_filter("user__pseudo", word, case_sensitive) - & Q(user__state__in=user_state)) + & Q(user__state__in=user_state) + & Q(user__email_state__in=email_state)) | contains_filter("interface__domain__name", word, case_sensitive) | contains_filter("interface__domain__related_domain__name", word, case_sensitive) @@ -228,6 +233,7 @@ def search_single_word(word, filters, user, start, end, filter_factures = ( contains_filter("user__pseudo", word, case_sensitive) & Q(user__state__in=user_state) + & Q(user__email_state__in=email_state) ) if start is not None: filter_factures &= Q(date__gte=start) @@ -240,6 +246,7 @@ def search_single_word(word, filters, user, start, end, filter_bans = ( contains_filter("user__pseudo", word, case_sensitive) & Q(user__state__in=user_state) + & Q(user__email_state__in=email_state) ) | contains_filter("raison", word, case_sensitive) if start is not None: filter_bans &= ( @@ -260,6 +267,7 @@ def search_single_word(word, filters, user, start, end, filter_whitelists = ( contains_filter("user__pseudo", word, case_sensitive) & Q(user__state__in=user_state) + & Q(user__email_state__in=email_state) ) | contains_filter("raison", word, case_sensitive) if start is not None: filter_whitelists &= ( @@ -397,7 +405,7 @@ def apply_filters(filters, user, aff): return results -def search_single_query(query, filters, user, start, end, user_state, aff): +def search_single_query(query, filters, user, start, end, user_state, email_state, aff): """ Handle different queries an construct the correct filters using search_single_word""" if query.operator == "+": @@ -406,7 +414,8 @@ def search_single_query(query, filters, user, start, end, user_state, aff): for q in query.subqueries: # Construct an independent filter for each subquery subfilters = search_single_query(q, empty_filters(), user, - start, end, user_state, aff) + start, end, user_state, + email_state, aff) # Apply the subfilter for field in filter_fields(): @@ -420,7 +429,8 @@ def search_single_query(query, filters, user, start, end, user_state, aff): # Handle standard queries return search_single_word(query.text, filters, user, start, end, - user_state, aff, query.case_sensitive) + user_state, email_state, aff, + query.case_sensitive) def create_queries(query): diff --git a/search/forms.py b/search/forms.py index 9f2ff82a..2c495b5a 100644 --- a/search/forms.py +++ b/search/forms.py @@ -37,6 +37,12 @@ CHOICES_USER = ( ("4", _("Fully archived")), ) +CHOICES_EMAILS = ( + ("0", _("Confirmed")), + ("1", _("Not confirmed")), + ("2", _("Waiting for email confirmation")), +) + CHOICES_AFF = ( ("0", _("Users")), ("1", _("Machines")), @@ -93,6 +99,13 @@ class SearchFormPlus(Form): choices=CHOICES_USER, initial=initial_choices(CHOICES_USER), ) + m = forms.MultipleChoiceField( + label=_("Email state filter"), + required=False, + widget=forms.CheckboxSelectMultiple, + choices=CHOICES_EMAILS, + initial=initial_choices(CHOICES_EMAILS), + ) a = forms.MultipleChoiceField( label=_("Display filter"), required=False, diff --git a/search/views.py b/search/views.py index a994240f..00141ed1 100644 --- a/search/views.py +++ b/search/views.py @@ -38,6 +38,7 @@ from search.forms import ( SearchForm, SearchFormPlus, CHOICES_USER, + CHOICES_EMAILS, CHOICES_AFF, initial_choices, ) @@ -56,6 +57,7 @@ def get_results(query, request, params): start = params.get("s", None) end = params.get("e", None) user_state = params.get("u", initial_choices(CHOICES_USER)) + email_state = params.get("m", initial_choices(CHOICES_EMAILS)) aff = params.get("a", initial_choices(CHOICES_AFF)) filters = empty_filters() @@ -63,7 +65,7 @@ def get_results(query, request, params): queries = create_queries(query) for q in queries: filters = search_single_query( - q, filters, request.user, start, end, user_state, aff + q, filters, request.user, start, end, user_state, email_state, aff ) results = apply_filters(filters, request.user, aff)