Amélioration du formulaire des lieux de stage

(non fonctionnel)
This commit is contained in:
champeno 2015-06-14 12:57:53 +02:00
parent eaf2537048
commit bd953fdbdf
2 changed files with 51 additions and 20 deletions

View file

@ -23,37 +23,37 @@
<form action="{% url 'monstage:stage_edit_lieu' stage.id %}" method="post">
{% csrf_token %}
<div id="places">
{% for form in forms %}
<div class="forms">
{{ form.as_p }}
</div>
{% endfor %}
</div>
<input type="submit" value="Etape suivante" />
</form>
<script>
var emptyform = "{{ emptyform |safe }}";
$(function(){
$("#geocomplete").geocomplete({
map: "#map_canvas",
types: ["geocode", "establishment"],
}).on("geocode:result", function(event, result){ digere(result); console.log(predata); });
}).on("geocode:result", function(event, result){ digere(result); console.log(result); });
});
$(function(){
var alreadyknown = [
{% for lieu in stage.lieux.all %}
{% if not forloop.first %},{% endif %}
{ name: "{{ lieu.name }}", ville: "{{ lieu.ville }}", pays: "{{ lieu.pays }}", latitude: "{{ lieu.coord.coords.0 }}", longitude: "{{ lieu.coord.coords.1 }}" }
{% endfor %} ];
for (var i in alreadyknown) {
addFieldSet(alreadyknown[i]);
}
});
var fieldcount = 0;
var fieldcount = {{ numforms }};
var places = $("#places");
function addFieldSet (data) {
if (data == undefined) return;
var t = $("<div>")
t.append($("<input>", {type:"text", id:"name"+fieldcount, name:"name"+fieldcount, placeholder:"Nom de l'institution", value:data.name || ""}));
t.append($("<input>", {type:"text", id:"ville"+fieldcount, name:"ville"+fieldcount, placeholder:"Ville", value:data.ville || ""}));
t.append($("<input>", {type:"text", id:"pays"+fieldcount, name:"pays"+fieldcount, placeholder:"Pays", value:data.pays || ""}));
t.append($("<input>", {type:"hidden", id:"latitude"+fieldcount, name:"latitude"+fieldcount, value:data.latitude || ""}));
t.append($("<input>", {type:"hidden", id:"longitude"+fieldcount, name:"longitude"+fieldcount, value:data.longitude || ""}));
t.html(emptyform.replace({% verbatim %}/\{\{ID\}\}/gi{% endverbatim %}, fieldcount));
places.append(t);
document.getElementById('id_'+fieldcount+'-name').value = data.name;
document.getElementById('id_'+fieldcount+'-ville').value = data.ville;
document.getElementById('id_'+fieldcount+'-pays').value = data.pays;
document.getElementById('id_'+fieldcount+'-latitude').value = data.latitude;
document.getElementById('id_'+fieldcount+'-longitude').value = data.longitude;
// {innerHTML:'<input id="name" type="text" placeholder="Nom de l\'institution" />\n <input id="ville" type="text" placeholder="Ville" />
// <input id="pays" type="text" placeholder="Pays" />
// <input id="latitude" type="hidden" />

View file

@ -7,6 +7,10 @@ from django.contrib.gis.geos import GEOSGeometry
from monstage.models import *
def addslashes(s):
d = {'"':'\\"', "'":"\\'", "\0":"\\\0", "\\":"\\\\", '\n':'\\n'}
return ''.join(d.get(c, c) for c in s)
@login_required
def index(request):
stages = Stage.objects.filter( profil_user = request.user.profil )
@ -70,11 +74,35 @@ def stage_edit_desc(request, stage_id):
pass
return render(request, 'monstage/stage_edit_desc.html', { 'stage': stage, 'debug':bullshit })
class LieuForm(forms.ModelForm):
latitude = forms.DecimalField()
longitude = forms.DecimalField()
delete = forms.BooleanField(help_text=(u'Supprimer ce lieu'))
delete.label = ''
def __init__(self, *args, **kwargs):
from django.forms.widgets import HiddenInput
super(LieuForm, self).__init__(*args, **kwargs)
self.fields['latitude'].widget = HiddenInput()
self.fields['longitude'].widget = HiddenInput()
class Meta:
model = Lieu
fields = ("name", "ville", "pays")
def save(self, commit=True):
lieu = super(StageLieuForm, self).save(commit=False)
lieu.coord = GEOSGeometry('POINT(%f %f)' % (self.cleaned_data['latitude'], self.cleaned_data['latitude']), srid=4326)
if commit:
return lieu.save(commit=True)
else:
return lieu
def stage_edit_lieu(request, stage_id):
stage = get_object_or_404( Stage, pk = stage_id)
bullshit = ''
if stage.profil_user != request.user.profil:
return HttpResponseForbidden("Ce stage ne vous appartient pas")
bullshit = ""
if request.POST:
i = 0
j = str(i)
@ -84,13 +112,16 @@ def stage_edit_lieu(request, stage_id):
lieu.name = request.POST['name'+j]
lieu.ville = request.POST['ville'+j]
lieu.pays = request.POST['pays'+j]
lieu.coord = GEOSGeometry('POINT(%f %f)' % (float(request.POST['latitude'+j]), float(request.POST['longitude'+j])), srid=4326)
lieu.save()
LieuStage.objects.create(lieu = lieu, stage = stage)
i = i+1
j = str(i)
return HttpResponseRedirect(reverse('monstage:stage_edit_feedback', args=(new_stage.id,)))
return render(request, 'monstage/stage_edit_lieu.html', { 'stage': stage, 'debug':bullshit })
else:
forms = [LieuForm(instance=lieu, prefix=str(counter)) for counter, lieu in enumerate(stage.lieux.all())]
emptyform = LieuForm(prefix='{{ID}}')
return render(request, 'monstage/stage_edit_lieu.html', { 'stage': stage, 'debug': bullshit, 'forms': forms, 'emptyform': addslashes(emptyform.as_p()), 'numforms':len(forms) })
class StageFeedbackForm(forms.ModelForm):
class Meta: