Recherche

This commit is contained in:
champeno 2015-09-16 18:59:39 +02:00
parent e183abe947
commit 784de8af71
2 changed files with 14 additions and 11 deletions

View file

@ -17,13 +17,11 @@
<div id="map_searchlieu"></div> <div id="map_searchlieu"></div>
<script> <script>
$(function(){ $(function(){
$("#addlieu").geocomplete({ $("#id_lieu").geocomplete({
map: "#map_searchlieu", map: "#map_searchlieu",
types: ["geocode", "establishment"], types: ["geocode", "establishment"],
}).on("geocode:result", function(event, result){ $("#map_searchlieu").css("display", "block"); digere(result); console.log(result); }); }).on("geocode:result", function(event, result){ $("#map_searchlieu").css("display", "block"); digere(result); console.log(result); });
}); });
var fieldcount = {{ numforms }};
var places = $("#places");
var predata; var predata;
function digere (gdata) { function digere (gdata) {
@ -44,7 +42,8 @@
data["name"] = gdata.name; data["name"] = gdata.name;
data["longitude"] = gdata.geometry.location.lng(); data["longitude"] = gdata.geometry.location.lng();
data["latitude"] = gdata.geometry.location.lat(); data["latitude"] = gdata.geometry.location.lat();
// TODO set to hidden inputs $("#id_longitude").val(data["longitude"]);
$("#id_latitude").val(data["latitude"]);
} }
</script> </script>
@ -52,7 +51,7 @@
</form> </form>
</div> </div>
{% if resultats %} {% if resultats %}
<p>{{ resultats.length }} stage(s) trouvé(s)</p> <p>{{ resultats|length }} stage(s) trouvé(s)</p>
{% for stage in resultats %} {% for stage in resultats %}
<div class="stagefound"> <div class="stagefound">
<h3><a href="{% url 'monstage:stage' stage.id %}">{{ stage.sujet }}</a></h3> <h3><a href="{% url 'monstage:stage' stage.id %}">{{ stage.sujet }}</a></h3>

View file

@ -268,6 +268,7 @@ def detail(request, question_id):
# #
# Recherche de stages # Recherche de stages
# #
from django.db.models import Q
class SearchForm(forms.Form): 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()])) 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(): if form.is_valid():
lon = form.cleaned_data['longitude'] lon = form.cleaned_data['longitude']
lat = form.cleaned_data['latitude'] lat = form.cleaned_data['latitude']
lieu = form.cleaned_data['lieu']
stages = Stage.objects stages = Stage.objects
if lat and lon: if lat and lon and lieu:
coords = GEOSGeometry('POINT(%f %f)' % (lon, lat), srid=4326) coords = GEOSGeometry('POINT(%f %f)' % (lon, lat), srid=4326)
distance = {'km': form.cleaned_data['tolerance']} distance = {'km': form.cleaned_data['tolerance']}
lieux = Lieu.objects.filter(coord__distance_lte=(coords, measure.D(**distance))) lieux = Lieu.objects.filter(coord__distance_lte=(coords, measure.D(**distance)))
lieux = lieux.distance(coords).order_by('distance') lieux = lieux.distance(coords).order_by('distance')
stages = lieux.stage_set stages = stages.filter(lieux__in=lieux)
matiere = form.cleaned_data['matiere'] matiere = form.cleaned_data['matiere']
if matiere: if matiere:
stages = stages.filter(matieres__pk=matiere) stages = stages.filter(matieres__pk=matiere)
thematiques = form.cleaned_data['thematiques'] thematiques = form.cleaned_data['thematiques'].split(',')
if thematiques: if thematiques:
#todo q = Q()
pass for thematique in thematiques:
q |= Q(thematiques__name__icontains = thematique)
stages = stages.filter(q).distinct()
stages = stages.all() stages = stages.all()
else: else:
form = SearchForm() form = SearchForm()