Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions traffic_control/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,25 @@ def clean(self):
cleaned_data = super().clean()
z_coord = cleaned_data.pop("z_coord", 0)
location = cleaned_data.get("location", None)
new_geom = self._get_new_geom(location, cleaned_data.get("location_ewkt", None), z_coord)
location_ewkt = cleaned_data.get("location_ewkt", None)
new_geom = self._get_new_geom(location, location_ewkt, z_coord)
has_location_error = True if "location" in self.errors else False
if new_geom:
if "location" in self.errors:
if has_location_error:
del self.errors["location"]
cleaned_data["location"] = new_geom
cleaned_data["location_ewkt"] = new_geom.ewkt
else:
if not location_ewkt and self.instance:
# this is update with clearing location_ewkt field
cleaned_data["location_ewkt"] = ""
cleaned_data["location"] = None
# sometimes mapwidget rounds coordinates differently
# in _get_new_geom check is done using 6 digits so when new_geom is None, location in new_geom need to be
# set to self.instance.location to prevent unnecessary auditlog entries on save.
cleaned_data["location"] = self.instance.location
elif not has_location_error:
cleaned_data["location"] = self.instance.location

return cleaned_data

def _get_new_geom(self, location, location_ewkt, z_coord):
Expand All @@ -219,6 +227,7 @@ def _get_new_geom(self, location, location_ewkt, z_coord):
location_changed = self._get_is_location_changed(location)
if not location_ewkt_changed and not location_changed:
return None

if not self.instance.location:
if location_ewkt:
return GEOSGeometry(location_ewkt)
Expand Down
2 changes: 2 additions & 0 deletions traffic_control/tests/test_additional_sign_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def test__additional_sign__update_device_with_content_to_missing_content(
"missing_content": True,
"content_s": "null",
"location": test_point.ewkt,
"location_ewkt": test_point.ewkt,
"owner": str(get_owner().pk),
"device_type": str(device_type.pk),
"z_coord": 0,
Expand Down Expand Up @@ -166,6 +167,7 @@ def test__additional_sign__update_device_with_missing_content_to_have_content(
"missing_content": False,
"content_s": json.dumps(content_valid_by_simple_schema),
"location": test_point.ewkt,
"location_ewkt": test_point.ewkt,
"owner": str(get_owner().pk),
"device_type": str(device_type.pk),
"z_coord": 0,
Expand Down
23 changes: 23 additions & 0 deletions traffic_control/tests/test_plan_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,29 @@ def test__plan_update_with_location_not_changed():
assert "location" not in update_entry.changes


@pytest.mark.django_db
def test__plan_update_to_empty_location():
location = MultiPolygon(test_polygon, srid=settings.SRID)
plan = PlanFactory(location=location)
logentries = LogEntry.objects.get_for_object(plan).filter(action=LogEntry.Action.CREATE)
assert logentries.count() == 1
assert Plan.objects.count() == 1

data = {
"location": location,
"location_ewkt": "",
"name": plan.name,
"decision_id": plan.decision_id,
"z_coord": get_z_for_polygon(test_polygon),
}
form = PlanModelForm(data=data, instance=plan)
assert form.is_valid() is True
form.save()

plan.refresh_from_db()
assert plan.location is None


@pytest.mark.django_db
def test__plan_create_no_location():
form = PlanModelForm(data=_get_plan_mandatory_fields())
Expand Down