From 9b8a8819140483de0aa7a4ded80a76e981467527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Duvauchelle?= Date: Tue, 2 Oct 2018 12:39:55 +0200 Subject: [PATCH 01/27] Add haystack index, and a view for forum search (WIP) --- core/middleware.py | 2 +- core/search_indexes.py | 8 +++++++ core/static/core/style.scss | 18 +++++++++++++++ .../indexes/forum/forummessage_text.txt | 1 + forum/templates/forum/main.jinja | 5 +++++ forum/templates/forum/search.jinja | 22 +++++++++++++++++++ forum/urls.py | 2 ++ forum/views.py | 10 +++++++++ 8 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 core/templates/search/indexes/forum/forummessage_text.txt create mode 100644 forum/templates/forum/search.jinja diff --git a/core/middleware.py b/core/middleware.py index 37c01ff9..d6479009 100644 --- a/core/middleware.py +++ b/core/middleware.py @@ -27,7 +27,7 @@ from django.conf import settings from django.utils.functional import SimpleLazyObject from django.contrib.auth import get_user from django.contrib.auth.middleware import ( - AuthenticationMiddleware as DjangoAuthenticationMiddleware, + AuthenticationMiddleware as DjangoAuthenticationMiddleware ) module, klass = settings.AUTH_ANONYMOUS_MODEL.rsplit(".", 1) diff --git a/core/search_indexes.py b/core/search_indexes.py index f157cddb..7c8a50aa 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -27,6 +27,7 @@ from django.db import models from haystack import indexes, signals from core.models import User +from forum.models import ForumMessage class UserIndex(indexes.SearchIndex, indexes.Indexable): @@ -54,3 +55,10 @@ class UserOnlySignalProcessor(signals.BaseSignalProcessor): # Disconnect only for the ``User`` model. models.signals.post_save.disconnect(self.handle_save, sender=User) models.signals.post_delete.disconnect(self.handle_delete, sender=User) + + +class ForumMessageIndex(indexes.SearchIndex, indexes.Indexable): + text = indexes.CharField(document=True, use_template=True) + + def get_model(self): + return ForumMessage diff --git a/core/static/core/style.scss b/core/static/core/style.scss index dc6ef753..ae172e95 100644 --- a/core/static/core/style.scss +++ b/core/static/core/style.scss @@ -1476,6 +1476,24 @@ textarea { } } } + + .search-results { + width: 100%; + .result-box { + width: 100%; + } + .result-topic { + display: inline-block; + background: #9f9f9f; + width: 19%; + } + .result-message { + display: inline-block; + background: #dbdbdb; + width: 80%; + + } + } } /*------------------------------SAS------------------------------------*/ diff --git a/core/templates/search/indexes/forum/forummessage_text.txt b/core/templates/search/indexes/forum/forummessage_text.txt new file mode 100644 index 00000000..2df962ae --- /dev/null +++ b/core/templates/search/indexes/forum/forummessage_text.txt @@ -0,0 +1 @@ +{{ object.message }} diff --git a/forum/templates/forum/main.jinja b/forum/templates/forum/main.jinja index 46c8e966..065007f8 100644 --- a/forum/templates/forum/main.jinja +++ b/forum/templates/forum/main.jinja @@ -21,6 +21,10 @@ {% trans %}New forum{% endtrans %}

{% endif %} +
{% trans %}Title{% endtrans %} @@ -34,6 +38,7 @@
+ {% for f in forum_list %}
{{ display_forum(f, user, True) }} diff --git a/forum/templates/forum/search.jinja b/forum/templates/forum/search.jinja new file mode 100644 index 00000000..465f20bb --- /dev/null +++ b/forum/templates/forum/search.jinja @@ -0,0 +1,22 @@ +{% extends "core/base.jinja" %} + +{% from 'forum/macros.jinja' import display_message %} + +{% block content %} +
+
+ {% for m in object_list %} + {% if user.can_view(m) %} +
+
+ {{ m }} +
+
+ {{ m.message }} +
+
+ {% endif %} + {% endfor %} +
+
+{% endblock %} \ No newline at end of file diff --git a/forum/urls.py b/forum/urls.py index 27ff5718..9a504034 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -26,8 +26,10 @@ from django.conf.urls import url from forum.views import * + urlpatterns = [ url(r"^$", ForumMainView.as_view(), name="main"), + url(r"^search/$", ForumSearchView.as_view(), name="search"), url(r"^new_forum$", ForumCreateView.as_view(), name="new_forum"), url(r"^mark_all_as_read$", ForumMarkAllAsRead.as_view(), name="mark_all_as_read"), url(r"^last_unread$", ForumLastUnread.as_view(), name="last_unread"), diff --git a/forum/views.py b/forum/views.py index 946d563c..d0296016 100644 --- a/forum/views.py +++ b/forum/views.py @@ -39,6 +39,16 @@ from ajax_select import make_ajax_field from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin from core.views.forms import MarkdownInput from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta +from haystack.query import SearchQuerySet + + +class ForumSearchView(ListView): + template_name = "forum/search.jinja" + + def get_queryset(self): + query = self.request.GET.get("query", "") + q = SearchQuerySet().models(ForumMessage).filter(text=query) + return [r.object for r in q] class ForumMainView(ListView): From 286ba91c75295071d0cf3d2e1d8cd9e39989d075 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 5 Dec 2018 14:05:47 +0100 Subject: [PATCH 02/27] Adapt forum search to xapian and improve query --- core/middleware.py | 2 +- core/templates/search/indexes/forum/forummessage_text.txt | 3 +++ forum/views.py | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/core/middleware.py b/core/middleware.py index d6479009..37c01ff9 100644 --- a/core/middleware.py +++ b/core/middleware.py @@ -27,7 +27,7 @@ from django.conf import settings from django.utils.functional import SimpleLazyObject from django.contrib.auth import get_user from django.contrib.auth.middleware import ( - AuthenticationMiddleware as DjangoAuthenticationMiddleware + AuthenticationMiddleware as DjangoAuthenticationMiddleware, ) module, klass = settings.AUTH_ANONYMOUS_MODEL.rsplit(".", 1) diff --git a/core/templates/search/indexes/forum/forummessage_text.txt b/core/templates/search/indexes/forum/forummessage_text.txt index 2df962ae..c74419ce 100644 --- a/core/templates/search/indexes/forum/forummessage_text.txt +++ b/core/templates/search/indexes/forum/forummessage_text.txt @@ -1 +1,4 @@ +{{ object.topic }} +{{ object.title }} {{ object.message }} +{{ object.author }} diff --git a/forum/views.py b/forum/views.py index d0296016..44352f17 100644 --- a/forum/views.py +++ b/forum/views.py @@ -47,7 +47,7 @@ class ForumSearchView(ListView): def get_queryset(self): query = self.request.GET.get("query", "") - q = SearchQuerySet().models(ForumMessage).filter(text=query) + q = SearchQuerySet().models(ForumMessage).filter_or(text__contains=query) return [r.object for r in q] From 404b825de4d293ad4e3637bf012610c149b0bdc3 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 5 Dec 2018 17:56:09 +0100 Subject: [PATCH 03/27] forum: make search fuzzy --- core/search_indexes.py | 1 + core/templates/search/indexes/forum/forummessage_auto.txt | 4 ++++ forum/views.py | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 core/templates/search/indexes/forum/forummessage_auto.txt diff --git a/core/search_indexes.py b/core/search_indexes.py index 7c8a50aa..d765fcf9 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -59,6 +59,7 @@ class UserOnlySignalProcessor(signals.BaseSignalProcessor): class ForumMessageIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) + auto = indexes.EdgeNgramField(use_template=True) def get_model(self): return ForumMessage diff --git a/core/templates/search/indexes/forum/forummessage_auto.txt b/core/templates/search/indexes/forum/forummessage_auto.txt new file mode 100644 index 00000000..c74419ce --- /dev/null +++ b/core/templates/search/indexes/forum/forummessage_auto.txt @@ -0,0 +1,4 @@ +{{ object.topic }} +{{ object.title }} +{{ object.message }} +{{ object.author }} diff --git a/forum/views.py b/forum/views.py index 44352f17..59d5a163 100644 --- a/forum/views.py +++ b/forum/views.py @@ -47,7 +47,7 @@ class ForumSearchView(ListView): def get_queryset(self): query = self.request.GET.get("query", "") - q = SearchQuerySet().models(ForumMessage).filter_or(text__contains=query) + q = SearchQuerySet().models(ForumMessage).autocomplete(auto=query) return [r.object for r in q] From 525b047b4fff39daf5ccd344f4ae2f098a34691c Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 5 Dec 2018 18:18:24 +0100 Subject: [PATCH 04/27] forum: display search bar on every forum pages --- forum/templates/forum/forum.jinja | 3 ++- forum/templates/forum/macros.jinja | 6 ++++++ forum/templates/forum/main.jinja | 7 ++----- forum/templates/forum/reply.jinja | 3 ++- forum/templates/forum/topic.jinja | 3 ++- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/forum/templates/forum/forum.jinja b/forum/templates/forum/forum.jinja index 89a0e3a7..ca53fb7b 100644 --- a/forum/templates/forum/forum.jinja +++ b/forum/templates/forum/forum.jinja @@ -1,5 +1,5 @@ {% extends "core/base.jinja" %} -{% from 'forum/macros.jinja' import display_forum, display_topic %} +{% from 'forum/macros.jinja' import display_forum, display_topic, display_search_bar %} {% block title %} {{ forum }} @@ -12,6 +12,7 @@ > {{ f }} {% endfor %} > {{ forum }} + {{ display_search_bar() }}

