From fb91dc3111911089bb8c96f7b9d76569c89dc1a9 Mon Sep 17 00:00:00 2001 From: Evarin Date: Fri, 16 Jun 2017 00:17:25 +0200 Subject: [PATCH] =?UTF-8?q?Recherche=20g=C3=A9n=C3=A9rique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- avisstage/documents.py | 12 +++++- .../avisstage/liste/recherche_resultats.html | 38 +++++++++++++++++++ avisstage/views.py | 8 +--- avisstage/views_search.py | 34 +++++++++++++++++ 4 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 avisstage/templates/avisstage/liste/recherche_resultats.html create mode 100644 avisstage/views_search.py diff --git a/avisstage/documents.py b/avisstage/documents.py index 75a0aaf..6886073 100644 --- a/avisstage/documents.py +++ b/avisstage/documents.py @@ -1,4 +1,5 @@ from django_elasticsearch_dsl import DocType, Index, fields +from elasticsearch_dsl import analyzer, token_filter, tokenizer from .models import Stage, AvisStage, AvisLieu stage = Index('stages') @@ -6,6 +7,13 @@ stage.settings( number_of_shards=1, number_of_replicas=0 ) + +text_analyzer = analyzer( + 'texte', + tokenizer="standard", + filters=['lowercase', 'standard', 'asciifolding', + token_filter("frsnow", type="snowball", language="French")]) +stage.analyzer(text_analyzer) @stage.doc_type class StageDocument(DocType): @@ -27,10 +35,10 @@ class StageDocument(DocType): ] def prepare_thematiques(self, instance): - return ", ".join(instance.thematiques.all().values_list("slug", flat=True)) + return ", ".join(instance.thematiques.all().values_list("name", flat=True)) def prepare_matieres(self, instance): - return ", ".join(instance.matieres.all().values_list("slug", flat=True)) + return ", ".join(instance.matieres.all().values_list("nom", flat=True)) def prepare_niveau_scol(self, instance): return instance.get_niveau_scol_display() diff --git a/avisstage/templates/avisstage/liste/recherche_resultats.html b/avisstage/templates/avisstage/liste/recherche_resultats.html new file mode 100644 index 0000000..b1837d4 --- /dev/null +++ b/avisstage/templates/avisstage/liste/recherche_resultats.html @@ -0,0 +1,38 @@ +{% extends "avisstage/base.html" %} +{% load staticfiles %} + +{% block title %}Dernières mises à jour - ExperiENS{% endblock %} + +{% block content %} + +

Résultats de la recherche

+
+

{{ form.generique }}

+
+
+ +
+{% endblock %} diff --git a/avisstage/views.py b/avisstage/views.py index 2e3d5d1..daf4aaf 100644 --- a/avisstage/views.py +++ b/avisstage/views.py @@ -14,6 +14,7 @@ from django.db.models import Q from avisstage.models import Normalien, Stage, Lieu, AvisLieu, AvisStage from avisstage.forms import StageForm, LieuForm, AvisStageForm, AvisLieuForm, FeedbackForm +from avisstage.views_search import * import random, math @@ -60,11 +61,6 @@ class StageListe(LoginRequiredMixin, ListView): def get_queryset(self): return Stage.objects.filter(public=True).order_by('-date_maj') -# Recherche -@login_required -def recherche(request): - return render(request, 'avisstage/recherche.html') - # FAQ def faq(request): return render(request, 'avisstage/faq.html') @@ -140,7 +136,7 @@ def manage_stage(request, pk=None): {'form': form, 'avis_stage_form': avis_stage_form, 'avis_lieu_formset': avis_lieu_formset, 'creation': pk is None}) - + # Ajout d'un lieu de stage #login_required diff --git a/avisstage/views_search.py b/avisstage/views_search.py new file mode 100644 index 0000000..8f97a28 --- /dev/null +++ b/avisstage/views_search.py @@ -0,0 +1,34 @@ +# coding: utf-8 + +from django.shortcuts import render, redirect, get_object_or_404 +from django.contrib.auth.decorators import login_required + +from django import forms + +from avisstage.documents import StageDocument +from avisstage.models import Stage + +# Recherche +class SearchForm(forms.Form): + generique = forms.CharField() + +def cherche(**kwargs): + resultat = [] + if "generique" in kwargs: + resultat = StageDocument.search().filter( + "match", + _all={"query": kwargs["generique"], + "fuzziness": "auto"}) + return Stage.objects.filter(id__in=[s._id for s in resultat], public=True) + +@login_required +def recherche(request): + stages = [] + if request.method == "GET": + form = SearchForm(request.GET) + if form.is_valid(): + stages = cherche(**form.cleaned_data) + else: + form = SearchForm() + return render(request, 'avisstage/liste/recherche_resultats.html', + {"form": form, "stages":stages})