Recherche de stage basique
This commit is contained in:
parent
bdf7702be4
commit
75c341c943
4 changed files with 107 additions and 19 deletions
|
@ -34,7 +34,7 @@ class Normalien(models.Model):
|
|||
verbose_name_plural = u"Profils élèves"
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.user.username)
|
||||
return unicode(self.user)
|
||||
|
||||
|
||||
def create_user_profile(sender, user, created, **kwargs):
|
||||
|
|
|
@ -1,9 +1,63 @@
|
|||
{% extends "skeleton.html" %}
|
||||
{% load staticfiles %}
|
||||
|
||||
{% block extra_head %}
|
||||
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places" type="text/javascript"></script>
|
||||
<script src="{% static 'script/jquery-1.11.1.min.js' %}" type="text/javascript"></script>
|
||||
<script src="{% static 'script/jquery.geocomplete.min.js' %}" type="text/javascript"></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<p><a href="{% url 'monstage:index' %}">Retour</a></p>
|
||||
<h1>Recherche</h1>
|
||||
<p>La recherche de stage n'est pas encore disponible. Elle devrait l'être à la rentrée 2015 !</p>
|
||||
{% endblock %}
|
||||
<h1>Rechercher des stages</h1>
|
||||
<div id="searchbox">
|
||||
<form action="{% url 'monstage:search' %}" method="get">
|
||||
<p>Critères :</p>
|
||||
{{ form.as_p }}
|
||||
<div id="map_searchlieu"></div>
|
||||
<script>
|
||||
$(function(){
|
||||
$("#addlieu").geocomplete({
|
||||
map: "#map_searchlieu",
|
||||
types: ["geocode", "establishment"],
|
||||
}).on("geocode:result", function(event, result){ $("#map_searchlieu").css("display", "block"); digere(result); console.log(result); });
|
||||
});
|
||||
var fieldcount = {{ numforms }};
|
||||
var places = $("#places");
|
||||
|
||||
var predata;
|
||||
function digere (gdata) {
|
||||
var data = {};
|
||||
for (var i in gdata.address_components) {
|
||||
var obj = gdata.address_components[i];
|
||||
for (var j in obj.types) {
|
||||
switch(obj.types[j]) {
|
||||
case "locality":
|
||||
data["ville"] = obj.long_name;
|
||||
break;
|
||||
case "country":
|
||||
data["pays"] = obj.short_name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
data["name"] = gdata.name;
|
||||
data["longitude"] = gdata.geometry.location.lng();
|
||||
data["latitude"] = gdata.geometry.location.lat();
|
||||
// TODO set to hidden inputs
|
||||
}
|
||||
|
||||
</script>
|
||||
<input type="submit" action="submit" />
|
||||
</form>
|
||||
</div>
|
||||
{% if resultats %}
|
||||
<p>{{ resultats.length }} stage(s) trouvé(s)</p>
|
||||
{% for stage in resultats %}
|
||||
<div class="stagefound">
|
||||
<h3><a href="{% url 'monstage:stage' stage.id %}">{{ stage.sujet }}</a></h3>
|
||||
<p>Par {{ stage.profil_user }} à {% for lieu in stage.lieux.all %}{% if not forloop.first %}{% if forloop.last %} et {% else %}, {% endif %}{% endif %}{{ lieu.name }} ({{ lieu.ville }}){% endfor %}.</p>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
{% block content %}
|
||||
<p><a href="{% url 'monstage:home' %}">Retour</a></p>
|
||||
<h1>{{ stage.sujet }}</h1>
|
||||
{% if modifiable %}
|
||||
<form action="{% url 'monstage:stage_publish' stage.id %}" method="post">
|
||||
{% csrf_token %}
|
||||
|
@ -39,7 +40,6 @@
|
|||
</div>
|
||||
</form>
|
||||
{% endif %}
|
||||
<h1>{{ stage.sujet }}</h1>
|
||||
<div id="stage_present">
|
||||
<div id="stage_map"></div>
|
||||
<div id="stage_misc">
|
||||
|
|
|
@ -12,7 +12,7 @@ from django.forms.widgets import HiddenInput
|
|||
from monstage.models import *
|
||||
|
||||
def addslashes(s):
|
||||
d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\", '\n':'\\n'}
|
||||
d = {'"':'\\"', "\0":"\\\0", "\\":"\\\\", '\n':'\\n'}
|
||||
return ''.join(d.get(c, c) for c in s)
|
||||
|
||||
def index(request):
|
||||
|
@ -129,7 +129,7 @@ class LieuForm(forms.ModelForm):
|
|||
|
||||
class Meta:
|
||||
model = Lieu
|
||||
fields = ("name", "ville", "pays")
|
||||
fields = ("name", "type_lieu", "ville", "pays")
|
||||
|
||||
def is_to_delete(self):
|
||||
return self.cleaned_data['delete']
|
||||
|
@ -164,7 +164,7 @@ def stage_edit_lieu(request, stage_id):
|
|||
prevLieuxStage = [k for k in stage.lieustage_set.all()]
|
||||
lieuforms = []
|
||||
deleted = []
|
||||
for i in range(0, int(request.POST['numplaces'])):
|
||||
for i in range(0, int(request.POST['numplaces'])): # Pour chaque lieu
|
||||
toSave = False
|
||||
if request.POST.get('%d-name' % i, False): # Lieu à créer
|
||||
bullshit += str(i) + 'a '
|
||||
|
@ -173,9 +173,9 @@ def stage_edit_lieu(request, stage_id):
|
|||
valid = False
|
||||
lieuforms.append(lieuform)
|
||||
continue
|
||||
if lieuform.is_to_delete():
|
||||
if lieuform.is_to_delete(): # On ne fait rien : le lieu est nouveau donc n'était pas encore en mémoire
|
||||
pass
|
||||
else:
|
||||
else: # On crée un nouveau lieu
|
||||
lieu = lieuform.save()
|
||||
lieu.save()
|
||||
lieuform.save_m2m()
|
||||
|
@ -189,26 +189,26 @@ def stage_edit_lieu(request, stage_id):
|
|||
valid = False
|
||||
lieuforms.append(lieuform)
|
||||
continue
|
||||
if lieuform.is_to_delete():
|
||||
if lieuform.is_to_delete(): # On transvase de prevLieuxStage vers deleted pour conserver l'assignation des commentaires
|
||||
if len(prevLieuxStage) > 0:
|
||||
deleted.append(prevLieuxStage.pop(0))
|
||||
else:
|
||||
else: # On récupère le lieu concerné
|
||||
lieuforms.append(lieuform)
|
||||
lieu = Lieu.objects.get( pk = lieuform.cleaned_data['lieu_id'] )
|
||||
toSave = True
|
||||
if toSave: # Mise à jour des liens lieu - stage
|
||||
if len(prevLieuxStage) > 0:
|
||||
if toSave: # Mise à jour des lieuStage
|
||||
if len(prevLieuxStage) > 0: # On met à jour un lieuStage conservé (même lieu)
|
||||
lieustage = prevLieuxStage.pop(0)
|
||||
lieustage.lieu = lieu
|
||||
lieustage.save()
|
||||
elif len(deleted) > 0:
|
||||
elif len(deleted) > 0: # On réutilise l'emplacement d'un lieuStage qui a été supprimé (pour éviter de perdre les commentaires)
|
||||
lieustage = deleted.pop(0)
|
||||
lieustage.lieu = lieu
|
||||
lieustage.save()
|
||||
else:
|
||||
else: # On en crée un nouveau
|
||||
LieuStage.objects.create(lieu = lieu, stage = stage)
|
||||
if valid:
|
||||
for lieustage in deleted:
|
||||
for lieustage in deleted: # On supprime effectivement les lieuStages non réutilisé
|
||||
lieustage.delete()
|
||||
return HttpResponseRedirect(reverse('monstage:stage_edit_feedback', args=(stage.id,)))
|
||||
else:
|
||||
|
@ -269,5 +269,39 @@ def detail(request, question_id):
|
|||
# Recherche de stages
|
||||
#
|
||||
|
||||
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()]))
|
||||
thematiques = forms.CharField(label='Thématiques :', required=False)
|
||||
|
||||
latitude = forms.DecimalField(widget=HiddenInput(), required=False)
|
||||
longitude = forms.DecimalField(widget=HiddenInput(), required=False)
|
||||
tolerance = forms.DecimalField(label='Dans un rayon de (en km) :', initial=100, required=False)
|
||||
lieu = forms.CharField(label=u'A proximité de :', required=False)
|
||||
|
||||
|
||||
def search(request):
|
||||
return render(request, 'monstage/search.html')
|
||||
stages = None
|
||||
if request.GET:
|
||||
form = SearchForm(request.GET)
|
||||
if form.is_valid():
|
||||
lon = form.cleaned_data['longitude']
|
||||
lat = form.cleaned_data['latitude']
|
||||
stages = Stage.objects
|
||||
if lat and lon:
|
||||
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
|
||||
matiere = form.cleaned_data['matiere']
|
||||
if matiere:
|
||||
stages = stages.filter(matieres__pk=matiere)
|
||||
thematiques = form.cleaned_data['thematiques']
|
||||
if thematiques:
|
||||
#todo
|
||||
pass
|
||||
stages = stages.all()
|
||||
else:
|
||||
form = SearchForm()
|
||||
return render(request, 'monstage/search.html', {'form':form, 'resultats':stages})
|
||||
|
||||
|
|
Loading…
Reference in a new issue