{{ forum.name }}

diff --git a/forum/templates/forum/macros.jinja b/forum/templates/forum/macros.jinja index 0b6804eb..5e2cc7d5 100644 --- a/forum/templates/forum/macros.jinja +++ b/forum/templates/forum/macros.jinja @@ -155,3 +155,9 @@ {{ m.mark_as_read(user) or "" }} {% endmacro %} +{% macro display_search_bar() %} +
+ + +
+{% endmacro %} diff --git a/forum/templates/forum/main.jinja b/forum/templates/forum/main.jinja index 065007f8..c6199c16 100644 --- a/forum/templates/forum/main.jinja +++ b/forum/templates/forum/main.jinja @@ -1,6 +1,6 @@ {% extends "core/base.jinja" %} {% from 'core/macros.jinja' import user_profile_link %} -{% from 'forum/macros.jinja' import display_forum %} +{% from 'forum/macros.jinja' import display_forum, display_search_bar %} {% block title %} {% trans %}Forum{% endtrans %} @@ -12,6 +12,7 @@

{% trans %}Forum{% endtrans %}

+ {{ display_search_bar() }}

{% trans %}View last unread messages{% endtrans %} {% trans %}Favorite topics{% endtrans %} @@ -21,10 +22,6 @@ {% trans %}New forum{% endtrans %}

{% endif %} -
{% trans %}Title{% endtrans %} diff --git a/forum/templates/forum/reply.jinja b/forum/templates/forum/reply.jinja index 3a4b49de..8cafbbba 100644 --- a/forum/templates/forum/reply.jinja +++ b/forum/templates/forum/reply.jinja @@ -1,5 +1,5 @@ {% extends "core/base.jinja" %} -{% from 'forum/macros.jinja' import display_message %} +{% from 'forum/macros.jinja' import display_message, display_search_bar %} {% block title %} {% if topic %} @@ -11,6 +11,7 @@ {% block content %} {% if topic %} +{{ display_search_bar() }}

{% trans %}Forum{% endtrans %} {% for f in topic.forum.get_parent_list() %} diff --git a/forum/templates/forum/topic.jinja b/forum/templates/forum/topic.jinja index 918b9665..ba165b75 100644 --- a/forum/templates/forum/topic.jinja +++ b/forum/templates/forum/topic.jinja @@ -1,6 +1,6 @@ {% extends "core/base.jinja" %} {% from 'core/macros.jinja' import user_profile_link %} -{% from 'forum/macros.jinja' import display_message %} +{% from 'forum/macros.jinja' import display_message, display_search_bar %} {% block title %} {{ topic }} @@ -27,6 +27,7 @@ {% block content %}

