From 784de8af7145fa56bd6ebe6eda972942978f3e2e Mon Sep 17 00:00:00 2001 From: champeno Date: Wed, 16 Sep 2015 18:59:39 +0200 Subject: [PATCH] Recherche --- monstage/templates/monstage/search.html | 9 ++++----- monstage/views.py | 16 ++++++++++------ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/monstage/templates/monstage/search.html b/monstage/templates/monstage/search.html index d84a50a..e772df0 100644 --- a/monstage/templates/monstage/search.html +++ b/monstage/templates/monstage/search.html @@ -17,13 +17,11 @@
@@ -52,7 +51,7 @@ {% if resultats %} -

{{ resultats.length }} stage(s) trouvé(s)

+

{{ resultats|length }} stage(s) trouvé(s)

{% for stage in resultats %}

{{ stage.sujet }}

diff --git a/monstage/views.py b/monstage/views.py index b6532f3..1e0de06 100644 --- a/monstage/views.py +++ b/monstage/views.py @@ -268,6 +268,7 @@ def detail(request, question_id): # # Recherche de stages # +from django.db.models import Q class SearchForm(forms.Form): matiere = forms.ChoiceField(label='Matière :', required=False, choices=tuple([('','Toute matière')] + [(mat.id, mat.name) for mat in StageMatiere.objects.all()])) @@ -286,20 +287,23 @@ def search(request): if form.is_valid(): lon = form.cleaned_data['longitude'] lat = form.cleaned_data['latitude'] + lieu = form.cleaned_data['lieu'] stages = Stage.objects - if lat and lon: + if lat and lon and lieu: coords = GEOSGeometry('POINT(%f %f)' % (lon, lat), srid=4326) distance = {'km': form.cleaned_data['tolerance']} lieux = Lieu.objects.filter(coord__distance_lte=(coords, measure.D(**distance))) lieux = lieux.distance(coords).order_by('distance') - stages = lieux.stage_set + stages = stages.filter(lieux__in=lieux) matiere = form.cleaned_data['matiere'] if matiere: stages = stages.filter(matieres__pk=matiere) - thematiques = form.cleaned_data['thematiques'] - if thematiques: - #todo - pass + thematiques = form.cleaned_data['thematiques'].split(',') + if thematiques: + q = Q() + for thematique in thematiques: + q |= Q(thematiques__name__icontains = thematique) + stages = stages.filter(q).distinct() stages = stages.all() else: form = SearchForm()