Use Django-Haystack to make indexed researches

This commit is contained in:
Yohann D'ANELLO 2020-10-15 12:57:20 +02:00
parent b397d00011
commit 6c59bf11be
7 changed files with 90 additions and 1 deletions

View File

@ -1,5 +1,7 @@
import re
from django.urls import reverse_lazy
from corres2math.lists import get_sympa_client
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import RegexValidator
@ -56,6 +58,9 @@ class Team(models.Model):
return super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse_lazy("participation:team_detail", args=(self.pk,))
def __str__(self):
return _("Team {name} ({trigram})").format(name=self.name, trigram=self.trigram)
@ -115,6 +120,9 @@ class Participation(models.Model):
verbose_name=_("synthesis video"),
)
def get_absolute_url(self):
return reverse_lazy("participation:team_detail", args=(self.pk,))
def __str__(self):
return _("Participation of the team {name} ({trigram})").format(name=self.team.name, trigram=self.team.trigram)

View File

@ -0,0 +1,18 @@
from haystack import indexes
from .models import Participation, Team, Video
class TeamIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr="name")
trigram = indexes.CharField(model_attr="trigram")
def get_model(self):
return Team
class ParticipationIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr="team__trigram")
def get_model(self):
return Participation

View File

@ -0,0 +1,13 @@
from haystack import indexes
from .models import Registration
class RegistrationIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr="user__username")
last_name = indexes.CharField(model_attr="user__last_name")
first_name = indexes.CharField(model_attr="user__first_name")
email = indexes.CharField(model_attr="user__email")
def get_model(self):
return Registration

View File

@ -54,6 +54,7 @@ INSTALLED_APPS = [
'crispy_forms',
'django_extensions',
'django_tables2',
'haystack',
'logs',
'mailer',
'polymorphic',
@ -181,6 +182,13 @@ CRISPY_TEMPLATE_PACK = 'bootstrap4'
DJANGO_TABLES2_TEMPLATE = 'django_tables2/bootstrap4.html'
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
}
}
_db_type = os.getenv('DJANGO_DB_TYPE', 'sqlite').lower()
if _db_type == 'mysql' or _db_type.startswith('postgres') or _db_type == 'psql':

View File

@ -26,6 +26,7 @@ urlpatterns = [
path('admin/doc/', include('django.contrib.admindocs.urls')),
path('admin/', admin.site.urls, name="admin"),
path('accounts/', include('django.contrib.auth.urls')),
path('search/', include('haystack.urls')),
path('api/', include('api.urls')),
path('participation/', include('participation.urls')),

View File

@ -3,6 +3,7 @@ django-bootstrap-datepicker-plus
django-crispy-forms
django-extensions
django-filter
django-haystack
django-mailer
django-polymorphic
django-tables2
@ -10,4 +11,5 @@ djangorestframework
django-rest-polymorphic
ptpython
python-magic
gunicorn
gunicorn
whoosh

View File

@ -0,0 +1,39 @@
{% extends 'base.html' %}
{% block content %}
<h2>Search</h2>
<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="Search">
</td>
</tr>
</table>
{% if query %}
<h3>Results</h3>
{% for result in page.object_list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.text }}</a>
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}
{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
</div>
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>
{% endblock %}