+ {{ display_search_bar() }} {% trans %}Forum{% endtrans %} {% for f in topic.forum.get_parent_list()|reverse %} > {{ f }} From 3fdb83c1c2a4a4d63bd2459ebf28269ab86876d2 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Thu, 6 Dec 2018 14:22:55 +0100 Subject: [PATCH 05/27] forum and core: add access rights on search query --- core/views/__init__.py | 20 +++++++++++++++++--- forum/models.py | 6 +++--- forum/views.py | 7 ++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/core/views/__init__.py b/core/views/__init__.py index 77841dcd..639145a3 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -2,6 +2,7 @@ # # Copyright 2016,2017 # - Skia +# - Sli # # Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM, # http://ae.utbm.fr. @@ -42,6 +43,7 @@ from django.db.models import Count from core.models import Group from core.views.forms import LoginForm +from haystack.query import SearchQuerySet def forbidden(request): @@ -176,6 +178,7 @@ class CanViewMixin(View): """ def dispatch(self, request, *arg, **kwargs): + try: self.object = self.get_object() if can_view(self.object, request.user): @@ -184,13 +187,24 @@ class CanViewMixin(View): except: pass # If we get here, it's a ListView - l_id = [o.id for o in self.get_queryset() if can_view(o, request.user)] - if not l_id and self.get_queryset().count() != 0: + queryset = self.get_queryset() + + # Test if comes from a haystack query + if isinstance(queryset, SearchQuerySet): + l_id = [o.object.id for o in queryset if can_view(o.object, request.user)] + else: + l_id = [o.id for o in queryset if can_view(o, request.user)] + if not l_id and queryset.count() != 0: raise PermissionDenied self._get_queryset = self.get_queryset def get_qs(self2): - return self2._get_queryset().filter(id__in=l_id) + q = self2._get_queryset() + # Test if comes from a haystack query + if isinstance(q, SearchQuerySet): + resp = [r.object for r in q if r.object.id in l_id] + return resp + return q.filter(id__in=l_id) self.get_queryset = types.MethodType(get_qs, self) return super(CanViewMixin, self).dispatch(request, *arg, **kwargs) diff --git a/forum/models.py b/forum/models.py index 523b1169..c52f56db 100644 --- a/forum/models.py +++ b/forum/models.py @@ -331,9 +331,9 @@ class ForumMessage(models.Model): return user.can_edit(self.topic.forum) def can_be_viewed_by(self, user): - return ( - not self._deleted - ) # No need to check the real rights since it's already done by the Topic view + return not self._deleted and self.topic.can_be_viewed_by( + user + ) # Useful in search engine def can_be_moderated_by(self, user): return self.topic.forum.is_owned_by(user) or user.id == self.author.id diff --git a/forum/views.py b/forum/views.py index 59d5a163..435eba3f 100644 --- a/forum/views.py +++ b/forum/views.py @@ -2,6 +2,7 @@ # # Copyright 2016,2017,2018 # - Skia +# - Sli # # Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM, # http://ae.utbm.fr. @@ -42,13 +43,13 @@ from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta from haystack.query import SearchQuerySet -class ForumSearchView(ListView): +class ForumSearchView(CanViewMixin, ListView): template_name = "forum/search.jinja" def get_queryset(self): query = self.request.GET.get("query", "") - q = SearchQuerySet().models(ForumMessage).autocomplete(auto=query) - return [r.object for r in q] + return SearchQuerySet().models(ForumMessage).autocomplete(auto=query) + # return [r.object for r in q] class ForumMainView(ListView): From 076b10e32593a0d145c57d6daf16a99913ed0718 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Fri, 7 Dec 2018 11:36:24 +0100 Subject: [PATCH 06/27] forum and core: add a dedicated mixin to exclude unauthorized search results --- core/views/__init__.py | 36 +++++++++++++++++++++++++----------- forum/views.py | 11 ++++++++--- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/core/views/__init__.py b/core/views/__init__.py index 639145a3..79c005ea 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -189,27 +189,41 @@ class CanViewMixin(View): # If we get here, it's a ListView queryset = self.get_queryset() - # Test if comes from a haystack query - if isinstance(queryset, SearchQuerySet): - l_id = [o.object.id for o in queryset if can_view(o.object, request.user)] - else: - l_id = [o.id for o in queryset if can_view(o, request.user)] + l_id = [o.id for o in queryset if can_view(o, request.user)] if not l_id and queryset.count() != 0: raise PermissionDenied self._get_queryset = self.get_queryset def get_qs(self2): - q = self2._get_queryset() - # Test if comes from a haystack query - if isinstance(q, SearchQuerySet): - resp = [r.object for r in q if r.object.id in l_id] - return resp - return q.filter(id__in=l_id) + return self._get_queryset().filter(id__in=l_id) self.get_queryset = types.MethodType(get_qs, self) return super(CanViewMixin, self).dispatch(request, *arg, **kwargs) +class CanViewSearchMixin(View): + """ + This view removes all forbidden content from a SearchQuerySet + """ + + def dispatch(self, request, *arg, **kwargs): + + queryset = self.get_queryset() + excluded = [ + o.object.id for o in queryset if not can_view(o.object, request.user) + ] + + self._queryset = queryset + + def get_qs(self2): + q = self2._queryset.exclude(id__in=excluded) + resp = [r.object for r in q] + return resp + + self.get_queryset = types.MethodType(get_qs, self) + return super(CanViewSearchMixin, self).dispatch(request, *arg, **kwargs) + + class FormerSubscriberMixin(View): """ This view check if the user was at least an old subscriber diff --git a/forum/views.py b/forum/views.py index 435eba3f..61d87fe8 100644 --- a/forum/views.py +++ b/forum/views.py @@ -37,19 +37,24 @@ from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from ajax_select import make_ajax_field -from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin +from core.views import ( + CanViewMixin, + CanEditMixin, + CanEditPropMixin, + CanCreateMixin, + CanViewSearchMixin, +) from core.views.forms import MarkdownInput from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta from haystack.query import SearchQuerySet -class ForumSearchView(CanViewMixin, ListView): +class ForumSearchView(CanViewSearchMixin, ListView): template_name = "forum/search.jinja" def get_queryset(self): query = self.request.GET.get("query", "") return SearchQuerySet().models(ForumMessage).autocomplete(auto=query) - # return [r.object for r in q] class ForumMainView(ListView): From 1de77f2fdd0016786db00670e7d10a7785542434 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Fri, 7 Dec 2018 11:41:40 +0100 Subject: [PATCH 07/27] core: fix typo --- core/views/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/views/__init__.py b/core/views/__init__.py index 79c005ea..84cffaaf 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -195,7 +195,7 @@ class CanViewMixin(View): self._get_queryset = self.get_queryset def get_qs(self2): - return self._get_queryset().filter(id__in=l_id) + return self2._get_queryset().filter(id__in=l_id) self.get_queryset = types.MethodType(get_qs, self) return super(CanViewMixin, self).dispatch(request, *arg, **kwargs) From 884855c178f32c2176bbff2f506ddaee7d9783e4 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Fri, 7 Dec 2018 11:49:06 +0100 Subject: [PATCH 08/27] forum and core: remove CanViewSearchMixin and use specialized view instead --- core/views/__init__.py | 23 ----------------------- forum/models.py | 6 +++--- forum/views.py | 13 ++++++++++--- 3 files changed, 13 insertions(+), 29 deletions(-) diff --git a/core/views/__init__.py b/core/views/__init__.py index 84cffaaf..4b690c4c 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -201,29 +201,6 @@ class CanViewMixin(View): return super(CanViewMixin, self).dispatch(request, *arg, **kwargs) -class CanViewSearchMixin(View): - """ - This view removes all forbidden content from a SearchQuerySet - """ - - def dispatch(self, request, *arg, **kwargs): - - queryset = self.get_queryset() - excluded = [ - o.object.id for o in queryset if not can_view(o.object, request.user) - ] - - self._queryset = queryset - - def get_qs(self2): - q = self2._queryset.exclude(id__in=excluded) - resp = [r.object for r in q] - return resp - - self.get_queryset = types.MethodType(get_qs, self) - return super(CanViewSearchMixin, self).dispatch(request, *arg, **kwargs) - - class FormerSubscriberMixin(View): """ This view check if the user was at least an old subscriber diff --git a/forum/models.py b/forum/models.py index c52f56db..4ef955ba 100644 --- a/forum/models.py +++ b/forum/models.py @@ -331,9 +331,9 @@ class ForumMessage(models.Model): return user.can_edit(self.topic.forum) def can_be_viewed_by(self, user): - return not self._deleted and self.topic.can_be_viewed_by( - user - ) # Useful in search engine + # No need to check the real rights since it's already done by the Topic view + # and it impacts performances too much + return not self._deleted def can_be_moderated_by(self, user): return self.topic.forum.is_owned_by(user) or user.id == self.author.id diff --git a/forum/views.py b/forum/views.py index 61d87fe8..65b09e45 100644 --- a/forum/views.py +++ b/forum/views.py @@ -42,19 +42,26 @@ from core.views import ( CanEditMixin, CanEditPropMixin, CanCreateMixin, - CanViewSearchMixin, + can_view, ) from core.views.forms import MarkdownInput from forum.models import Forum, ForumMessage, ForumTopic, ForumMessageMeta from haystack.query import SearchQuerySet -class ForumSearchView(CanViewSearchMixin, ListView): +class ForumSearchView(ListView): template_name = "forum/search.jinja" def get_queryset(self): query = self.request.GET.get("query", "") - return SearchQuerySet().models(ForumMessage).autocomplete(auto=query) + queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query) + excluded = [ + o.object.id + for o in queryset + if not can_view(o.object.topic, self.request.user) + ] + queryset.exclude(id__in=excluded) + return [r.object for r in queryset] class ForumMainView(ListView): From 107c6c196fc130bea8af4480ec5eb15df05986eb Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Fri, 7 Dec 2018 13:16:01 +0100 Subject: [PATCH 09/27] forum: limit results and improve search engine speed --- forum/views.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/forum/views.py b/forum/views.py index 65b09e45..56245752 100644 --- a/forum/views.py +++ b/forum/views.py @@ -54,14 +54,10 @@ class ForumSearchView(ListView): def get_queryset(self): query = self.request.GET.get("query", "") - queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query) - excluded = [ - o.object.id - for o in queryset - if not can_view(o.object.topic, self.request.user) - ] - queryset.exclude(id__in=excluded) - return [r.object for r in queryset] + queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query)[:100] + return [ + r.object for r in queryset if can_view(r.object.topic, self.request.user) + ][:30] class ForumMainView(ListView): From 65a0b7b2d43c3dbf85d5ebdf473de23fbebc22ac Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Fri, 7 Dec 2018 13:28:30 +0100 Subject: [PATCH 10/27] forum: better display of search results --- core/static/core/style.scss | 18 ------------------ forum/templates/forum/search.jinja | 20 +++++++++----------- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/core/static/core/style.scss b/core/static/core/style.scss index ae172e95..dc6ef753 100644 --- a/core/static/core/style.scss +++ b/core/static/core/style.scss @@ -1476,24 +1476,6 @@ textarea { } } } - - .search-results { - width: 100%; - .result-box { - width: 100%; - } - .result-topic { - display: inline-block; - background: #9f9f9f; - width: 19%; - } - .result-message { - display: inline-block; - background: #dbdbdb; - width: 80%; - - } - } } /*------------------------------SAS------------------------------------*/ diff --git a/forum/templates/forum/search.jinja b/forum/templates/forum/search.jinja index 465f20bb..4d15714a 100644 --- a/forum/templates/forum/search.jinja +++ b/forum/templates/forum/search.jinja @@ -1,22 +1,20 @@ {% extends "core/base.jinja" %} -{% from 'forum/macros.jinja' import display_message %} +{% from 'forum/macros.jinja' import display_message, display_search_bar %} {% block content %}

