From 2fb603c86016e93312f6596666dc94a4f3e1e9ca Mon Sep 17 00:00:00 2001 From: champeno Date: Sun, 14 Jun 2015 19:13:27 +0200 Subject: [PATCH] =?UTF-8?q?Gestion=20compl=C3=A8te=20des=20lieux=20de=20st?= =?UTF-8?q?ages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../templates/monstage/lieux_proches.json | 5 + .../templates/monstage/stage_edit_lieu.html | 52 ++++++-- monstage/urls.py | 1 + monstage/views.py | 117 ++++++++++++------ 4 files changed, 128 insertions(+), 47 deletions(-) create mode 100644 monstage/templates/monstage/lieux_proches.json diff --git a/monstage/templates/monstage/lieux_proches.json b/monstage/templates/monstage/lieux_proches.json new file mode 100644 index 0000000..6cbed51 --- /dev/null +++ b/monstage/templates/monstage/lieux_proches.json @@ -0,0 +1,5 @@ +{ + "lieux": [ {% for lieu in lieux %} {% if not forloop.first %},{% endif %} + { "name": "{{ lieu.name }}", "id": "{{ lieu.id }}", "lat": {{ lieu.lat }}, "lon": {{ lieu.lon }}, "distance": "{{ lieu.distance }}" } +{% endfor %} ] +} \ No newline at end of file diff --git a/monstage/templates/monstage/stage_edit_lieu.html b/monstage/templates/monstage/stage_edit_lieu.html index 109b945..743acb4 100644 --- a/monstage/templates/monstage/stage_edit_lieu.html +++ b/monstage/templates/monstage/stage_edit_lieu.html @@ -19,13 +19,17 @@

{{ debug }}

- +
+
{% csrf_token %} +
- {% for form in forms %} -
- {{ form.as_p }} + {% for lieuform in lieuforms %} +
+

{{ lieuform.0.name }}

+

{{ lieuform.0.ville }}, {{ lieuform.0.get_pays_display }}

