summaryrefslogtreecommitdiff
path: root/archaeological_files/views.py
diff options
context:
space:
mode:
authorCefin <kevon@tuta.io>2022-02-18 15:36:17 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:21:00 +0100
commit2c9c7956072021dc94424216df3c5c503412570c (patch)
treefebae59af1c4fdb999383d5ccaf366ee9afb171b /archaeological_files/views.py
parenteb43d8ff163d347c7ac29a44a1a467a9925aba22 (diff)
downloadIshtar-2c9c7956072021dc94424216df3c5c503412570c.tar.bz2
Ishtar-2c9c7956072021dc94424216df3c5c503412570c.zip
File - Parcels - Modify: adapting conditions and tests for selection_form #5227
Diffstat (limited to 'archaeological_files/views.py')
-rw-r--r--archaeological_files/views.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/archaeological_files/views.py b/archaeological_files/views.py
index 36bd6f79d..061085710 100644
--- a/archaeological_files/views.py
+++ b/archaeological_files/views.py
@@ -27,6 +27,7 @@ from django.views.generic.edit import UpdateView
from django.shortcuts import redirect, render
from django.urls import reverse
from ishtar_common.utils import ugettext_lazy as _
+from archaeological_operations.utils import parse_parcels
from ishtar_common.views import wizard_is_available
from ishtar_common.views_item import get_item, show_item, revert_item, check_permission
@@ -377,6 +378,46 @@ def file_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)
+ parcel_selection = new_data.get('_parcel_selection', None)
+
+ if parcel_selection:
+ selected_town = new_data.get("_town")
+ _parcels = parse_parcels(parcel_selection)
+ existant_parcel = None
+
+ for p in _parcels:
+ p_year = p["year"]
+ p_section = p["section"]
+ p_parcel_number = p["parcel_number"]
+ p_town = models.Town.objects.get(pk=int(selected_town))
+ q = Parcel.objects.filter(
+ section=p_section, parcel_number=p_parcel_number, town=p_town, associated_file=file
+ )
+ nb = q.count()
+ # parcels_who_cant_be_deleted = 0
+
+ if nb > 1: # if duplicates parcels, keep just one
+ while nb > 1:
+ for d in q:
+ d.delete()
+ nb -= 1
+ elif nb == 1:
+ # parcels_who_cant_be_deleted += 1
+ pass
+ else:
+ added_parcel = Parcel.objects.create(
+ section = p_section,
+ parcel_number = p_parcel_number,
+ town = p_town,
+ associated_file = file,
+ )
+ if p_year:
+ added_parcel.year = p_year
+ added_parcel.save()
+ # if parcels_who_cant_be_deleted > 0:
+ # raise ValidationError(str(parcels_who_cant_be_deleted) + " parcels could not be created because there already exists.")
+ return redirect(reverse("operation-parcels-modify", args=[pk]))
+
if formset.is_valid():
for data in formset.cleaned_data:
if (not data.get('parcel_number') or not data.get('section')) and \