+ {% if object_list|length != 0 %} +
+ {{ display_search_bar() }}
{% for m in object_list %} - {% if user.can_view(m) %} -
-
- {{ m }} -
-
- {{ m.message }} -
-
- {% endif %} + {{ display_message(m, user) }} {% endfor %}
+ {% else %} + {% trans %}No result found{% endtrans %} + {% endif %}
+ {% endblock %} \ No newline at end of file From 347caa3b6ab6915c2fdd940918168ef72f50e2e5 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Fri, 7 Dec 2018 13:34:46 +0100 Subject: [PATCH 11/27] forum and core: fix error 500 when query is empty on search --- core/views/site.py | 2 ++ forum/templates/forum/search.jinja | 2 +- forum/views.py | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/core/views/site.py b/core/views/site.py index 8eedcf67..3b75cdde 100644 --- a/core/views/site.py +++ b/core/views/site.py @@ -71,6 +71,8 @@ def notification(request, notif_id): def search_user(query, as_json=False): + if query == "": + return [] res = SearchQuerySet().models(User).autocomplete(auto=query)[:20] return [r.object for r in res] diff --git a/forum/templates/forum/search.jinja b/forum/templates/forum/search.jinja index 4d15714a..fed3803e 100644 --- a/forum/templates/forum/search.jinja +++ b/forum/templates/forum/search.jinja @@ -4,9 +4,9 @@ {% block content %}
+ {{ display_search_bar() }} {% if object_list|length != 0 %}
- {{ display_search_bar() }}
{% for m in object_list %} {{ display_message(m, user) }} diff --git a/forum/views.py b/forum/views.py index 56245752..32dab177 100644 --- a/forum/views.py +++ b/forum/views.py @@ -54,6 +54,8 @@ class ForumSearchView(ListView): def get_queryset(self): query = self.request.GET.get("query", "") + if query == "": + return [] queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query)[:100] return [ r.object for r in queryset if can_view(r.object.topic, self.request.user) From 545671bec3a6be750f80e58a91acb881e54ba290 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Fri, 7 Dec 2018 13:54:40 +0100 Subject: [PATCH 12/27] forum: workaround for building index --- core/search_indexes.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/search_indexes.py b/core/search_indexes.py index d765fcf9..dcf8662f 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -57,8 +57,18 @@ class UserOnlySignalProcessor(signals.BaseSignalProcessor): models.signals.post_delete.disconnect(self.handle_delete, sender=User) +class BigCharFieldIndex(indexes.CharField): + """ + Workaround to avoid xapian.InvalidArgument: Term too long (> 245) + See https://groups.google.com/forum/#!topic/django-haystack/hRJKcPNPXqw/discussion + """ + + def prepare(self, term): + return super(BigCharFieldIndex, self).prepare(term)[:245] + + class ForumMessageIndex(indexes.SearchIndex, indexes.Indexable): - text = indexes.CharField(document=True, use_template=True) + text = BigCharFieldIndex(document=True, use_template=True) auto = indexes.EdgeNgramField(use_template=True) def get_model(self): From 269242601ae83485bec26f0234e851f210c1d5cd Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Mon, 10 Dec 2018 10:45:38 +0100 Subject: [PATCH 13/27] forum: workaround for error while indexing badly encoded text (legacy) --- core/search_indexes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/search_indexes.py b/core/search_indexes.py index dcf8662f..080cf403 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -29,6 +29,8 @@ from haystack import indexes, signals from core.models import User from forum.models import ForumMessage +from unicodedata import normalize + class UserIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) @@ -64,7 +66,7 @@ class BigCharFieldIndex(indexes.CharField): """ def prepare(self, term): - return super(BigCharFieldIndex, self).prepare(term)[:245] + return normalize("NFKC", super(BigCharFieldIndex, self).prepare(term))[:245] class ForumMessageIndex(indexes.SearchIndex, indexes.Indexable): From ee99ec1aede518708e09c4ee9897da16827d65be Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Mon, 10 Dec 2018 13:20:10 +0100 Subject: [PATCH 14/27] forum: workaround for building index with safety margin --- core/search_indexes.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/search_indexes.py b/core/search_indexes.py index 080cf403..510cad41 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -29,8 +29,6 @@ from haystack import indexes, signals from core.models import User from forum.models import ForumMessage -from unicodedata import normalize - class UserIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) @@ -66,7 +64,7 @@ class BigCharFieldIndex(indexes.CharField): """ def prepare(self, term): - return normalize("NFKC", super(BigCharFieldIndex, self).prepare(term))[:245] + return super(BigCharFieldIndex, self).prepare(term)[:240] class ForumMessageIndex(indexes.SearchIndex, indexes.Indexable): From 641d564ec607cfdb6597e186e72ea1e5234ead7b Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Mon, 10 Dec 2018 16:06:03 +0100 Subject: [PATCH 15/27] forum: add trigger to update index on ForumMessage update --- core/search_indexes.py | 39 +++++++++++++++++++++++++++++++++++++-- sith/settings.py | 2 +- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/core/search_indexes.py b/core/search_indexes.py index 510cad41..75dc262d 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -2,6 +2,7 @@ # # Copyright 2016,2017 # - Skia +# - Sli # # Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM, # http://ae.utbm.fr. @@ -27,7 +28,7 @@ from django.db import models from haystack import indexes, signals from core.models import User -from forum.models import ForumMessage +from forum.models import ForumMessage, ForumMessageMeta class UserIndex(indexes.SearchIndex, indexes.Indexable): @@ -45,17 +46,51 @@ class UserIndex(indexes.SearchIndex, indexes.Indexable): return "last_update" -class UserOnlySignalProcessor(signals.BaseSignalProcessor): +class IndexSignalProcessor(signals.BaseSignalProcessor): def setup(self): # Listen only to the ``User`` model. models.signals.post_save.connect(self.handle_save, sender=User) models.signals.post_delete.connect(self.handle_delete, sender=User) + # Listen only to the ``ForumMessage`` model. + models.signals.post_save.connect(self.handle_save, sender=ForumMessageMeta) + models.signals.post_delete.connect(self.handle_delete, sender=ForumMessage) + + # Listen to the ``ForumMessageMeta`` model pretending it's a ``ForumMessage``. + models.signals.post_save.connect( + self.handle_forum_message_meta_save, sender=ForumMessageMeta + ) + models.signals.post_delete.connect( + self.handle_forum_message_meta_delete, sender=ForumMessageMeta + ) + def teardown(self): # Disconnect only for the ``User`` model. models.signals.post_save.disconnect(self.handle_save, sender=User) models.signals.post_delete.disconnect(self.handle_delete, sender=User) + # Disconnect only to the ``ForumMessage`` model. + models.signals.post_save.disconnect(self.handle_save, sender=ForumMessage) + models.signals.post_delete.disconnect(self.handle_delete, sender=ForumMessage) + + # Disconnect to the ``ForumMessageMeta`` model pretending it's a ``ForumMessage``. + models.signals.post_save.disconnect( + self.handle_forum_message_meta_save, sender=ForumMessageMeta + ) + models.signals.post_delete.disconnect( + self.handle_forum_message_meta_delete, sender=ForumMessageMeta + ) + + def handle_forum_message_meta_save(self, sender, instance, **kwargs): + super(IndexSignalProcessor, self).handle_save( + ForumMessage, instance.message, **kwargs + ) + + def handle_forum_message_meta_delete(self, sender, instance, **kwargs): + super(IndexSignalProcessor, self).handle_delete( + ForumMessage, instance.message, **kwargs + ) + class BigCharFieldIndex(indexes.CharField): """ diff --git a/sith/settings.py b/sith/settings.py index 07532a4c..ee319665 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -190,7 +190,7 @@ HAYSTACK_CONNECTIONS = { } } -HAYSTACK_SIGNAL_PROCESSOR = "core.search_indexes.UserOnlySignalProcessor" +HAYSTACK_SIGNAL_PROCESSOR = "core.search_indexes.IndexSignalProcessor" SASS_PRECISION = 8 From 57454bffa0a6cdf87769d3459fa96ae27b81886d Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Tue, 11 Dec 2018 14:02:15 +0100 Subject: [PATCH 16/27] forum: workaround size error while building index, handle malformed utf8 --- core/search_indexes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/search_indexes.py b/core/search_indexes.py index 75dc262d..86401eed 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -99,7 +99,9 @@ class BigCharFieldIndex(indexes.CharField): """ def prepare(self, term): - return super(BigCharFieldIndex, self).prepare(term)[:240] + return bytes(super(BigCharFieldIndex, self).prepare(term), "utf-8")[ + :245 + ].decode("utf-8", errors="ignore") class ForumMessageIndex(indexes.SearchIndex, indexes.Indexable): From a9bae46f4573271ba6d7bcf4b7fe4b3b263fc16b Mon Sep 17 00:00:00 2001 From: Soldat Date: Tue, 11 Dec 2018 20:08:24 +0100 Subject: [PATCH 17/27] changed design --- core/static/core/style.scss | 12 ++ forum/templates/forum/forum.jinja | 2 +- forum/templates/forum/macros.jinja | 6 +- forum/templates/forum/main.jinja | 2 +- locale/fr/LC_MESSAGES/django.po | 190 +++++++++++++++-------------- 5 files changed, 119 insertions(+), 93 deletions(-) diff --git a/core/static/core/style.scss b/core/static/core/style.scss index dc6ef753..7ac05a0c 100644 --- a/core/static/core/style.scss +++ b/core/static/core/style.scss @@ -1403,6 +1403,18 @@ textarea { } } + .search_bar { + margin: 10px 0px; + display: flex; + height: 20p; + align-items: center; + } + .search_check { + margin-left: 10px; + } + .search_bouton { + margin-left: 10px; + } .category { margin-top: 5px; background: $secondary-color; diff --git a/forum/templates/forum/forum.jinja b/forum/templates/forum/forum.jinja index ca53fb7b..d3fe8b90 100644 --- a/forum/templates/forum/forum.jinja +++ b/forum/templates/forum/forum.jinja @@ -12,7 +12,6 @@ > {{ f }} {% endfor %} > {{ forum }} - {{ display_search_bar() }}