+ {{ lieuform.1.as_p }}
{% endfor %} @@ -36,6 +40,7 @@ {% endblock %} diff --git a/monstage/urls.py b/monstage/urls.py index dd77010..84c7180 100644 --- a/monstage/urls.py +++ b/monstage/urls.py @@ -6,6 +6,7 @@ urlpatterns = patterns('', url(r'^$', views.index, name='index'), url(r'^profil/show/(?P\w+)/$', views.profil, name='profil'), url(r'^profil/edit/$', views.profil_edit, name='profil_edit'), + url(r'^api/lieux/candidats/$', views.lieux_candidats, name='lieux_candidats'), url(r'^stage/(?P\d+)/$', views.stage, name='stage'), url(r'^stage/new/$', views.stage_add, name='stage_add'), url(r'^stage/(?P\d+)/edit/$', views.stage_edit, name='stage_edit'), diff --git a/monstage/views.py b/monstage/views.py index 091c2ea..b68c673 100644 --- a/monstage/views.py +++ b/monstage/views.py @@ -1,9 +1,13 @@ +# coding: utf-8 + from django.shortcuts import get_object_or_404, render from django.contrib.auth.decorators import login_required -from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden +from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, HttpResponseNotFound from django.core.urlresolvers import reverse from django import forms from django.contrib.gis.geos import GEOSGeometry +from django.contrib.gis import measure +from django.forms.widgets import HiddenInput from monstage.models import * @@ -74,21 +78,24 @@ def stage_edit_desc(request, stage_id): pass return render(request, 'monstage/stage_edit_desc.html', { 'stage': stage, 'debug':bullshit }) +class LieuStageForm(forms.Form): + lieu_id = forms.IntegerField(widget = HiddenInput()) + delete = forms.BooleanField(help_text=(u'Supprimer ce lieu'), required = False, label = '') + + def is_to_delete(self): + return self.cleaned_data['delete'] + class LieuForm(forms.ModelForm): latitude = forms.DecimalField() longitude = forms.DecimalField() delete = forms.BooleanField(help_text=(u'Supprimer ce lieu'), required = False, label = '') - temp_id = forms.IntegerField(required = False) 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() - self.fields['temp_id'].widget = HiddenInput() lieu = kwargs.pop('instance', None) if lieu: - self.fields['temp_id'].initial = lieu.id self.fields['latitude'].initial = lieu.coord.coords[0] self.fields['longitude'].initial = lieu.coord.coords[1] @@ -101,52 +108,84 @@ class LieuForm(forms.ModelForm): def save(self, *args, **kwargs): lieu = super(LieuForm, self).save(commit=False, *args, **kwargs) - lieu.coord = GEOSGeometry('POINT(%f %f)' % (self.cleaned_data['latitude'], self.cleaned_data['latitude']), srid=4326) + lieu.coord = GEOSGeometry('POINT(%f %f)' % (self.cleaned_data['latitude'], self.cleaned_data['longitude']), srid=4326) return lieu.save(*args, **kwargs) +def lieux_candidats(request): + if request.GET: + lat = float(request.GET.get('lat', False)) + lon = float(request.GET.get('lon', False)) + if lat and lon: + coords = GEOSGeometry('POINT(%f %f)' % (lat, lon), srid=4326) + distance = {'km': 0.5} + lieux = Lieu.objects.filter(coord__distance_lte=(coords, measure.D(**distance))) + lieux = lieux.distance(coords).order_by('distance') + retour = [ {'name': addslashes(lieu.name), 'id': lieu.id, 'lat':str(lieu.coord.coords[0]), 'lon':str(lieu.coord.coords[1]), 'distance':lieu.distance } for lieu in lieux.distance(coords) ] + return render(request, 'monstage/lieux_proches.json', { 'lieux':retour }, content_type='text/plain; charset=utf-8') + return HttpResponseNotFound() + 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") if request.POST: - i = 0 valid = True - lieux = [] - forms = [] - for lieustage in stage.lieustage_set.all(): - bullshit += str(i) + 'a ' - lieuform = LieuForm(request.POST, instance = lieustage.lieu, prefix = str(i)) - i = i+1 - if not lieuform.is_valid(): - valid = False - forms.append(lieuform) - continue - if lieuform.is_to_delete(): + prevLieuxStage = [k for k in stage.lieustage_set.all()] + lieuforms = [] + deleted = [] + for i in range(0, int(request.POST['numplaces'])): + if request.POST.get('%d-name' % i, False): # Lieu a modifier / créer + bullshit += str(i) + 'a ' + lieuform = LieuForm(request.POST, prefix = str(i)) + if not lieuform.is_valid(): + valid = False + lieuforms.append(lieuform) + continue + if lieuform.is_to_delete(): + pass + else: + lieu = lieuform.save() + if len(prevLieuxStage) > 0: + lieustage = prevLieuxStage.pop(0) + lieustage.lieu = lieu + lieustage.save() + else: + LieuStage.objects.create(lieu = lieu, stage = stage) + lieuforms.append(LieuStageForm(initial = {'lieu_id': lieu.id}, prefix = str(i))) + elif request.POST.get('%d-lieu_id' % i, False): # Lien vers un lieu déjà existant + bullshit += str(i) + 'b ' + lieuform = LieuStageForm(request.POST, prefix = str(i)) + if not lieuform.is_valid(): + bullshit += 'invalid ' + valid = False + lieuforms.append(lieuform) + continue + if lieuform.is_to_delete(): + if len(prevLieuxStage) > 0: + deleted.append(prevLieuxStage.pop(0)) + else: + lieuforms.append(lieuform) + lieu = Lieu.objects.get( pk = lieuform.cleaned_data['lieu_id'] ) + if len(prevLieuxStage) > 0: + lieustage = prevLieuxStage.pop(0) + lieustage.lieu = lieu + lieustage.save() + elif len(deleted) > 0: + lieustage = deleted.pop(0) + lieustage.lieu = lieu + lieustage.save() + else: + LieuStage.objects.create(lieu = lieu, stage = stage) + if valid: + for lieustage in deleted: lieustage.delete() - else: - forms.append(lieuform) - lieux.append(lieuform.save()) - while request.POST.get('%d-name' % i, False) and i<20: - bullshit += str(i) + 'b ' - lieuform = LieuForm(request.POST, prefix = str(i)) - i = i+1 - if not lieuform.is_valid(): - bullshit += 'invalid ' - valid = False - forms.append(lieuform) - continue - if not lieuform.is_to_delete(): - forms.append(lieuform) - lieu = lieuform.save(commit = True) - lieux.append(lieu) - LieuStage.objects.create(lieu = lieu, stage = stage) -# if valid: -# return HttpResponseRedirect(reverse('monstage:stage_edit_feedback', args=(new_stage.id,))) + return HttpResponseRedirect(reverse('monstage:stage_edit_feedback', args=(stage.id,))) else: - forms = [LieuForm(instance=lieu, prefix=str(counter)) for counter, lieu in enumerate(stage.lieux.all())] + lieuforms = [(lieu, LieuStageForm(initial={'lieu_id': lieu.id}, 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) }) + emptylieu = LieuStageForm(prefix='{{ID}}') + return render(request, 'monstage/stage_edit_lieu.html', { 'stage': stage, 'debug': bullshit, 'lieuforms': lieuforms, 'emptyform': addslashes(emptyform.as_p()), 'emptylieu': addslashes(emptylieu.as_p()), 'numforms':len(lieuforms) }) class StageFeedbackForm(forms.ModelForm): class Meta: