diff options
author | Cefin <kevon@tuta.io> | 2022-02-04 12:06:48 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:21:00 +0100 |
commit | 101f1a37c06ee2837f20444cf22072bff8384315 (patch) | |
tree | 0064d25f705ae341ceea0e855052d7a884097469 | |
parent | ae014c8afa2e6313fd571b52e6325de9aa801d17 (diff) | |
download | Ishtar-101f1a37c06ee2837f20444cf22072bff8384315.tar.bz2 Ishtar-101f1a37c06ee2837f20444cf22072bff8384315.zip |
Operation - Parcels - Modify: add a quick form for new parcel, code cleaning #5227
-rw-r--r-- | archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html | 1 | ||||
-rw-r--r-- | archaeological_operations/views.py | 42 |
2 files changed, 17 insertions, 26 deletions
diff --git a/archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html b/archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html index 4b937a2a6..cb526ee79 100644 --- a/archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html +++ b/archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html @@ -10,7 +10,6 @@ </div> <form enctype="multipart/form-data" action="{{url}}" method="post" id="qa-new-{{ formset.form_slug }}"> {{ formset.management_form }} - {% comment %} {{ formset_add.management_form }} {% endcomment %} {% csrf_token %} <div class="modal-body body-scroll"> <div class='form'> diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py index 8c2357bb9..15fe0fce8 100644 --- a/archaeological_operations/views.py +++ b/archaeological_operations/views.py @@ -443,23 +443,13 @@ def operation_modify_parcels(request, pk): 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) #, prefix="modified_parcel" - new_parcel_formset = formset_class(new_data) #, prefix="new_parcel + # new_parcel_formset = formset_class(new_data) #, prefix="new_parcel for form in formset: #DEBUG print(form.errors) #DEBUG - if formset.is_valid() and new_parcel_formset.is_valid(): + if formset.is_valid(): print(formset.cleaned_data) #DEBUG post_data = [form.cleaned_data for form in formset] - post_data_new = [form.cleaned_data for form in new_parcel_formset] - - for new_data in post_data_new: - parcel_to_add = models.Parcel.objects.create( - year = new_data.get("year"), - section = new_data.get("section"), - parcel_number = new_data.get("parcel_number"), - town = new_data.get("town") - ) - operation.parcels.add(parcel_to_add) for data in post_data: print(data) @@ -472,25 +462,26 @@ def operation_modify_parcels(request, pk): if key == "DELETE" and value == True: parcel_associated_by_context_record = models.ContextRecord.objects.filter(parcel=current_parcel.pk).all() if len(parcel_associated_by_context_record) > 0: - raise ValidationError("This parcel is associated with a context record. It can't be deleted.") + raise ValidationError("This parcel is associated with a context record. It can't be deleted.") else: - current_parcel.delete() + current_parcel.delete() # Need to delete parcel or just the relation ? + # operation.parcels.delete(current_parcel) # Is this work ? not_deleted_or_associated = False if not_deleted_or_associated: current_parcel.year = data.get("year") current_parcel.section = data.get("section") current_parcel.parcel_number = data.get("parcel_number") - current_parcel.town = data.get("town") + current_parcel.town = models.Town.objects.get(pk=int(data.get("town"))) current_parcel.save() - # else: - # parcel_to_add = models.Parcel.objects.create( - # year = post_data.get("year"), - # section = post_data.get("section"), - # parcel_number = post_data.get("parcel_number"), - # town = post_data.get("town") - # ) - # operation.parcels.add(parcel_to_add) + else: + parcel_to_add = models.Parcel.objects.create( + year = data.get("year"), + section = data.get("section"), + parcel_number = data.get("parcel_number"), + town = models.Town.objects.get(pk=int(data.get("town"))) + ) + operation.parcels.add(parcel_to_add) else: print("je passe ici") formset = formset_class() @@ -502,10 +493,11 @@ def operation_modify_parcels(request, pk): 'form-MIN_NUM_FORMS': 0, 'form-MAX_NUM_FORMS': 100, }) + data_new = data.copy() + data_new['form-TOTAL_FORMS'] = 1 - # formset = formset_class(initial=initial, data=data) formset = formset_class(initial=initial, data=data) #, prefix="modified_parcel" - formset_add = formset_class(data=data) #, prefix="new_parcel" + formset_add = formset_class(data=data_new) #, prefix="new_parcel" for form in formset:# DEBUG print(form.as_table())# DEBUG |