summaryrefslogtreecommitdiff
path: root/archaeological_operations
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2022-09-08 17:49:05 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:23:18 +0100
commitf115f144dc01a41c1373485416488b77ecaa62fc (patch)
tree95fc53fcdc8959da12250e0a4f49b35e0af902d3 /archaeological_operations
parent392a3508b2a5bfe266844b5be4ab58a01caff766 (diff)
downloadIshtar-f115f144dc01a41c1373485416488b77ecaa62fc.tar.bz2
Ishtar-f115f144dc01a41c1373485416488b77ecaa62fc.zip
Geo display: clean old code - manage bad data
Diffstat (limited to 'archaeological_operations')
-rw-r--r--archaeological_operations/models.py135
-rw-r--r--archaeological_operations/tests.py163
2 files changed, 66 insertions, 232 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index 69f8d4fdc..69b24fecd 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -30,6 +30,7 @@ from django.contrib.gis.db import models
from django.contrib.gis.db.models.aggregates import Union
from django.contrib.gis.db.models.functions import Centroid
from django.contrib.postgres.indexes import GinIndex
+from django.contrib.sites.models import Site
from django.db import IntegrityError, transaction
from django.db.models import Q, Count, Sum, Max, Avg
from django.db.models.signals import post_save, m2m_changed, post_delete
@@ -1729,92 +1730,88 @@ class Operation(
q = self.towns.filter(limit__isnull=False).annotate(poly=Union("limit")).all()
if not q.count():
return None
- return q.all()[0].poly, self._meta.verbose_namea
-
- """
- def _get_child_geodata(self, query, cr_list, collection_context_records_id,
- current_geodata):
- childs = []
- for cr in query.exclude(id__in=cr_list).values(
- "main_geodata", "id").distinct().all():
- childs.append(cr["id"])
- geodata_id = cr["main_geodata"]
- if geodata_id not in collection_context_records_id \
- and geodata_id not in current_geodata:
- collection_context_records_id.append(geodata_id)
- cr_list += childs
- return childs
- """
-
- def get_geo_items(self, rounded=True):
- dct = super(Operation, self).get_geo_items(rounded=rounded)
- ContextRecord = apps.get_model(
- "archaeological_context_records", "ContextRecord"
- )
-
- q = self.context_record.filter(main_geodata__isnull=False)
-
- """
- included = q.filter(
- right_relations__relation_type__txt_idx='is_included')
- q_upper = q.exclude(id__in=set(included.values_list("id", flat=True)))
- cr_list = []
-
- childs = self._get_child_geodata(
- q_upper, cr_list, collection_context_records_id,
- current_geodata)
- while childs:
- new_childs = []
- for child_id in childs:
- q_lower = q.filter(
- right_relations__relation_type__txt_idx='is_included',
- right_relations__right_record_id=child_id
- )
- new_childs += self._get_child_geodata(
- q_lower, cr_list, collection_context_records_id,
- current_geodata)
- childs = new_childs
-
+ return q.all()[0].poly, self._meta.verbose_name
-
- """
- collection_context_records_id = []
- current_geodata = self.geodata.values_list("id", flat=True)
+ def _get_geo_item_list(self, q, current_geodata, url, precision, rounded):
+ collection_id = []
+ items_id = []
q = q.values("main_geodata", "id")
- for cr in q.distinct().all():
- geodata_id = cr["main_geodata"]
- if geodata_id not in collection_context_records_id \
- and geodata_id not in current_geodata:
- collection_context_records_id.append(geodata_id)
-
- collection_context_records = []
- for cr_id in collection_context_records_id:
- geo = json.loads(GeoVectorData.objects.get(pk=cr_id).geojson)
+ for item in q.distinct().all():
+ geodata_id = item["main_geodata"]
+ if geodata_id not in current_geodata:
+ collection_id.append(geodata_id)
+ items_id.append(item["id"])
+ current_geodata.append(geodata_id)
+
+ collection = []
+ for idx in range(len(collection_id)):
+ geo = json.loads(GeoVectorData.objects.get(pk=collection_id[idx]).geojson)
geo_type = geo.get("type", None)
+ url_geo = url.format(items_id[idx])
if geo_type == "FeatureCollection":
- collection_context_records += geo["features"]
+ for feat in geo["features"]:
+ if "properties" in feat:
+ feat["properties"]["url"] = url_geo
+ collection += geo["features"]
elif geo_type:
- collection_context_records.append(geo)
+ if "properties" in geo:
+ geo["properties"]["url"] = url_geo
+ collection.append(geo)
- profile = get_current_profile()
- precision = profile.point_precision
if not precision and rounded:
- precision = 5
- if precision is not None:
- r = re.compile(r"(\d+)\.(\d{6})(\d*)")
- for feat in collection_context_records:
- geom_type = feat["geometry"].get("type", None)
- if geom_type == "Point":
+ precision = 6
+ r = re.compile(r"(\d+)\.(\d{6})(\d*)")
+ new_collection = []
+ for feat in collection:
+ geom_type = feat["geometry"].get("type", None)
+ if geom_type == "Point":
+ if precision is not None:
feat["geometry"]["coordinates"] = [
round(feat["geometry"]["coordinates"][0], precision),
round(feat["geometry"]["coordinates"][1], precision),
]
+ if not (-90 <= feat["geometry"]["coordinates"][1] <= 90) or not (
+ -180 <= feat["geometry"]["coordinates"][0] <= 180):
+ # probably a bad projection
+ continue
+ new_collection.append(feat)
+ return new_collection
+
+
+ def get_geo_items(self, rounded=True):
+ dct = super(Operation, self).get_geo_items(rounded=rounded)
+ site = Site.objects.get_current()
+ scheme = "https" if settings.ISHTAR_SECURE else "http"
+ base_url = scheme + "://" + site.domain
+
+ profile = get_current_profile()
+ precision = profile.point_precision
+
+ current_geodata = list(self.geodata.values_list("id", flat=True))
+
+ q = self.context_record.filter(main_geodata__isnull=False)
+ url = base_url + "/show-contextrecord/{}/"
+ collection_context_records = self._get_geo_item_list(
+ q, current_geodata, url, precision, rounded
+ )
+
+ BaseFind = apps.get_model("archaeological_finds", "BaseFind")
+ q = BaseFind.objects.filter(context_record__operation_id=self.pk,
+ main_geodata__isnull=False)
+ url = base_url + "/show-basefind/{}/"
+ collection_finds = self._get_geo_item_list(
+ q, current_geodata, url, precision, rounded
+ )
dct["context-records"] = {
"type": "FeatureCollection",
"features": collection_context_records,
}
+ dct["finds"] = {
+ "type": "FeatureCollection",
+ "features": collection_finds,
+ }
return dct
def context_record_relations_q(self):
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py
index 2521651ae..bfe8e3b11 100644
--- a/archaeological_operations/tests.py
+++ b/archaeological_operations/tests.py
@@ -109,13 +109,9 @@ from ishtar_common.tests import (
GenericSerializationTest,
WAREHOUSE_FIXTURES,
SearchText,
- SeleniumTests,
)
from ishtar_common.serializers import restore_serialized
-if settings.SELENIUM_TEST:
- from selenium.webdriver.support.wait import WebDriverWait
-
class FileInit(object):
def login_as_superuser(self):
@@ -4483,165 +4479,6 @@ class DocumentQATest(OperationInitTest, TestCase):
)
-@tag("ui")
-class SeleniumTestsOperations(SeleniumTests):
- fixtures = OPERATION_FIXTURES
-
- def _test_operation(self, xpath, slug_pk, geojsons):
- self.wait_and_click(xpath)
- label = self.label_from_internal_id(slug_pk)
-
- get_poly_id = "get-poly-for-" + slug_pk
- get_poly_label_id = "get-poly-label-for-" + slug_pk
- disp_cr_id = "disp-cr-for-" + slug_pk
- disp_bf_id = "disp-bf-for-" + slug_pk
-
- ope_polys = geojsons[label]["get_polys"]
- cr_labels_polys = ope_polys["properties"].pop("context-records")
- bf_labels_polys = ope_polys["properties"].pop("base-finds")
- cr_polys = {"type": "FeatureCollection", "features": []}
- for cr_label in cr_labels_polys:
- geojsons[cr_label]["get_polys"]["properties"] = None
- cr_polys["features"].append(geojsons[cr_label]["get_polys"])
- bf_polys = {
- "type": "FeatureCollection",
- "features": [
- geojsons[bf_label]["get_polys"] for bf_label in bf_labels_polys
- ],
- }
-
- ope_pts = geojsons[label]["get_pts"]
- cr_labels_pts = ope_pts["properties"].pop("context-records")
- cr_pts = {"type": "FeatureCollection", "features": []}
- for cr_label in cr_labels_pts:
- geojsons[cr_label]["get_pts"]["properties"] = None
- cr_pts["features"].append(geojsons[cr_label]["get_pts"])
- bf_labels_pts = ope_pts["properties"].pop("base-finds")
- bf_pts = {
- "type": "FeatureCollection",
- "features": [geojsons[bf_label]["get_pts"] for bf_label in bf_labels_pts],
- }
-
- ope_base = geojsons[label]["get_polys"]
-
- WebDriverWait(self.selenium, self.waiting_time).until(
- lambda driver: driver.find_element_by_xpath(
- '//dl[@class="col-12"]/dt[@id="display-geo-items-for-' + slug_pk + '"]'
- )
- )
- dd = '//dl[@class="col-12"]/dd'
- self.selenium.find_element_by_xpath(
- dd + '/fieldset/label[@for="' + disp_cr_id + '"]'
- )
- disp_cr = self.selenium.find_element_by_xpath(
- dd + '/fieldset/input[@id="' + disp_cr_id + '"]'
- )
- self.selenium.find_element_by_xpath(
- dd + '/fieldset/label[@for="' + disp_bf_id + '"]'
- )
- disp_bf = self.selenium.find_element_by_xpath(
- dd + '/fieldset/input[@id="' + disp_bf_id + '"]'
- )
- self.assertFalse(disp_cr.is_selected())
- self.assertFalse(disp_bf.is_selected())
- select_get_poly = dd + '/select[@id="' + get_poly_id + '"]'
- get_poly_label = dd + '/label[@id="' + get_poly_label_id + '"]'
- self.assertNotInDOM(select_get_poly)
- self.assertNotInDOM(get_poly_label)
- self.assertMap(ope_base, slug_pk)
-
- # click on "Context records"
- self.selenium.execute_script("initialize_test_map(arguments[0])", slug_pk)
- self.scroll(disp_cr)
- disp_cr.click()
-
- cr_geoms = [{"type": "FeatureCollection", "features": []}, cr_polys]
- self.assertTrue(disp_cr.is_selected())
- self.assertFalse(disp_bf.is_selected())
- self.selenium.find_element_by_xpath(get_poly_label)
- self.assertMap(ope_base, slug_pk, cr_geoms)
-
- get_poly = self.selenium.find_element_by_xpath(select_get_poly)
- get_poly.click()
- self.selenium.find_element_by_xpath(
- select_get_poly + '/option[@value="points"]'
- ).click()
- cr_geoms = [{"type": "FeatureCollection", "features": []}, cr_pts]
- self.assertTrue(disp_cr.is_selected())
- self.assertFalse(disp_bf.is_selected())
- self.selenium.find_element_by_xpath(get_poly_label)
- self.assertMap(ope_base, slug_pk, cr_geoms)
-
- # Click on everything
- self.selenium.execute_script("initialize_test_map(arguments[0])", slug_pk)
- disp_bf.click()
- every_geoms = [bf_pts, cr_pts]
- self.assertTrue(disp_cr.is_selected())
- self.assertTrue(disp_bf.is_selected())
- self.selenium.find_element_by_xpath(get_poly_label)
- self.assertMap(ope_base, slug_pk, every_geoms)
-
- self.selenium.execute_script("initialize_test_map(arguments[0])", slug_pk)
- get_poly.click()
- self.selenium.find_element_by_xpath(
- select_get_poly + '/option[@value="polygons"]'
- ).click()
- every_geoms = [bf_polys, cr_polys]
- self.assertTrue(disp_cr.is_selected())
- self.assertTrue(disp_bf.is_selected())
- self.selenium.find_element_by_xpath(get_poly_label)
- self.assertMap(ope_base, slug_pk, every_geoms)
-
- # click on "Base finds"
- self.selenium.execute_script("initialize_test_map(arguments[0])", slug_pk)
- self.scroll(disp_cr)
- disp_cr.click()
-
- bf_geoms = [bf_polys, {"type": "FeatureCollection", "features": []}]
- self.assertFalse(disp_cr.is_selected())
- self.assertTrue(disp_bf.is_selected())
- self.selenium.find_element_by_xpath(get_poly_label)
- self.assertMap(ope_base, slug_pk, bf_geoms)
-
- select_get_poly = dd + '/select[@id="' + get_poly_id + '"]'
- get_poly = self.selenium.find_element_by_xpath(select_get_poly)
- get_poly.click()
- self.selenium.find_element_by_xpath(
- select_get_poly + '/option[@value="points"]'
- ).click()
- bf_geoms = [bf_pts, {"type": "FeatureCollection", "features": []}]
- self.assertFalse(disp_cr.is_selected())
- self.assertTrue(disp_bf.is_selected())
- self.selenium.find_element_by_xpath(get_poly_label)
- self.assertMap(ope_base, slug_pk, bf_geoms)
-
- # Click on "None"
- self.selenium.execute_script("initialize_test_map(arguments[0])", slug_pk)
- disp_bf.click()
- self.assertFalse(disp_cr.is_selected())
- self.assertFalse(disp_bf.is_selected())
- self.assertNotInDOM(select_get_poly)
- self.assertNotInDOM(get_poly_label)
- self.assertMap(ope_base, slug_pk)
-
- def test_geo_items(self):
- if not settings.SELENIUM_TEST:
- return
- geojsons = self.default_geojson()
- slug = "operation"
- self.access_from_dropdown(slug)
- from_table = self.pks_and_details_from_table(slug)
- for pk, xpath in from_table:
- self.access_from_dropdown(slug)
- slug_pk = slug + "-" + str(pk)
- self._test_operation(xpath, slug_pk, copy.deepcopy(geojsons))
- # display both at the same time
- self.access_from_dropdown(slug)
- for pk, xpath in from_table:
- slug_pk = slug + "-" + str(pk)
- self._test_operation(xpath, slug_pk, copy.deepcopy(geojsons))
-
-
class ApiTest(OperationInitTest, APITestCase):
fixtures = FILE_FIXTURES