summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2022-02-01 11:50:04 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:21:00 +0100
commit280eabb5e82195bb79ef08ae71a7bb1a2efaf06e (patch)
tree575a59c1722b59521371540301f4b61094466409
parentbf0c1161182e905b44a4cb1f17afc9b1f08414c3 (diff)
downloadIshtar-280eabb5e82195bb79ef08ae71a7bb1a2efaf06e.tar.bz2
Ishtar-280eabb5e82195bb79ef08ae71a7bb1a2efaf06e.zip
Operation - parcels: fix submit
-rw-r--r--archaeological_operations/views.py62
1 files changed, 32 insertions, 30 deletions
diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py
index faf73826e..b6099d6b1 100644
--- a/archaeological_operations/views.py
+++ b/archaeological_operations/views.py
@@ -413,23 +413,36 @@ def operation_modify_parcels(request, pk):
formset_class = SelectedParcelGeneralFormSet
operation = models.Operation.objects.get(pk=pk)
parcels = models.Parcel.objects.filter(operation=pk).all()
- data = {}
available_towns = []
for town in operation.towns.all():
available_towns.append((town.pk, str(town)))
-
- if request.method == 'POST':
-
- data = {
- 'TOWNS': available_towns,
- 'form-TOTAL_FORMS': 1,
- 'form-INITIAL_FORMS': 0,
- }
- request_to_update = request.POST.copy()
-
- formset = formset_class(request_to_update.update({'TOWNS': available_towns}), request.FILES) #DON'T WORK THAT WAY
+ initial = []
+ for parcel in parcels:
+ town = models.Town.objects.get(pk=parcel.town_id)
+ initial.append({
+ "pk": parcel.pk,
+ "town": town.pk,
+ "year": parcel.year,
+ "section": parcel.section,
+ "parcel_number": parcel.parcel_number,
+ "public_domain": parcel.public_domain
+ })
+
+ data = {
+ 'TOWNS': available_towns,
+ 'form-TOTAL_FORMS': len(initial),
+ 'form-INITIAL_FORMS': 0,
+ 'form-MIN_NUM_FORMS': 0,
+ 'form-MAX_NUM_FORMS': 100,
+ }
+
+ if request.method == 'POST': #TODO
+ new_data = dict(request.POST)
+ new_data = {k: new_data[k][0] for k in new_data} # convert POST to classic dict
+ new_data.update(data)
+ formset = formset_class(new_data)
for form in formset: #DEBUG
print(form.errors) #DEBUG
@@ -437,7 +450,8 @@ def operation_modify_parcels(request, pk):
print(formset.cleaned_data) #DEBUG
post_data = [form.cleaned_data for form in formset]
for data in post_data:
- current_parcel = models.Parcel.objects.get(pk=post_data.get("pk"))
+ print(data)
+ current_parcel = models.Parcel.objects.get(pk=post_data["pk"])
if current_parcel:
not_deleted = True
@@ -467,24 +481,12 @@ def operation_modify_parcels(request, pk):
print("je passe ici")
formset = formset_class()
else:
- initial = []
- if parcels:
- for parcel in parcels:
- town = models.Town.objects.get(pk=int(parcel.town_id))
- initial.append({
- "pk": parcel.pk,
- "town": town,
- "year": parcel.year,
- "section": parcel.section,
- "parcel_number": parcel.parcel_number,
- "public_domain": parcel.public_domain
- })
-
- data = {
- 'TOWNS': available_towns,
+ data.update({
'form-TOTAL_FORMS': len(initial),
'form-INITIAL_FORMS': 0,
- }
+ 'form-MIN_NUM_FORMS': 0,
+ 'form-MAX_NUM_FORMS': 100,
+ })
formset = formset_class(initial=initial, data=data)
@@ -493,7 +495,7 @@ def operation_modify_parcels(request, pk):
return render(request, 'ishtar/forms/operation_modify_parcels.html', {
'formset': formset,
- 'url': reverse("operation-parcels-modify", args=[pk])
+ "url": reverse("operation-parcels-modify", args=[pk])
})