{{ forum.name }}

@@ -23,6 +22,7 @@ {% if not forum.is_category %} {% trans %}New topic{% endtrans %} {% endif %} + {{ display_search_bar() }}

{% if forum.children.exists() %}
diff --git a/forum/templates/forum/macros.jinja b/forum/templates/forum/macros.jinja index 5e2cc7d5..a70881b0 100644 --- a/forum/templates/forum/macros.jinja +++ b/forum/templates/forum/macros.jinja @@ -156,8 +156,10 @@ {% endmacro %} {% macro display_search_bar() %} -
+ - + {% trans %}Order by date{% endtrans %}
+ +
{% endmacro %} diff --git a/forum/templates/forum/main.jinja b/forum/templates/forum/main.jinja index c6199c16..7beb5543 100644 --- a/forum/templates/forum/main.jinja +++ b/forum/templates/forum/main.jinja @@ -12,10 +12,10 @@

{% trans %}Forum{% endtrans %}

- {{ display_search_bar() }}

{% trans %}View last unread messages{% endtrans %} {% trans %}Favorite topics{% endtrans %} + {{ display_search_bar() }}

{% if user.is_in_group(settings.SITH_GROUP_FORUM_ADMIN_ID) or user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) %}

diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index b64e86a1..b2842057 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-11-08 00:26+0100\n" +"POT-Creation-Date: 2018-12-11 20:07+0100\n" "PO-Revision-Date: 2016-07-18\n" "Last-Translator: Skia \n" "Language-Team: AE info \n" @@ -219,7 +219,7 @@ msgstr "Compte" msgid "Company" msgstr "Entreprise" -#: accounting/models.py:317 sith/settings.py:377 +#: accounting/models.py:317 sith/settings.py:378 #: stock/templates/stock/shopping_list_items.jinja:37 msgid "Other" msgstr "Autre" @@ -776,7 +776,7 @@ msgstr "Opération liée : " #: core/templates/core/user_preferences.jinja:12 #: core/templates/core/user_preferences.jinja:19 #: counter/templates/counter/cash_register_summary.jinja:22 -#: forum/templates/forum/reply.jinja:33 +#: forum/templates/forum/reply.jinja:34 #: subscription/templates/subscription/subscription.jinja:25 #: trombi/templates/trombi/comment.jinja:26 #: trombi/templates/trombi/edit_profile.jinja:13 @@ -1492,9 +1492,9 @@ msgstr "Type" #: com/templates/com/news_admin_list.jinja:249 #: com/templates/com/news_admin_list.jinja:286 #: com/templates/com/weekmail.jinja:19 com/templates/com/weekmail.jinja:48 -#: core/templates/core/base.jinja:341 forum/templates/forum/forum.jinja:29 -#: forum/templates/forum/forum.jinja:48 forum/templates/forum/main.jinja:26 -#: forum/views.py:192 +#: core/templates/core/base.jinja:341 forum/templates/forum/forum.jinja:30 +#: forum/templates/forum/forum.jinja:49 forum/templates/forum/main.jinja:27 +#: forum/views.py:213 msgid "Title" msgstr "Titre" @@ -1518,7 +1518,7 @@ msgstr "Résumé" #: com/templates/com/news_admin_list.jinja:252 #: com/templates/com/news_admin_list.jinja:289 #: com/templates/com/weekmail.jinja:17 com/templates/com/weekmail.jinja:46 -#: forum/templates/forum/forum.jinja:52 +#: forum/templates/forum/forum.jinja:53 msgid "Author" msgstr "Auteur" @@ -1628,7 +1628,7 @@ msgstr "" #: com/templates/com/news_edit.jinja:56 com/templates/com/weekmail.jinja:10 #: core/templates/core/macros_pages.jinja:49 -#: forum/templates/forum/reply.jinja:32 +#: forum/templates/forum/reply.jinja:33 msgid "Preview" msgstr "Prévisualiser" @@ -2113,7 +2113,7 @@ msgstr "Un utilisateur de ce nom d'utilisateur existe déjà" #: core/templates/core/user_edit.jinja:17 #: election/templates/election/election_detail.jinja:340 #: forum/templates/forum/macros.jinja:93 forum/templates/forum/macros.jinja:95 -#: forum/templates/forum/reply.jinja:38 forum/templates/forum/reply.jinja:40 +#: forum/templates/forum/reply.jinja:39 forum/templates/forum/reply.jinja:41 #: trombi/templates/trombi/user_tools.jinja:41 msgid "Profile" msgstr "Profil" @@ -2310,6 +2310,8 @@ msgid "Register" msgstr "S'enregister" #: core/templates/core/base.jinja:75 core/templates/core/base.jinja:76 +#: forum/templates/forum/macros.jinja:160 +#: forum/templates/forum/macros.jinja:162 #: matmat/templates/matmat/search_form.jinja:37 #: matmat/templates/matmat/search_form.jinja:47 #: matmat/templates/matmat/search_form.jinja:58 @@ -2370,8 +2372,8 @@ msgstr "GA" #: forum/templates/forum/forum.jinja:10 #: forum/templates/forum/last_unread.jinja:14 #: forum/templates/forum/main.jinja:6 forum/templates/forum/main.jinja:11 -#: forum/templates/forum/main.jinja:14 forum/templates/forum/reply.jinja:15 -#: forum/templates/forum/topic.jinja:30 +#: forum/templates/forum/main.jinja:14 forum/templates/forum/reply.jinja:16 +#: forum/templates/forum/topic.jinja:31 msgid "Forum" msgstr "Forum" @@ -2385,7 +2387,7 @@ msgstr "Photos" #: eboutic/templates/eboutic/eboutic_main.jinja:24 #: eboutic/templates/eboutic/eboutic_makecommand.jinja:8 #: eboutic/templates/eboutic/eboutic_payment_result.jinja:4 -#: sith/settings.py:376 sith/settings.py:384 +#: sith/settings.py:377 sith/settings.py:385 msgid "Eboutic" msgstr "Eboutic" @@ -3589,8 +3591,8 @@ msgstr "quantité" msgid "Sith account" msgstr "Compte utilisateur" -#: counter/models.py:448 sith/settings.py:369 sith/settings.py:374 -#: sith/settings.py:392 +#: counter/models.py:448 sith/settings.py:370 sith/settings.py:375 +#: sith/settings.py:393 msgid "Credit card" msgstr "Carte bancaire" @@ -4352,25 +4354,25 @@ msgstr "dernière date de lecture" msgid "Favorite topics" msgstr "Topics favoris" -#: forum/templates/forum/forum.jinja:20 forum/templates/forum/main.jinja:21 +#: forum/templates/forum/forum.jinja:20 forum/templates/forum/main.jinja:22 msgid "New forum" msgstr "Nouveau forum" #: forum/templates/forum/forum.jinja:23 forum/templates/forum/reply.jinja:8 -#: forum/templates/forum/reply.jinja:27 +#: forum/templates/forum/reply.jinja:28 msgid "New topic" msgstr "Nouveau sujet" -#: forum/templates/forum/forum.jinja:33 forum/templates/forum/main.jinja:30 +#: forum/templates/forum/forum.jinja:34 forum/templates/forum/main.jinja:31 msgid "Topics" msgstr "Sujets" -#: forum/templates/forum/forum.jinja:36 forum/templates/forum/forum.jinja:58 -#: forum/templates/forum/main.jinja:33 +#: forum/templates/forum/forum.jinja:37 forum/templates/forum/forum.jinja:59 +#: forum/templates/forum/main.jinja:34 msgid "Last message" msgstr "Dernier message" -#: forum/templates/forum/forum.jinja:55 +#: forum/templates/forum/forum.jinja:56 msgid "Messages" msgstr "Messages" @@ -4400,28 +4402,38 @@ msgstr " le " msgid "Deleted or unreadable message." msgstr "Message supprimé ou non-visible." +#: forum/templates/forum/macros.jinja:161 +msgid "Order by date" +msgstr "Trier par date" + #: forum/templates/forum/main.jinja:16 msgid "View last unread messages" msgstr "Voir les derniers messages non lus" -#: forum/templates/forum/reply.jinja:6 forum/templates/forum/reply.jinja:24 -#: forum/templates/forum/topic.jinja:41 forum/templates/forum/topic.jinja:66 +#: forum/templates/forum/reply.jinja:6 forum/templates/forum/reply.jinja:25 +#: forum/templates/forum/topic.jinja:42 forum/templates/forum/topic.jinja:67 msgid "Reply" msgstr "Répondre" -#: forum/templates/forum/topic.jinja:43 +#: forum/templates/forum/search.jinja:16 +#, fuzzy +#| msgid "Not found" +msgid "No result found" +msgstr "Non trouvé" + +#: forum/templates/forum/topic.jinja:44 msgid "Unmark as favorite" msgstr "Enlever des favoris" -#: forum/templates/forum/topic.jinja:45 +#: forum/templates/forum/topic.jinja:46 msgid "Mark as favorite" msgstr "Ajouter aux favoris" -#: forum/views.py:138 +#: forum/views.py:159 msgid "Apply rights and club owner recursively" msgstr "Appliquer les droits et le club propriétaire récursivement" -#: forum/views.py:356 +#: forum/views.py:377 #, python-format msgid "%(author)s said" msgstr "Citation de %(author)s" @@ -4475,12 +4487,12 @@ msgid "Washing and drying" msgstr "Lavage et séchage" #: launderette/templates/launderette/launderette_book.jinja:27 -#: sith/settings.py:520 +#: sith/settings.py:521 msgid "Washing" msgstr "Lavage" #: launderette/templates/launderette/launderette_book.jinja:31 -#: sith/settings.py:520 +#: sith/settings.py:521 msgid "Drying" msgstr "Séchage" @@ -4660,251 +4672,251 @@ msgstr "Erreur de création de l'album %(album)s : %(msg)s" msgid "Add user" msgstr "Ajouter une personne" -#: sith/settings.py:215 +#: sith/settings.py:216 msgid "English" msgstr "Anglais" -#: sith/settings.py:215 +#: sith/settings.py:216 msgid "French" msgstr "Français" -#: sith/settings.py:350 +#: sith/settings.py:351 msgid "TC" msgstr "TC" -#: sith/settings.py:351 +#: sith/settings.py:352 msgid "IMSI" msgstr "IMSI" -#: sith/settings.py:352 +#: sith/settings.py:353 msgid "IMAP" msgstr "IMAP" -#: sith/settings.py:353 +#: sith/settings.py:354 msgid "INFO" msgstr "INFO" -#: sith/settings.py:354 +#: sith/settings.py:355 msgid "GI" msgstr "GI" -#: sith/settings.py:355 +#: sith/settings.py:356 msgid "E" msgstr "E" -#: sith/settings.py:356 +#: sith/settings.py:357 msgid "EE" msgstr "EE" -#: sith/settings.py:357 +#: sith/settings.py:358 msgid "GESC" msgstr "GESC" -#: sith/settings.py:358 +#: sith/settings.py:359 msgid "GMC" msgstr "GMC" -#: sith/settings.py:359 +#: sith/settings.py:360 msgid "MC" msgstr "MC" -#: sith/settings.py:360 +#: sith/settings.py:361 msgid "EDIM" msgstr "EDIM" -#: sith/settings.py:361 +#: sith/settings.py:362 msgid "Humanities" msgstr "Humanités" -#: sith/settings.py:362 +#: sith/settings.py:363 msgid "N/A" msgstr "N/A" -#: sith/settings.py:366 sith/settings.py:373 sith/settings.py:390 +#: sith/settings.py:367 sith/settings.py:374 sith/settings.py:391 msgid "Check" msgstr "Chèque" -#: sith/settings.py:367 sith/settings.py:375 sith/settings.py:391 +#: sith/settings.py:368 sith/settings.py:376 sith/settings.py:392 msgid "Cash" msgstr "Espèces" -#: sith/settings.py:368 +#: sith/settings.py:369 msgid "Transfert" msgstr "Virement" -#: sith/settings.py:381 +#: sith/settings.py:382 msgid "Belfort" msgstr "Belfort" -#: sith/settings.py:382 +#: sith/settings.py:383 msgid "Sevenans" msgstr "Sevenans" -#: sith/settings.py:383 +#: sith/settings.py:384 msgid "Montbéliard" msgstr "Montbéliard" -#: sith/settings.py:434 +#: sith/settings.py:435 msgid "One semester" msgstr "Un semestre, 15 €" -#: sith/settings.py:435 +#: sith/settings.py:436 msgid "Two semesters" msgstr "Deux semestres, 28 €" -#: sith/settings.py:437 +#: sith/settings.py:438 msgid "Common core cursus" msgstr "Cursus tronc commun, 45 €" -#: sith/settings.py:441 +#: sith/settings.py:442 msgid "Branch cursus" msgstr "Cursus branche, 45 €" -#: sith/settings.py:442 +#: sith/settings.py:443 msgid "Alternating cursus" msgstr "Cursus alternant, 30 €" -#: sith/settings.py:443 +#: sith/settings.py:444 msgid "Honorary member" msgstr "Membre honoraire, 0 €" -#: sith/settings.py:444 +#: sith/settings.py:445 msgid "Assidu member" msgstr "Membre d'Assidu, 0 €" -#: sith/settings.py:445 +#: sith/settings.py:446 msgid "Amicale/DOCEO member" msgstr "Membre de l'Amicale/DOCEO, 0 €" -#: sith/settings.py:446 +#: sith/settings.py:447 msgid "UT network member" msgstr "Cotisant du réseau UT, 0 €" -#: sith/settings.py:447 +#: sith/settings.py:448 msgid "CROUS member" msgstr "Membres du CROUS, 0 €" -#: sith/settings.py:448 +#: sith/settings.py:449 msgid "Sbarro/ESTA member" msgstr "Membre de Sbarro ou de l'ESTA, 15 €" -#: sith/settings.py:450 +#: sith/settings.py:451 msgid "One semester Welcome Week" msgstr "Un semestre Welcome Week" -#: sith/settings.py:454 +#: sith/settings.py:455 msgid "Two months for free" msgstr "Deux mois gratuits" -#: sith/settings.py:455 +#: sith/settings.py:456 msgid "Eurok's volunteer" msgstr "Bénévole Eurockéennes" -#: sith/settings.py:457 +#: sith/settings.py:458 msgid "Six weeks for free" msgstr "6 semaines gratuites" -#: sith/settings.py:461 +#: sith/settings.py:462 msgid "One day" msgstr "Un jour" -#: sith/settings.py:480 +#: sith/settings.py:481 msgid "President" msgstr "Président" -#: sith/settings.py:481 +#: sith/settings.py:482 msgid "Vice-President" msgstr "Vice-Président" -#: sith/settings.py:482 +#: sith/settings.py:483 msgid "Treasurer" msgstr "Trésorier" -#: sith/settings.py:483 +#: sith/settings.py:484 msgid "Communication supervisor" msgstr "Responsable communication" -#: sith/settings.py:484 +#: sith/settings.py:485 msgid "Secretary" msgstr "Secrétaire" -#: sith/settings.py:485 +#: sith/settings.py:486 msgid "IT supervisor" msgstr "Responsable info" -#: sith/settings.py:486 +#: sith/settings.py:487 msgid "Board member" msgstr "Membre du bureau" -#: sith/settings.py:487 +#: sith/settings.py:488 msgid "Active member" msgstr "Membre actif" -#: sith/settings.py:488 +#: sith/settings.py:489 msgid "Curious" msgstr "Curieux" -#: sith/settings.py:524 +#: sith/settings.py:525 msgid "A new poster needs to be moderated" msgstr "Une nouvelle affiche a besoin d'être modérée" -#: sith/settings.py:525 +#: sith/settings.py:526 msgid "A new mailing list needs to be moderated" msgstr "Une nouvelle mailing list a besoin d'être modérée" -#: sith/settings.py:526 +#: sith/settings.py:527 #, python-format msgid "There are %s fresh news to be moderated" msgstr "Il y a %s nouvelles toutes fraîches à modérer" -#: sith/settings.py:527 +#: sith/settings.py:528 msgid "New files to be moderated" msgstr "Nouveaux fichiers à modérer" -#: sith/settings.py:528 +#: sith/settings.py:529 #, python-format msgid "There are %s pictures to be moderated in the SAS" msgstr "Il y a %s photos à modérer dans le SAS" -#: sith/settings.py:529 +#: sith/settings.py:530 msgid "You've been identified on some pictures" msgstr "Vous avez été identifié sur des photos" -#: sith/settings.py:530 +#: sith/settings.py:531 #, python-format msgid "You just refilled of %s €" msgstr "Vous avez rechargé votre compte de %s€" -#: sith/settings.py:531 +#: sith/settings.py:532 #, python-format msgid "You just bought %s" msgstr "Vous avez acheté %s" -#: sith/settings.py:532 +#: sith/settings.py:533 msgid "You have a notification" msgstr "Vous avez une notification" -#: sith/settings.py:544 +#: sith/settings.py:545 msgid "Success!" msgstr "Succès !" -#: sith/settings.py:545 +#: sith/settings.py:546 msgid "Fail!" msgstr "Échec !" -#: sith/settings.py:546 +#: sith/settings.py:547 msgid "You successfully posted an article in the Weekmail" msgstr "Article posté avec succès dans le Weekmail" -#: sith/settings.py:547 +#: sith/settings.py:548 msgid "You successfully edited an article in the Weekmail" msgstr "Article édité avec succès dans le Weekmail" -#: sith/settings.py:548 +#: sith/settings.py:549 msgid "You successfully sent the Weekmail" msgstr "Weekmail envoyé avec succès" -#: sith/settings.py:556 +#: sith/settings.py:557 msgid "AE tee-shirt" msgstr "Tee-shirt AE" From e421a2b4cdfd7b4dbc4401f95acd0019a265948d Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 12 Dec 2018 15:31:22 +0100 Subject: [PATCH 18/27] forum: increase search speed by optimizing permission filter --- core/views/__init__.py | 1 - forum/views.py | 22 ++++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/views/__init__.py b/core/views/__init__.py index 4b690c4c..2d764237 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -43,7 +43,6 @@ from django.db.models import Count from core.models import Group from core.views.forms import LoginForm -from haystack.query import SearchQuerySet def forbidden(request): diff --git a/forum/views.py b/forum/views.py index 32dab177..6289c42f 100644 --- a/forum/views.py +++ b/forum/views.py @@ -56,10 +56,24 @@ class ForumSearchView(ListView): query = self.request.GET.get("query", "") if query == "": return [] - queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query)[:100] - return [ - r.object for r in queryset if can_view(r.object.topic, self.request.user) - ][:30] + queryset = ( + SearchQuerySet().models(ForumMessage).autocomplete(auto=query).load_all() + ) + + # Filter unauthorized responses + resp = [] + count = 0 + max_count = 30 + for r in queryset: + if count >= max_count: + return resp + if can_view(r.object, self.request.user) and can_view( + r.object.topic, self.request.user + ): + resp.append(r.object) + count += 1 + + return resp class ForumMainView(ListView): From 721b22a1e9dad5aea17bb9dd3cd113c514630f8a Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 12 Dec 2018 15:45:05 +0100 Subject: [PATCH 19/27] forum: improve search bar UX behavior --- forum/templates/forum/macros.jinja | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/forum/templates/forum/macros.jinja b/forum/templates/forum/macros.jinja index a70881b0..6f287ebb 100644 --- a/forum/templates/forum/macros.jinja +++ b/forum/templates/forum/macros.jinja @@ -157,9 +157,31 @@ {% macro display_search_bar() %}

