diff options
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 |
commit | f115f144dc01a41c1373485416488b77ecaa62fc (patch) | |
tree | 95fc53fcdc8959da12250e0a4f49b35e0af902d3 /ishtar_common | |
parent | 392a3508b2a5bfe266844b5be4ab58a01caff766 (diff) | |
download | Ishtar-f115f144dc01a41c1373485416488b77ecaa62fc.tar.bz2 Ishtar-f115f144dc01a41c1373485416488b77ecaa62fc.zip |
Geo display: clean old code - manage bad data
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/static/js/ishtar-map.js | 173 | ||||
-rw-r--r-- | ishtar_common/tests.py | 181 | ||||
-rw-r--r-- | ishtar_common/views_item.py | 2 |
3 files changed, 13 insertions, 343 deletions
diff --git a/ishtar_common/static/js/ishtar-map.js b/ishtar_common/static/js/ishtar-map.js index ae591db08..8ffe399d6 100644 --- a/ishtar_common/static/js/ishtar-map.js +++ b/ishtar_common/static/js/ishtar-map.js @@ -529,6 +529,9 @@ var open_map_window = function(map_id){ var complete_list_label = "complete list..."; +var map_display_base_url_start = "<a class='display_details' href='#' onclick='load_window(\""; +var map_display_base_url_end = "\")'><i class='fa fa-info-circle' aria-hidden='true'></i></a>"; + var _display_items = function(map_id, features, offset_x, offset_y){ // console.log("display_items"); var feature = features[0]; @@ -549,7 +552,10 @@ var _display_items = function(map_id, features, offset_x, offset_y){ if (link_template[map_id]){ link = link_template[map_id].replace("<pk>", properties["id"]); } - var txt = "<li>" + link + " " + properties['name'] + "</li>"; + if ("url" in properties){ + link = map_display_base_url_start + properties["url"] + map_display_base_url_end; + } + var txt = "<li>" + link + " " + properties["name"] + "</li>"; window_content += txt; if (idx_feat < 5){ popup_content += txt; @@ -728,8 +734,7 @@ var display_points = function(map_id, points, first_init){ if (!cluster_source[map_id]){ enable_clustering(map_id); } else { - cluster_source[map_id].getSource().clear(); - cluster_source[map_id].getSource().refresh(); + reinit_clustering(map_id); } cluster_source[map_id].getSource().addFeatures(point_features[map_id]); if (first_init && points.features && points.features.length){ @@ -761,160 +766,6 @@ var display_lines_and_polys = function(map_id, lines_and_polys, first_init){ } }; -var display_geo_items = function(map_id, base_url, slug, pk, - disp_cr, disp_bf, get_poly) { - let url = base_url; - if (slug === "operation") { - url += "?operation_pk="; - } - else { - url += "?context_record_pk="; - } - url += pk; - - httpRequest = new XMLHttpRequest(); - if (!httpRequest) { return; } - httpRequest.onreadystatechange = function() { - if (httpRequest.readyState === XMLHttpRequest.DONE) { - if (httpRequest.status === 200) { - const geo_items = to_geo_items(JSON.parse(httpRequest.responseText), slug, disp_cr, disp_bf) - if (current_test) { - geo_items_features[map_id] = []; - } - let bfs = null; - let crs = null; - if (get_poly) { - bfs = display_associated_polys(map_id, geo_items['base-finds'], 'basefind'); - crs = display_associated_polys(map_id, geo_items['context-records'], 'contextrecord'); - } else { - bfs = display_associated_points(map_id, geo_items['base-finds'], 'basefind'); - crs = display_associated_points(map_id, geo_items['context-records'], 'contextrecord'); - } - if (current_test) { - geo_items_features[map_id].push(bfs) - geo_items_features[map_id].push(crs) - $("#http-geo-items-ready-"+slug+"-"+pk).show(); - } - } else { return; } - } - }; - httpRequest.open('GET', url, true); - httpRequest.send(); -}; - -var to_geo_items = function (obj, slug, disp_cr, disp_bf) { - const objects = {'context-records': {'features': [], 'type': 'FeatureCollection'}, - 'base-finds': {'features': [], 'type': 'FeatureCollection'}}; - if (slug === "operation") { - const crs = obj['properties']['context-records']; - for (const cr of crs['features']) { - if (disp_bf) { - for (const bf of cr['properties']['base-finds']['features']) { - objects['base-finds']['features'].push(bf) - } - } - delete cr['properties']; - if (disp_cr) { - objects['context-records']['features'].push(cr) - } - } - } - else { - for (const bf of obj['properties']['base-finds']['features']) { - objects['base-finds']['features'].push(bf) - } - } - return objects; -} - -var display_associated_polys = function (map_id, polys, slug) { - const _vector_features = geojson_format.readFeatures(polys); - const _vector_source = new ol.source.Vector(); - _vector_source.addFeatures(_vector_features); - let style = get_associated_base_find_style; - if (slug === 'contextrecord') { - style = get_associated_context_record_style; - } - const _vector_layer = new ol.layer.Vector({ - source: _vector_source, - style: style - }); - map[map_id].addLayer(_vector_layer); - return _vector_features -} - -var get_associated_base_find_style = function (feature) { - return new ol.style.Style({ - stroke: new ol.style.Stroke({ - color: 'rgba(0, 0, 0, 1)', - width: 2 - }), - fill: new ol.style.Fill({ - color: 'rgba(O, O, O, 0.2)' - }) - }); -}; - -var get_associated_context_record_style = function (feature) { - return new ol.style.Style({ - stroke: new ol.style.Stroke({ - color: 'rgba(255, 255, 255, 1)', - width: 2 - }), - fill: new ol.style.Fill({ - color: 'rgba(255, 255, 255, 0.2)' - }), - radius: 10 - }); -}; - -var display_associated_points = function (map_id, points, slug) { - const _point_features = geojson_format.readFeatures(points); - const _cluster_source = new ol.source.Cluster({ - distance: 40, - source: new ol.source.Vector() - }); - _cluster_source.getSource().addFeatures(_point_features); - // TODO: create own style - const style = cluster_get_style; - const _cluster_layer = new ol.layer.Vector({ - name: 'Cluster', - source: _cluster_source, - style: style - }); - map[map_id].addLayer(_cluster_layer); - return _point_features; -} - -var get_features_by_id = function (id) { - let base_features = null - if (vector_features[id] == null) { - base_features = geojson_format.writeFeaturesObject(point_features[id], {decimals: 5}); - } else { - base_features = geojson_format.writeFeaturesObject(vector_features[id], {decimals: 5}); - } - const geo_items_feats = []; - if (geo_items_features && id in geo_items_features) { - for (const features of geo_items_features[id]) { - geo_items_feats.push(geojson_format.writeFeaturesObject(features, {decimals: 5})); - } - } - return [base_features, geo_items_feats]; -} - -var base_features_type = function (id) { - if (vector_features[id] == null) { - if (point_features[id] == null) { - return 'both null'; - } - else { return 'Point'; } - } - if (point_features[id] == null) { - return 'MultiPolygon'; - } - else { return 'Point and MultiPolygon' } -} - var _geo_points = new Array(); var _geo_other = new Array(); @@ -948,10 +799,10 @@ const _refresh_map_finds = function(idx) { } } -const refresh_map_finds_crs = function(url, attrs, idx, crs_check, find_check) { +const refresh_map_finds_crs = function(url, attrs, idx, crs_check, finds_check) { if (idx in _point_list_finds){ if (crs_check) _refresh_map_crs(idx); - if (find_check) _refresh_map_finds(idx); + if (finds_check) _refresh_map_finds(idx); return; } _point_list_crs[idx] = new Array(); @@ -1011,9 +862,9 @@ const refresh_map = function(idx, geodata_list, url, attrs) { } } if (url && attrs){ - let find_check = $("#map-ol-" + idx + "-finds").prop('checked'); + let finds_check = $("#map-ol-" + idx + "-finds").prop('checked'); let crs_check = $("#map-ol-" + idx + "-crs").prop('checked'); - refresh_map_finds_crs(url, attrs, idx, crs_check, find_check); + refresh_map_finds_crs(url, attrs, idx, crs_check, finds_check); } } diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 25d4d8e85..a7cc8361c 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -90,12 +90,6 @@ from ishtar_common import utils_secretary from django.contrib.staticfiles.testing import StaticLiveServerTestCase -if settings.SELENIUM_TEST: - from selenium.webdriver.firefox.webdriver import WebDriver - from selenium.webdriver.support.wait import WebDriverWait - from selenium.webdriver.common.by import By - from selenium.webdriver.support import expected_conditions as EC - COMMON_FIXTURES = [ settings.ROOT_PATH + "../fixtures/initial_data-auth-fr.json", @@ -3369,178 +3363,3 @@ class TemplateGenerationTest(TestCase): filtr = doc.get_filter(template, filter_re) for key in expected_keys: self.assertIn(key, filtr) - - -@tag("ui") -class SeleniumTests(StaticLiveServerTestCase): - @classmethod - def setUpClass(cls): - if not settings.SELENIUM_TEST: - sys.stdout.write("UI test not activated. Set SELENIUM_TEST to True.") - return - super().setUpClass() - cls.waiting_time = 20 - cls.selenium = WebDriver() - cls.selenium.implicitly_wait(cls.waiting_time) - - @classmethod - def tearDownClass(cls): - if not settings.SELENIUM_TEST: - return - cls.selenium.quit() - super().tearDownClass() - - def setUp(self): - if not settings.SELENIUM_TEST: - return - - # profile - profile = models.get_current_profile() - profile.mapping = True - profile.files = True - profile.context_record = True - profile.find = True - profile.warehouse = True - profile.experimental_feature = True - profile.save() - - # login - self.username, self.password, self.user = create_superuser() - self.selenium.get("%s%s" % (self.live_server_url, "/accounts/login/")) - WebDriverWait(self.selenium, self.waiting_time).until( - lambda driver: driver.find_element_by_name("username") - ) - username_input = self.selenium.find_element_by_name("username") - username_input.send_keys(self.username) - password_input = self.selenium.find_element_by_name("password") - password_input.send_keys(self.password) - self.selenium.find_element_by_xpath( - '//button[@type="submit"][@class="btn btn-primary"]' - ).click() - - def setUpDefaultGeoItems(self): - if not settings.SELENIUM_TEST: - return - return GeomaticTest.setUpDefaultGeoItems(self.user) - - def default_geojson(self): - geoms, pks = self.setUpDefaultGeoItems() - return GeomaticTest.geojson_geo_items(geoms, pks) - - def scroll(self, element): - self.selenium.execute_script( - "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);" - + "var elementTop = arguments[0].getBoundingClientRect().top;" - + "window.scrollBy(0, elementTop-(viewPortHeight/2));", - element, - ) - - def wait_and_click(self, xpath): - WebDriverWait(self.selenium, self.waiting_time).until( - EC.visibility_of_element_located((By.XPATH, xpath)) - ) - el = self.selenium.find_element_by_xpath(xpath) - self.scroll(el) - el.click() - - def access_from_dropdown(self, slug): - if slug == "contextrecord": - slug = "record" - if slug == "basefind": - slug = "find" - self.wait_and_click( - '//a[@class="nav-link dropdown-toggle"][@id="dropdown-menu-main"]' - ) - self.selenium.find_element_by_xpath( - '//div[@class="dropdown-menu show"]/a[@href="/' + slug + '_search/"]' - ).click() - - def pks_and_details_from_table(self, slug, source=None): - res = [] - xpath = "//table" - keywords = ["find", "record", "operation"] - for key in keywords: - if key in slug or (source and key in source): - xpath += '[contains(@id, "' + key + '")]' - else: - xpath += '[not(contains(@id, "' + key + '"))]' - xpath += ( - '/tbody/tr/td/a[@class="display_details"][@href="#"][contains(@onclick, "' - + slug - + '")]' - ) - for el in self.selenium.find_elements_by_xpath(xpath): - pk = int(re.findall(r"\d+", el.get_attribute("onclick"))[-1]) - res.append([pk, xpath + '[contains(@onclick, "' + str(pk) + '")]']) - return res - - def label_from_internal_id(self, slug_pk): - xpath = '//div[contains(@id, "' - if slug_pk[:3] == "ope": - xpath += slug_pk + '")]' + "/div" * 5 - elif slug_pk[:7] == "context": - xpath += slug_pk + '")]' + "/div" * 3 - else: - xpath += slug_pk[4:] + '")]' - xpath += '/p/small[@title="ID interne" or @title="Internal ID"]' - WebDriverWait(self.selenium, self.waiting_time).until( - EC.visibility_of_element_located((By.XPATH, xpath)) - ) - label = self.selenium.find_element_by_xpath(xpath).text - if slug_pk[:3] == "ope": - if label == "2": - label = "Poly" - else: - label = "Pt" - elif slug_pk[:7] == "context": - label = label[1:] - else: - label = label.split("-") - label = label[2] - return label - - def assertNotInDOM(self, xpath): - self.selenium.implicitly_wait(self.waiting_time / 10) - try: - WebDriverWait(self.selenium, self.waiting_time / 10).until( - EC.visibility_of_element_located((By.XPATH, xpath)) - ) - found = True - except: - found = False - self.selenium.implicitly_wait(self.waiting_time) - self.assertFalse(found) - - def assertMap(self, dic_base, slug_pk, features_collecs=None): - dic_base = {"type": "FeatureCollection", "features": [dic_base]} - mapid = None - t = time() - while not mapid and time() - t < self.waiting_time: - mapdivs = self.selenium.find_elements_by_class_name("window-map") - for mapdiv in mapdivs: - if slug_pk in mapdiv.get_attribute("id"): - mapid = mapdiv.get_attribute("id") - break - self.assertIsNotNone(mapid, "Timeout, no mapid found") - id = int(re.search(r"\d+$", mapid).group()) - # TODO: use the map and not the map_features in the js function - if features_collecs: - WebDriverWait(self.selenium, self.waiting_time).until( - EC.visibility_of_element_located( - (By.ID, "http-geo-items-ready-" + slug_pk) - ) - ) - base_features, geo_items_feats = self.selenium.execute_script( - "return get_features_by_id(arguments[0])", mapid - ) - base_features["features"][0]["properties"].pop("name") - base_features_type = self.selenium.execute_script( - "return base_features_type(arguments[0])", mapid - ) - dic_base["features"][0]["properties"] = {"id": id} - self.assertEqual( - base_features_type, dic_base["features"][0]["geometry"]["type"] - ) - self.assertEqual(base_features, dic_base) - if features_collecs: - self.assertEqual(geo_items_feats, features_collecs) diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index b1a7e397d..d06613b19 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -360,7 +360,7 @@ def show_item(model, name, extra_dct=None, model_for_perms=None): date = dct.pop("date") dct["sheet_id"] = f"{name}-{pk}" dct["window_id"] = f"{name}-{pk}-{datetime.datetime.now().strftime('%M%s')}" - if pk.startswith("source-"): + if str(pk).startswith("source-"): return show_source_item(request, pk, model, name, dct, extra_dct) try: item = q.get(pk=pk) |