diff --git a/umap/models.py b/umap/models.py index 97df55f9..8de2105d 100644 --- a/umap/models.py +++ b/umap/models.py @@ -366,7 +366,7 @@ class DataLayer(NamedModel): } obj["id"] = self.pk obj["permissions"] = {"edit_status": self.edit_status} - obj["allowEdit"] = self.can_edit(user, request) + obj["editMode"] = "advanced" if self.can_edit(user, request) else 'disabled' return obj def clone(self, map_inst=None): diff --git a/umap/static/umap/js/umap.controls.js b/umap/static/umap/js/umap.controls.js index 87baa753..97a6665b 100644 --- a/umap/static/umap/js/umap.controls.js +++ b/umap/static/umap/js/umap.controls.js @@ -275,7 +275,7 @@ L.U.ContinueLineAction = L.U.BaseVertexAction.extend({ // Leaflet.Toolbar doesn't allow twice same toolbar class… L.U.SettingsToolbar = L.Toolbar.Control.extend({ addTo: function (map) { - if (map.options.allowMapEdit === false) return + if (map.options.editMode !== 'advanced') return L.Toolbar.Control.prototype.addTo.call(this, map) }, }) @@ -1467,7 +1467,7 @@ L.U.IframeExporter = L.Evented.extend({ miniMap: false, scrollWheelZoom: false, zoomControl: true, - allowEdit: false, + editMode: 'disabled', moreControl: true, searchControl: null, tilelayersControl: null, diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index 405e3b62..592a2a77 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -15,7 +15,7 @@ L.Map.mergeOptions({ default_interactive: true, default_labelDirection: 'auto', attributionControl: false, - allowEdit: true, + editMode: 'advanced', embedControl: true, zoomControl: true, datalayersControl: true, @@ -103,7 +103,7 @@ L.U.Map.include({ L.Util.setBooleanFromQueryString(this.options, 'moreControl') L.Util.setBooleanFromQueryString(this.options, 'scaleControl') L.Util.setBooleanFromQueryString(this.options, 'miniMap') - L.Util.setBooleanFromQueryString(this.options, 'allowEdit') + L.Util.setBooleanFromQueryString(this.options, 'editMode') L.Util.setBooleanFromQueryString(this.options, 'displayDataBrowserOnLoad') L.Util.setBooleanFromQueryString(this.options, 'displayCaptionOnLoad') L.Util.setBooleanFromQueryString(this.options, 'captionBar') @@ -122,7 +122,7 @@ L.U.Map.include({ if (this.datalayersOnLoad) this.datalayersOnLoad = this.datalayersOnLoad.toString().split(',') - if (L.Browser.ielt9) this.options.allowEdit = false // TODO include ie9 + if (L.Browser.ielt9) this.options.editMode = 'disabled' // TODO include ie9 let editedFeature = null const self = this @@ -235,7 +235,7 @@ L.U.Map.include({ this.isDirty = true this._default_extent = true this.options.name = L._('Untitled map') - this.options.allowEdit = true + this.options.editMode = 'advanced' const datalayer = this.createDataLayer() datalayer.connectToMap() this.enableEdit() @@ -253,7 +253,7 @@ L.U.Map.include({ this.slideshow = new L.U.Slideshow(this, this.options.slideshow) this.permissions = new L.U.MapPermissions(this) this.initCaptionBar() - if (this.options.allowEdit) { + if (this.hasEditMode()) { this.editTools = new L.U.Editable(this) this.ui.on( 'panel:closed panel:open', @@ -292,7 +292,7 @@ L.U.Map.include({ this.helpMenuActions = {} this._controls = {} - if (this.options.allowEdit && !this.options.noControl) { + if (this.hasEditMode() && !this.options.noControl) { new L.U.EditControl(this).addTo(this) new L.U.DrawToolbar({ map: this }).addTo(this) @@ -511,7 +511,7 @@ L.U.Map.include({ else this.ui.closePanel() } - if (!this.options.allowEdit) return + if (!this.hasEditMode()) return /* Edit mode only shortcuts */ if (key === L.U.Keys.E && modifierKey && !this.editEnabled) { @@ -1790,6 +1790,11 @@ L.U.Map.include({ this.fire('edit:disabled') }, + hasEditMode: function () { + return this.options.editMode === 'simple' || this.options.editMode === 'advanced' + }, + + getDisplayName: function () { return this.options.name || L._('Untitled map') }, @@ -1946,7 +1951,7 @@ L.U.Map.include({ items = items.concat(e.relatedTarget.getContextMenuItems(e)) } } - if (this.options.allowEdit) { + if (this.hasEditMode()) { items.push('-') if (this.editEnabled) { if (!this.hasDirty) { diff --git a/umap/static/umap/js/umap.layer.js b/umap/static/umap/js/umap.layer.js index 1140a47a..ff159888 100644 --- a/umap/static/umap/js/umap.layer.js +++ b/umap/static/umap/js/umap.layer.js @@ -356,7 +356,7 @@ L.U.DataLayer = L.Evented.extend({ // So do not let all options to be reset // Fix is a proper migration so all datalayers settings are // in DB, and we remove it from geojson flat files. - geojson['_umap_options']['allowEdit'] = this.options.allowEdit + geojson['_umap_options']['editMode'] = this.options.editMode this.fromUmapGeoJSON(geojson) this.backupOptions() this.fire('loaded') @@ -496,7 +496,7 @@ L.U.DataLayer = L.Evented.extend({ }) // No browser cache for owners/editors. - if (this.map.options.allowEdit) url = `${url}?${Date.now()}` + if (this.map.hasEditMode()) url = `${url}?${Date.now()}` return url }, @@ -1194,7 +1194,8 @@ L.U.DataLayer = L.Evented.extend({ }, isReadOnly: function () { - return this.options.allowEdit === false + // isReadOnly must return true if unset + return this.options.editMode === 'disabled' }, save: function () { diff --git a/umap/static/umap/test/_pre.js b/umap/static/umap/test/_pre.js index df561c5d..25323c2c 100644 --- a/umap/static/umap/test/_pre.js +++ b/umap/static/umap/test/_pre.js @@ -190,7 +190,7 @@ function initMap(options) { name: 'name of the map', description: 'The description of the map', locale: 'en', - allowEdit: true, + editMode: 'advanced', moreControl: true, scaleControl: true, miniMap: false, diff --git a/umap/templatetags/umap_tags.py b/umap/templatetags/umap_tags.py index 3e7ae9c0..70bc244e 100644 --- a/umap/templatetags/umap_tags.py +++ b/umap/templatetags/umap_tags.py @@ -37,7 +37,7 @@ def map_fragment(map_instance, **kwargs): 'datalayers': datalayer_data, 'urls': _urls_for_js(), 'STATIC_URL': settings.STATIC_URL, - "allowEdit": False, + "editMode": 'disabled', 'hash': False, 'attributionControl': False, 'scrollWheelZoom': False, diff --git a/umap/tests/test_map_views.py b/umap/tests/test_map_views.py index 25d14350..a0f4bdcf 100644 --- a/umap/tests/test_map_views.py +++ b/umap/tests/test_map_views.py @@ -127,9 +127,9 @@ def test_wrong_slug_should_redirect_to_canonical(client, map): def test_wrong_slug_should_redirect_with_query_string(client, map): url = reverse("map", kwargs={"map_id": map.pk, "slug": "wrong-slug"}) - url = "{}?allowEdit=0".format(url) + url = "{}?editMode=simple".format(url) canonical = reverse("map", kwargs={"map_id": map.pk, "slug": map.slug}) - canonical = "{}?allowEdit=0".format(canonical) + canonical = "{}?editMode=simple".format(canonical) response = client.get(url) assert response.status_code == 301 assert response["Location"] == canonical @@ -137,7 +137,7 @@ def test_wrong_slug_should_redirect_with_query_string(client, map): def test_should_not_consider_the_query_string_for_canonical_check(client, map): url = reverse("map", kwargs={"map_id": map.pk, "slug": map.slug}) - url = "{}?allowEdit=0".format(url) + url = "{}?editMode=simple".format(url) response = client.get(url) assert response.status_code == 200 diff --git a/umap/views.py b/umap/views.py index 1bb36493..e58d75ee 100644 --- a/umap/views.py +++ b/umap/views.py @@ -449,10 +449,7 @@ class MapDetailMixin: properties = { "urls": _urls_for_js(), "tilelayers": TileLayer.get_list(), - "allowEdit": self.is_edit_allowed(), # showEditMode - "allowMapEdit": self.object.can_edit(self.request.user, self.request) - if getattr(self, "object", None) - else True, # FIXME naming + "editMode": self.edit_mode, "default_iconUrl": "%sumap/img/marker.png" % settings.STATIC_URL, # noqa "umap_id": self.get_umap_id(), "starred": self.is_starred(), @@ -500,8 +497,9 @@ class MapDetailMixin: def get_datalayers(self): return [] - def is_edit_allowed(self): - return True + @property + def edit_mode(self): + return "advanced" def get_umap_id(self): return None @@ -563,11 +561,17 @@ class MapView(MapDetailMixin, PermissionsMixin, DetailView): for l in self.object.datalayer_set.all() ] - def is_edit_allowed(self): - return self.object.can_edit(self.request.user, self.request) or any( + @property + def edit_mode(self): + edit_mode = 'disabled' + if self.object.can_edit(self.request.user, self.request): + edit_mode = "advanced" + elif any( d.can_edit(self.request.user, self.request) for d in self.object.datalayer_set.all() - ) + ): + edit_mode = "simple" + return edit_mode def get_umap_id(self): return self.object.pk