Recherche générique

This commit is contained in:
Evarin 2017-06-16 00:17:25 +02:00
parent 402a46ab74
commit fb91dc3111
4 changed files with 84 additions and 8 deletions

View file

@ -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()

View file

@ -0,0 +1,38 @@
{% extends "avisstage/base.html" %}
{% load staticfiles %}
{% block title %}Dernières mises à jour - ExperiENS{% endblock %}
{% block content %}
<h1>Résultats de la recherche</h1>
<form class="recherche" method="GET" action="">
<p>{{ form.generique }} <input type="submit" action="submit" value="Rechercher"/></p>
</form>
<article>
<ul class="stage-liste">
{% for stage in stages %}
{% ifchanged stage.date_maj.date %}<li class="date-maj">Mis à jour le {{ stage.date_maj.date }}</li>{% endifchanged %}
<li class="stage">
<div class="misc-hdr">
<h3><a href="{% url "avisstage:stage" stage.id %}">{{ stage.sujet }}</a><span class="auteur"> par <a href="{% url "avisstage:profil" stage.auteur.user.username %}">{{ stage.auteur.nom }}</a></span></h3>
<p class="dates"><span class="detail"><span class="debut">{{ stage.date_debut|date:"d/m" }}</span><span class="fin">{{ stage.date_fin|date:"d/m" }}</span></span><span class="year">{{ stage.date_debut|date:"Y" }}</span></p>
</div>
<div>
<ul class="infos">
<li class="type">{{ stage.get_type_stage_display }}</li>
<li class="structure">{{ stage.structure }}</li>
{% for lieu in stage.lieux.all %}<li class="lieu">{{ lieu.nom }}</li>{% endfor %}
{% for matiere in stage.matieres.all %}
<li class="matiere">{{ matiere.nom }}</li>
{% endfor %}
{% for thematique in stage.thematiques.all %}
<li class="thematique">{{ thematique.name }}</li>
{% endfor %}
</ul>
</div>
</li>
{% endfor %}
</ul>
</article>
{% endblock %}

View file

@ -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

34
avisstage/views_search.py Normal file
View file

@ -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})