{% endmacro %} From 68911749351321be6133383771fa3fdfd2c6d241 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 12 Dec 2018 15:48:11 +0100 Subject: [PATCH 20/27] forum: implement order by date for search --- core/search_indexes.py | 1 + forum/views.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/search_indexes.py b/core/search_indexes.py index 86401eed..7bece290 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -107,6 +107,7 @@ class BigCharFieldIndex(indexes.CharField): class ForumMessageIndex(indexes.SearchIndex, indexes.Indexable): text = BigCharFieldIndex(document=True, use_template=True) auto = indexes.EdgeNgramField(use_template=True) + date = indexes.DateTimeField(model_attr="date") def get_model(self): return ForumMessage diff --git a/forum/views.py b/forum/views.py index 6289c42f..a1011c05 100644 --- a/forum/views.py +++ b/forum/views.py @@ -54,11 +54,17 @@ class ForumSearchView(ListView): def get_queryset(self): query = self.request.GET.get("query", "") + order_by = self.request.GET.get("order", "") + if query == "": return [] - queryset = ( - SearchQuerySet().models(ForumMessage).autocomplete(auto=query).load_all() - ) + + queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query) + + if order_by == "date": + queryset.order_by("date") + + queryset = queryset.load_all() # Filter unauthorized responses resp = [] From fc7e45190d1a0eae8b701b3d26a670013e10beda Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 12 Dec 2018 16:54:19 +0100 Subject: [PATCH 21/27] forum: use pure jinja for search bar --- forum/templates/forum/forum.jinja | 2 +- forum/templates/forum/macros.jinja | 31 +++++------------------------- forum/templates/forum/main.jinja | 2 +- forum/templates/forum/reply.jinja | 2 +- forum/templates/forum/search.jinja | 2 +- forum/templates/forum/topic.jinja | 2 +- 6 files changed, 10 insertions(+), 31 deletions(-) diff --git a/forum/templates/forum/forum.jinja b/forum/templates/forum/forum.jinja index d3fe8b90..6cb8287d 100644 --- a/forum/templates/forum/forum.jinja +++ b/forum/templates/forum/forum.jinja @@ -22,7 +22,7 @@ {% if not forum.is_category %} {% trans %}New topic{% endtrans %} {% endif %} - {{ display_search_bar() }} + {{ display_search_bar(request) }}

