diff --git a/monstage/listepays.py b/monstage/listepays.py new file mode 100644 index 0000000..60e6583 --- /dev/null +++ b/monstage/listepays.py @@ -0,0 +1,245 @@ +# coding: utf-8 + +PAYS_CHOICES = ( + ("AF", u"Afghanistan"), + ("AL", u"Albanie"), + ("AQ", u"Antarctique"), + ("DZ", u"Algérie"), + ("AS", u"Samoa Américaines"), + ("AD", u"Andorre"), + ("AO", u"Angola"), + ("AG", u"Antigua-et-Barbuda"), + ("AZ", u"Azerbaïdjan"), + ("AR", u"Argentine"), + ("AU", u"Australie"), + ("AT", u"Autriche"), + ("BS", u"Bahamas"), + ("BH", u"Bahreïn"), + ("BD", u"Bangladesh"), + ("AM", u"Arménie"), + ("BB", u"Barbade"), + ("BE", u"Belgique"), + ("BM", u"Bermudes"), + ("BT", u"Bhoutan"), + ("BO", u"Bolivie"), + ("BA", u"Bosnie-Herzégovine"), + ("BW", u"Botswana"), + ("BV", u"Île Bouvet"), + ("BR", u"Brésil"), + ("BZ", u"Belize"), + ("IO", u"Territoire Britannique de l'Océan Indien"), + ("SB", u"Îles Salomon"), + ("VG", u"Îles Vierges Britanniques"), + ("BN", u"Brunéi Darussalam"), + ("BG", u"Bulgarie"), + ("MM", u"Myanmar"), + ("BI", u"Burundi"), + ("BY", u"Bélarus"), + ("KH", u"Cambodge"), + ("CM", u"Cameroun"), + ("CA", u"Canada"), + ("CV", u"Cap-vert"), + ("KY", u"Îles Caïmanes"), + ("CF", u"République Centrafricaine"), + ("LK", u"Sri Lanka"), + ("TD", u"Tchad"), + ("CL", u"Chili"), + ("CN", u"Chine"), + ("TW", u"Taïwan"), + ("CX", u"Île Christmas"), + ("CC", u"Îles Cocos (Keeling)"), + ("CO", u"Colombie"), + ("KM", u"Comores"), + ("YT", u"Mayotte"), + ("CG", u"République du Congo"), + ("CD", u"République Démocratique du Congo"), + ("CK", u"Îles Cook"), + ("CR", u"Costa Rica"), + ("HR", u"Croatie"), + ("CU", u"Cuba"), + ("CY", u"Chypre"), + ("CZ", u"République Tchèque"), + ("BJ", u"Bénin"), + ("DK", u"Danemark"), + ("DM", u"Dominique"), + ("DO", u"République Dominicaine"), + ("EC", u"Équateur"), + ("SV", u"El Salvador"), + ("GQ", u"Guinée Équatoriale"), + ("ET", u"Éthiopie"), + ("ER", u"Érythrée"), + ("EE", u"Estonie"), + ("FO", u"Îles Féroé"), + ("FK", u"Îles (malvinas) Falkland"), + ("GS", u"Géorgie du Sud et les Îles Sandwich du Sud"), + ("FJ", u"Fidji"), + ("FI", u"Finlande"), + ("AX", u"Îles Åland"), + ("FR", u"France"), + ("GF", u"Guyane Française"), + ("PF", u"Polynésie Française"), + ("TF", u"Terres Australes Françaises"), + ("DJ", u"Djibouti"), + ("GA", u"Gabon"), + ("GE", u"Géorgie"), + ("GM", u"Gambie"), + ("PS", u"Territoire Palestinien Occupé"), + ("DE", u"Allemagne"), + ("GH", u"Ghana"), + ("GI", u"Gibraltar"), + ("KI", u"Kiribati"), + ("GR", u"Grèce"), + ("GL", u"Groenland"), + ("GD", u"Grenade"), + ("GP", u"Guadeloupe"), + ("GU", u"Guam"), + ("GT", u"Guatemala"), + ("GN", u"Guinée"), + ("GY", u"Guyana"), + ("HT", u"Haïti"), + ("HM", u"Îles Heard et Mcdonald"), + ("VA", u"Saint-Siège (état de la Cité du Vatican)"), + ("HN", u"Honduras"), + ("HK", u"Hong-Kong"), + ("HU", u"Hongrie"), + ("IS", u"Islande"), + ("IN", u"Inde"), + ("ID", u"Indonésie"), + ("IR", u"République Islamique d'Iran"), + ("IQ", u"Iraq"), + ("IE", u"Irlande"), + ("IL", u"Israël"), + ("IT", u"Italie"), + ("CI", u"Côte d'Ivoire"), + ("JM", u"Jamaïque"), + ("JP", u"Japon"), + ("KZ", u"Kazakhstan"), + ("JO", u"Jordanie"), + ("KE", u"Kenya"), + ("KP", u"République Populaire Démocratique de Corée"), + ("KR", u"République de Corée"), + ("KW", u"Koweït"), + ("KG", u"Kirghizistan"), + ("LA", u"République Démocratique Populaire Lao"), + ("LB", u"Liban"), + ("LS", u"Lesotho"), + ("LV", u"Lettonie"), + ("LR", u"Libéria"), + ("LY", u"Jamahiriya Arabe Libyenne"), + ("LI", u"Liechtenstein"), + ("LT", u"Lituanie"), + ("LU", u"Luxembourg"), + ("MO", u"Macao"), + ("MG", u"Madagascar"), + ("MW", u"Malawi"), + ("MY", u"Malaisie"), + ("MV", u"Maldives"), + ("ML", u"Mali"), + ("MT", u"Malte"), + ("MQ", u"Martinique"), + ("MR", u"Mauritanie"), + ("MU", u"Maurice"), + ("MX", u"Mexique"), + ("MC", u"Monaco"), + ("MN", u"Mongolie"), + ("MD", u"République de Moldova"), + ("MS", u"Montserrat"), + ("MA", u"Maroc"), + ("MZ", u"Mozambique"), + ("OM", u"Oman"), + ("NA", u"Namibie"), + ("NR", u"Nauru"), + ("NP", u"Népal"), + ("NL", u"Pays-Bas"), + ("AN", u"Antilles Néerlandaises"), + ("AW", u"Aruba"), + ("NC", u"Nouvelle-Calédonie"), + ("VU", u"Vanuatu"), + ("NZ", u"Nouvelle-Zélande"), + ("NI", u"Nicaragua"), + ("NE", u"Niger"), + ("NG", u"Nigéria"), + ("NU", u"Niué"), + ("NF", u"Île Norfolk"), + ("NO", u"Norvège"), + ("MP", u"Îles Mariannes du Nord"), + ("UM", u"Îles Mineures Éloignées des États-Unis"), + ("FM", u"États Fédérés de Micronésie"), + ("MH", u"Îles Marshall"), + ("PW", u"Palaos"), + ("PK", u"Pakistan"), + ("PA", u"Panama"), + ("PG", u"Papouasie-Nouvelle-Guinée"), + ("PY", u"Paraguay"), + ("PE", u"Pérou"), + ("PH", u"Philippines"), + ("PN", u"Pitcairn"), + ("PL", u"Pologne"), + ("PT", u"Portugal"), + ("GW", u"Guinée-Bissau"), + ("TL", u"Timor-Leste"), + ("PR", u"Porto Rico"), + ("QA", u"Qatar"), + ("RE", u"Réunion"), + ("RO", u"Roumanie"), + ("RU", u"Fédération de Russie"), + ("RW", u"Rwanda"), + ("SH", u"Sainte-Hélène"), + ("KN", u"Saint-Kitts-et-Nevis"), + ("AI", u"Anguilla"), + ("LC", u"Sainte-Lucie"), + ("PM", u"Saint-Pierre-et-Miquelon"), + ("VC", u"Saint-Vincent-et-les Grenadines"), + ("SM", u"Saint-Marin"), + ("ST", u"Sao Tomé-et-Principe"), + ("SA", u"Arabie Saoudite"), + ("SN", u"Sénégal"), + ("SC", u"Seychelles"), + ("SL", u"Sierra Leone"), + ("SG", u"Singapour"), + ("SK", u"Slovaquie"), + ("VN", u"Viet Nam"), + ("SI", u"Slovénie"), + ("SO", u"Somalie"), + ("ZA", u"Afrique du Sud"), + ("ZW", u"Zimbabwe"), + ("ES", u"Espagne"), + ("EH", u"Sahara Occidental"), + ("SD", u"Soudan"), + ("SR", u"Suriname"), + ("SJ", u"Svalbard etÎle Jan Mayen"), + ("SZ", u"Swaziland"), + ("SE", u"Suède"), + ("CH", u"Suisse"), + ("SY", u"République Arabe Syrienne"), + ("TJ", u"Tadjikistan"), + ("TH", u"Thaïlande"), + ("TG", u"Togo"), + ("TK", u"Tokelau"), + ("TO", u"Tonga"), + ("TT", u"Trinité-et-Tobago"), + ("AE", u"Émirats Arabes Unis"), + ("TN", u"Tunisie"), + ("TR", u"Turquie"), + ("TM", u"Turkménistan"), + ("TC", u"Îles Turks et Caïques"), + ("TV", u"Tuvalu"), + ("UG", u"Ouganda"), + ("UA", u"Ukraine"), + ("MK", u"L'ex-République Yougoslave de Macédoine"), + ("EG", u"Égypte"), + ("GB", u"Royaume-Uni"), + ("IM", u"Île de Man"), + ("TZ", u"République-Unie de Tanzanie"), + ("US", u"États-Unis"), + ("VI", u"Îles Vierges des États-Unis"), + ("BF", u"Burkina Faso"), + ("UY", u"Uruguay"), + ("UZ", u"Ouzbékistan"), + ("VE", u"Venezuela"), + ("WF", u"Wallis et Futuna"), + ("WS", u"Samoa"), + ("YE", u"Yémen"), + ("CS", u"Serbie-et-Monténégro"), + ("ZM", u"Zambie"), +) \ No newline at end of file diff --git a/monstage/migrations/0004_auto_20150614_1517.py b/monstage/migrations/0004_auto_20150614_1517.py new file mode 100644 index 0000000..7b71118 --- /dev/null +++ b/monstage/migrations/0004_auto_20150614_1517.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('monstage', '0003_auto_20150613_1930'), + ] + + operations = [ + migrations.AlterField( + model_name='lieu', + name='pays', + field=models.CharField(max_length=5, verbose_name='Pays', choices=[(b'AF', 'Afghanistan'), (b'AL', 'Albanie'), (b'AQ', 'Antarctique'), (b'DZ', 'Alg\xe9rie'), (b'AS', 'Samoa Am\xe9ricaines'), (b'AD', 'Andorre'), (b'AO', 'Angola'), (b'AG', 'Antigua-et-Barbuda'), (b'AZ', 'Azerba\xefdjan'), (b'AR', 'Argentine'), (b'AU', 'Australie'), (b'AT', 'Autriche'), (b'BS', 'Bahamas'), (b'BH', 'Bahre\xefn'), (b'BD', 'Bangladesh'), (b'AM', 'Arm\xe9nie'), (b'BB', 'Barbade'), (b'BE', 'Belgique'), (b'BM', 'Bermudes'), (b'BT', 'Bhoutan'), (b'BO', 'Bolivie'), (b'BA', 'Bosnie-Herz\xe9govine'), (b'BW', 'Botswana'), (b'BV', '\xcele Bouvet'), (b'BR', 'Br\xe9sil'), (b'BZ', 'Belize'), (b'IO', "Territoire Britannique de l'Oc\xe9an Indien"), (b'SB', '\xceles Salomon'), (b'VG', '\xceles Vierges Britanniques'), (b'BN', 'Brun\xe9i Darussalam'), (b'BG', 'Bulgarie'), (b'MM', 'Myanmar'), (b'BI', 'Burundi'), (b'BY', 'B\xe9larus'), (b'KH', 'Cambodge'), (b'CM', 'Cameroun'), (b'CA', 'Canada'), (b'CV', 'Cap-vert'), (b'KY', '\xceles Ca\xefmanes'), (b'CF', 'R\xe9publique Centrafricaine'), (b'LK', 'Sri Lanka'), (b'TD', 'Tchad'), (b'CL', 'Chili'), (b'CN', 'Chine'), (b'TW', 'Ta\xefwan'), (b'CX', '\xcele Christmas'), (b'CC', '\xceles Cocos (Keeling)'), (b'CO', 'Colombie'), (b'KM', 'Comores'), (b'YT', 'Mayotte'), (b'CG', 'R\xe9publique du Congo'), (b'CD', 'R\xe9publique D\xe9mocratique du Congo'), (b'CK', '\xceles Cook'), (b'CR', 'Costa Rica'), (b'HR', 'Croatie'), (b'CU', 'Cuba'), (b'CY', 'Chypre'), (b'CZ', 'R\xe9publique Tch\xe8que'), (b'BJ', 'B\xe9nin'), (b'DK', 'Danemark'), (b'DM', 'Dominique'), (b'DO', 'R\xe9publique Dominicaine'), (b'EC', '\xc9quateur'), (b'SV', 'El Salvador'), (b'GQ', 'Guin\xe9e \xc9quatoriale'), (b'ET', '\xc9thiopie'), (b'ER', '\xc9rythr\xe9e'), (b'EE', 'Estonie'), (b'FO', '\xceles F\xe9ro\xe9'), (b'FK', '\xceles (malvinas) Falkland'), (b'GS', 'G\xe9orgie du Sud et les \xceles Sandwich du Sud'), (b'FJ', 'Fidji'), (b'FI', 'Finlande'), (b'AX', '\xceles \xc5land'), (b'FR', 'France'), (b'GF', 'Guyane Fran\xe7aise'), (b'PF', 'Polyn\xe9sie Fran\xe7aise'), (b'TF', 'Terres Australes Fran\xe7aises'), (b'DJ', 'Djibouti'), (b'GA', 'Gabon'), (b'GE', 'G\xe9orgie'), (b'GM', 'Gambie'), (b'PS', 'Territoire Palestinien Occup\xe9'), (b'DE', 'Allemagne'), (b'GH', 'Ghana'), (b'GI', 'Gibraltar'), (b'KI', 'Kiribati'), (b'GR', 'Gr\xe8ce'), (b'GL', 'Groenland'), (b'GD', 'Grenade'), (b'GP', 'Guadeloupe'), (b'GU', 'Guam'), (b'GT', 'Guatemala'), (b'GN', 'Guin\xe9e'), (b'GY', 'Guyana'), (b'HT', 'Ha\xefti'), (b'HM', '\xceles Heard et Mcdonald'), (b'VA', 'Saint-Si\xe8ge (\xe9tat de la Cit\xe9 du Vatican)'), (b'HN', 'Honduras'), (b'HK', 'Hong-Kong'), (b'HU', 'Hongrie'), (b'IS', 'Islande'), (b'IN', 'Inde'), (b'ID', 'Indon\xe9sie'), (b'IR', "R\xe9publique Islamique d'Iran"), (b'IQ', 'Iraq'), (b'IE', 'Irlande'), (b'IL', 'Isra\xebl'), (b'IT', 'Italie'), (b'CI', "C\xf4te d'Ivoire"), (b'JM', 'Jama\xefque'), (b'JP', 'Japon'), (b'KZ', 'Kazakhstan'), (b'JO', 'Jordanie'), (b'KE', 'Kenya'), (b'KP', 'R\xe9publique Populaire D\xe9mocratique de Cor\xe9e'), (b'KR', 'R\xe9publique de Cor\xe9e'), (b'KW', 'Kowe\xeft'), (b'KG', 'Kirghizistan'), (b'LA', 'R\xe9publique D\xe9mocratique Populaire Lao'), (b'LB', 'Liban'), (b'LS', 'Lesotho'), (b'LV', 'Lettonie'), (b'LR', 'Lib\xe9ria'), (b'LY', 'Jamahiriya Arabe Libyenne'), (b'LI', 'Liechtenstein'), (b'LT', 'Lituanie'), (b'LU', 'Luxembourg'), (b'MO', 'Macao'), (b'MG', 'Madagascar'), (b'MW', 'Malawi'), (b'MY', 'Malaisie'), (b'MV', 'Maldives'), (b'ML', 'Mali'), (b'MT', 'Malte'), (b'MQ', 'Martinique'), (b'MR', 'Mauritanie'), (b'MU', 'Maurice'), (b'MX', 'Mexique'), (b'MC', 'Monaco'), (b'MN', 'Mongolie'), (b'MD', 'R\xe9publique de Moldova'), (b'MS', 'Montserrat'), (b'MA', 'Maroc'), (b'MZ', 'Mozambique'), (b'OM', 'Oman'), (b'NA', 'Namibie'), (b'NR', 'Nauru'), (b'NP', 'N\xe9pal'), (b'NL', 'Pays-Bas'), (b'AN', 'Antilles N\xe9erlandaises'), (b'AW', 'Aruba'), (b'NC', 'Nouvelle-Cal\xe9donie'), (b'VU', 'Vanuatu'), (b'NZ', 'Nouvelle-Z\xe9lande'), (b'NI', 'Nicaragua'), (b'NE', 'Niger'), (b'NG', 'Nig\xe9ria'), (b'NU', 'Niu\xe9'), (b'NF', '\xcele Norfolk'), (b'NO', 'Norv\xe8ge'), (b'MP', '\xceles Mariannes du Nord'), (b'UM', '\xceles Mineures \xc9loign\xe9es des \xc9tats-Unis'), (b'FM', '\xc9tats F\xe9d\xe9r\xe9s de Micron\xe9sie'), (b'MH', '\xceles Marshall'), (b'PW', 'Palaos'), (b'PK', 'Pakistan'), (b'PA', 'Panama'), (b'PG', 'Papouasie-Nouvelle-Guin\xe9e'), (b'PY', 'Paraguay'), (b'PE', 'P\xe9rou'), (b'PH', 'Philippines'), (b'PN', 'Pitcairn'), (b'PL', 'Pologne'), (b'PT', 'Portugal'), (b'GW', 'Guin\xe9e-Bissau'), (b'TL', 'Timor-Leste'), (b'PR', 'Porto Rico'), (b'QA', 'Qatar'), (b'RE', 'R\xe9union'), (b'RO', 'Roumanie'), (b'RU', 'F\xe9d\xe9ration de Russie'), (b'RW', 'Rwanda'), (b'SH', 'Sainte-H\xe9l\xe8ne'), (b'KN', 'Saint-Kitts-et-Nevis'), (b'AI', 'Anguilla'), (b'LC', 'Sainte-Lucie'), (b'PM', 'Saint-Pierre-et-Miquelon'), (b'VC', 'Saint-Vincent-et-les Grenadines'), (b'SM', 'Saint-Marin'), (b'ST', 'Sao Tom\xe9-et-Principe'), (b'SA', 'Arabie Saoudite'), (b'SN', 'S\xe9n\xe9gal'), (b'SC', 'Seychelles'), (b'SL', 'Sierra Leone'), (b'SG', 'Singapour'), (b'SK', 'Slovaquie'), (b'VN', 'Viet Nam'), (b'SI', 'Slov\xe9nie'), (b'SO', 'Somalie'), (b'ZA', 'Afrique du Sud'), (b'ZW', 'Zimbabwe'), (b'ES', 'Espagne'), (b'EH', 'Sahara Occidental'), (b'SD', 'Soudan'), (b'SR', 'Suriname'), (b'SJ', 'Svalbard et\xcele Jan Mayen'), (b'SZ', 'Swaziland'), (b'SE', 'Su\xe8de'), (b'CH', 'Suisse'), (b'SY', 'R\xe9publique Arabe Syrienne'), (b'TJ', 'Tadjikistan'), (b'TH', 'Tha\xeflande'), (b'TG', 'Togo'), (b'TK', 'Tokelau'), (b'TO', 'Tonga'), (b'TT', 'Trinit\xe9-et-Tobago'), (b'AE', '\xc9mirats Arabes Unis'), (b'TN', 'Tunisie'), (b'TR', 'Turquie'), (b'TM', 'Turkm\xe9nistan'), (b'TC', '\xceles Turks et Ca\xefques'), (b'TV', 'Tuvalu'), (b'UG', 'Ouganda'), (b'UA', 'Ukraine'), (b'MK', "L'ex-R\xe9publique Yougoslave de Mac\xe9doine"), (b'EG', '\xc9gypte'), (b'GB', 'Royaume-Uni'), (b'IM', '\xcele de Man'), (b'TZ', 'R\xe9publique-Unie de Tanzanie'), (b'US', '\xc9tats-Unis'), (b'VI', '\xceles Vierges des \xc9tats-Unis'), (b'BF', 'Burkina Faso'), (b'UY', 'Uruguay'), (b'UZ', 'Ouzb\xe9kistan'), (b'VE', 'Venezuela'), (b'WF', 'Wallis et Futuna'), (b'WS', 'Samoa'), (b'YE', 'Y\xe9men'), (b'CS', 'Serbie-et-Mont\xe9n\xe9gro'), (b'ZM', 'Zambie')]), + preserve_default=True, + ), + ] diff --git a/monstage/models.py b/monstage/models.py index a48d7e2..b5648b1 100644 --- a/monstage/models.py +++ b/monstage/models.py @@ -6,6 +6,8 @@ from django.utils.translation import ugettext_lazy as _ from django.contrib.gis.db import models as geomodels from django_cas_ng.signals import cas_user_authenticated +from monstage.listepays import PAYS_CHOICES + TYPE_STAGE_CHOICES = ( ('stage', _(u"Stage")), ) @@ -41,7 +43,9 @@ cas_user_authenticated.connect(create_user_profile) class Lieu(models.Model): name = models.CharField(_(u"Nom de l'institution d'accueil"), max_length = 250) ville = models.CharField(_(u"Ville"), max_length = 200) - pays = models.CharField(_(u"Pays"), max_length = 200) + pays = models.CharField(_(u"Pays"), + choices = PAYS_CHOICES, + max_length = choices_length (TYPE_STAGE_CHOICES)) coord = geomodels.PointField(_(u"Coordonnées"), geography = True) objects = geomodels.GeoManager() # Requis par GeoDjango type_lieu = models.CharField( _(u"Type de structure d'accueil"), diff --git a/monstage/templates/monstage/stage_edit_lieu.html b/monstage/templates/monstage/stage_edit_lieu.html index d11eb14..109b945 100644 --- a/monstage/templates/monstage/stage_edit_lieu.html +++ b/monstage/templates/monstage/stage_edit_lieu.html @@ -47,7 +47,7 @@ function addFieldSet (data) { if (data == undefined) return; var t = $("
") - t.html(emptyform.replace({% verbatim %}/\{\{ID\}\}/gi{% endverbatim %}, fieldcount)); + t.html(emptyform.replace(/\{\{ID\}\}/gi, fieldcount)); places.append(t); document.getElementById('id_'+fieldcount+'-name').value = data.name; document.getElementById('id_'+fieldcount+'-ville').value = data.ville; @@ -65,15 +65,14 @@ function digere (gdata) { var data = {}; for (var i in gdata.address_components) { - var txt = gdata.address_components[i].long_name; - var types = gdata.address_components[i].types - for (var j in types) { - switch(types[j]) { + var obj = gdata.address_components[i]; + for (var j in obj.types) { + switch(obj.types[j]) { case "locality": - data["ville"] = txt; + data["ville"] = obj.long_name; break; case "country": - data["pays"] = txt; + data["pays"] = obj.short_name; break; } } diff --git a/monstage/views.py b/monstage/views.py index 511bdf1..091c2ea 100644 --- a/monstage/views.py +++ b/monstage/views.py @@ -77,26 +77,32 @@ def stage_edit_desc(request, stage_id): class LieuForm(forms.ModelForm): latitude = forms.DecimalField() longitude = forms.DecimalField() - delete = forms.BooleanField(help_text=(u'Supprimer ce lieu')) - delete.label = '' + 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] class Meta: model = Lieu fields = ("name", "ville", "pays") - def save(self, commit=True): - lieu = super(StageLieuForm, self).save(commit=False) + def is_to_delete(self): + return self.cleaned_data['delete'] + + 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) - if commit: - return lieu.save(commit=True) - else: - return lieu + return lieu.save(*args, **kwargs) def stage_edit_lieu(request, stage_id): stage = get_object_or_404( Stage, pk = stage_id) @@ -105,22 +111,41 @@ def stage_edit_lieu(request, stage_id): return HttpResponseForbidden("Ce stage ne vous appartient pas") if request.POST: i = 0 - j = str(i) - bullshit = ", ".join([k for k in request.POST]) - while request.POST.get('name'+j, False): - lieu = Lieu() - lieu.name = request.POST['name'+j] - lieu.ville = request.POST['ville'+j] - lieu.pays = request.POST['pays'+j] - - lieu.save() - LieuStage.objects.create(lieu = lieu, stage = stage) + 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 - j = str(i) - return HttpResponseRedirect(reverse('monstage:stage_edit_feedback', args=(new_stage.id,))) + if not lieuform.is_valid(): + valid = False + forms.append(lieuform) + continue + if lieuform.is_to_delete(): + 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,))) else: forms = [LieuForm(instance=lieu, prefix=str(counter)) for counter, lieu in enumerate(stage.lieux.all())] - emptyform = LieuForm(prefix='{{ID}}') + 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):