diff options
-rw-r--r-- | archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html | 24 | ||||
-rw-r--r-- | archaeological_operations/views.py | 54 |
2 files changed, 45 insertions, 33 deletions
diff --git a/archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html b/archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html index 434ba9490..4b937a2a6 100644 --- a/archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html +++ b/archaeological_operations/templates/ishtar/forms/operation_modify_parcels.html @@ -10,7 +10,7 @@ </div> <form enctype="multipart/form-data" action="{{url}}" method="post" id="qa-new-{{ formset.form_slug }}"> {{ formset.management_form }} - {{ formset_add.management_form }} + {% comment %} {{ formset_add.management_form }} {% endcomment %} {% csrf_token %} <div class="modal-body body-scroll"> <div class='form'> @@ -32,17 +32,17 @@ </td> </tr> </table> - {% comment %} <h3>Add a new parcel</h3> {% endcomment %} - {% comment %} {% if formset_add.non_form_errors %} {% endcomment %} - {% comment %} <div class="alert alert-danger" role="alert"> {% endcomment %} - {% comment %} {{ formset_add.non_form_errors }} {% endcomment %} - {% comment %} </div> {% endcomment %} - {% comment %} {% endif %} {% endcomment %} - {% comment %} <table> {% endcomment %} - {% comment %} {% for form in formset_add %} {% endcomment %} - {% comment %} {{form}} {% endcomment %} - {% comment %} {% endfor %} {% endcomment %} - {% comment %} </table> {% endcomment %} + <h3>Add a new parcel</h3> + {% if formset_add.non_form_errors %} + <div class="alert alert-danger" role="alert"> + {{ formset_add.non_form_errors }} + </div> + {% endif %} + <table> + {% for form in formset_add %} + {{form}} + {% endfor %} + </table> {% endblock %} </div> </div> diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py index f7da19483..8c2357bb9 100644 --- a/archaeological_operations/views.py +++ b/archaeological_operations/views.py @@ -442,16 +442,28 @@ def operation_modify_parcels(request, pk): 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) + formset = formset_class(new_data) #, prefix="modified_parcel" + new_parcel_formset = formset_class(new_data) #, prefix="new_parcel for form in formset: #DEBUG print(form.errors) #DEBUG - if formset.is_valid(): + if formset.is_valid() and new_parcel_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) - current_parcel = models.Parcel.objects.get(pk=post_data["pk"]) + current_parcel = models.Parcel.objects.get(pk=data["pk"]) if current_parcel: not_deleted_or_associated = True @@ -460,29 +472,29 @@ 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.") - not_deleted_or_associated = False + raise ValidationError("This parcel is associated with a context record. It can't be deleted.") else: current_parcel.delete() - not_deleted_or_associated = False + not_deleted_or_associated = False if not_deleted_or_associated: - current_parcel.year = post_data.get("year") - current_parcel.section = post_data.get("section") - current_parcel.parcel_number = post_data.get("parcel_number") - current_parcel.town = post_data.get("town") + 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.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 = 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: print("je passe ici") formset = formset_class() + formset_add = formset_class() else: data.update({ 'form-TOTAL_FORMS': len(initial), @@ -491,9 +503,9 @@ def operation_modify_parcels(request, pk): 'form-MAX_NUM_FORMS': 100, }) - 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 = 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" for form in formset:# DEBUG print(form.as_table())# DEBUG |