{% if forum.children.exists() %}
diff --git a/forum/templates/forum/macros.jinja b/forum/templates/forum/macros.jinja index 6f287ebb..f3ebb000 100644 --- a/forum/templates/forum/macros.jinja +++ b/forum/templates/forum/macros.jinja @@ -155,33 +155,12 @@ {{ m.mark_as_read(user) or "" }} {% endmacro %} -{% macro display_search_bar() %} +{% macro display_search_bar(request) %} {% endmacro %} diff --git a/forum/templates/forum/main.jinja b/forum/templates/forum/main.jinja index 7beb5543..8bedc310 100644 --- a/forum/templates/forum/main.jinja +++ b/forum/templates/forum/main.jinja @@ -15,7 +15,7 @@

{% trans %}View last unread messages{% endtrans %} {% trans %}Favorite topics{% endtrans %} - {{ display_search_bar() }} + {{ display_search_bar(request) }}

{% if user.is_in_group(settings.SITH_GROUP_FORUM_ADMIN_ID) or user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) %}

diff --git a/forum/templates/forum/reply.jinja b/forum/templates/forum/reply.jinja index 8cafbbba..b2ea3f52 100644 --- a/forum/templates/forum/reply.jinja +++ b/forum/templates/forum/reply.jinja @@ -11,7 +11,7 @@ {% block content %} {% if topic %} -{{ display_search_bar() }} +{{ display_search_bar(request) }}

{% trans %}Forum{% endtrans %} {% for f in topic.forum.get_parent_list() %} diff --git a/forum/templates/forum/search.jinja b/forum/templates/forum/search.jinja index fed3803e..57fd1c14 100644 --- a/forum/templates/forum/search.jinja +++ b/forum/templates/forum/search.jinja @@ -4,7 +4,7 @@ {% block content %}

- {{ display_search_bar() }} + {{ display_search_bar(request) }} {% if object_list|length != 0 %}
diff --git a/forum/templates/forum/topic.jinja b/forum/templates/forum/topic.jinja index ba165b75..7692898d 100644 --- a/forum/templates/forum/topic.jinja +++ b/forum/templates/forum/topic.jinja @@ -27,7 +27,7 @@ {% block content %}

- {{ display_search_bar() }} + {{ display_search_bar(request) }} {% trans %}Forum{% endtrans %} {% for f in topic.forum.get_parent_list()|reverse %} > {{ f }} From 068a80519c72e2e667c57161028ce58e1da539e0 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Thu, 13 Dec 2018 10:12:20 +0100 Subject: [PATCH 22/27] forum: fix order by date --- forum/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forum/views.py b/forum/views.py index a1011c05..94d5ee68 100644 --- a/forum/views.py +++ b/forum/views.py @@ -62,7 +62,7 @@ class ForumSearchView(ListView): queryset = SearchQuerySet().models(ForumMessage).autocomplete(auto=query) if order_by == "date": - queryset.order_by("date") + queryset = queryset.order_by("-date") queryset = queryset.load_all() From 82ffd75372c43664325c1828452eebd74c39b8c2 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Thu, 13 Dec 2018 14:06:33 +0100 Subject: [PATCH 23/27] forum: fix typo in search bar checkbox class name --- forum/templates/forum/macros.jinja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forum/templates/forum/macros.jinja b/forum/templates/forum/macros.jinja index f3ebb000..48a6d3ce 100644 --- a/forum/templates/forum/macros.jinja +++ b/forum/templates/forum/macros.jinja @@ -158,7 +158,7 @@ {% macro display_search_bar(request) %}