From 1a815b313d7f3a941660228b53257e1607810a18 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Sat, 7 Jul 2018 16:44:40 +0200 Subject: [PATCH] Add an explicit button to attach an owner to an anonymous map fix #568 --- umap/models.py | 8 --- umap/static/umap/js/umap.permissions.js | 35 +++++++++---- umap/templates/umap/map_messages.html | 1 - umap/tests/conftest.py | 4 +- umap/tests/test_map_views.py | 68 +++++++++++++++++++------ umap/urls.py | 2 + umap/views.py | 18 ++++++- 7 files changed, 97 insertions(+), 39 deletions(-) diff --git a/umap/models.py b/umap/models.py index 0f7e8f16..0e7812b2 100644 --- a/umap/models.py +++ b/umap/models.py @@ -172,14 +172,6 @@ class Map(NamedModel): if (getattr(settings, "UMAP_ALLOW_ANONYMOUS", False) and self.is_anonymous_owner(request)): can = True - if user and user.is_authenticated: - # TODO: only when using the anonymous-edit URL with an - # authenticated user - # if user is authenticated, attach as owner - self.owner = user - self.save() - msg = _("Your anonymous map has been attached to your account %s" % user) - messages.info(request, msg) if self.edit_status == self.ANONYMOUS: can = True elif not user.is_authenticated: diff --git a/umap/static/umap/js/umap.permissions.js b/umap/static/umap/js/umap.permissions.js index 6f7e7c92..5cd5bb00 100644 --- a/umap/static/umap/js/umap.permissions.js +++ b/umap/static/umap/js/umap.permissions.js @@ -65,19 +65,30 @@ L.U.MapPermissions = L.Class.extend({ var builder = new L.U.FormBuilder(this, fields); var form = builder.build(); container.appendChild(form); + if (this.isAnonymousMap() && this.map.options.user) { + // We have a user, and this user has come through here, so they can edit the map, so let's allow to own the map. + // Note: real check is made on the back office anyway. + var advancedActions = L.DomUtil.createFieldset(container, L._('Advanced actions')); + var advancedButtons = L.DomUtil.create('div', 'button-bar', advancedActions); + var download = L.DomUtil.create('a', 'button', advancedButtons); + download.href = '#'; + download.innerHTML = L._('Attach the map to my account'); + L.DomEvent + .on(download, 'click', L.DomEvent.stop) + .on(download, 'click', this.attach, this); + } this.map.ui.openPanel({data: {html: container}, className: 'dark'}); }, - anonymousMapPanel: function () { - var container = L.DomUtil.create('div'), - fields = [], - title = L.DomUtil.create('h4', '', container); - fields.push(['options.edit_status', {handler: 'IntSelect', label: L._('Who can edit'), selectOptions: this.map.options.edit_statuses}]); - title.innerHTML = L._('Update permissions'); - var builder = new L.U.FormBuilder(this, fields); - var form = builder.build(); - container.appendChild(form); - this.map.ui.openPanel({data: {html: container}, className: 'dark'}); + attach: function () { + this.map.post(this.getAttachUrl(), { + callback: function () { + this.options.owner = this.map.options.user; + this.map.ui.alert({content: L._("Map has been attached to your account"), level: 'info'}); + this.map.ui.closePanel(); + }, + context: this + }) }, save: function () { @@ -106,6 +117,10 @@ L.U.MapPermissions = L.Class.extend({ return L.Util.template(this.map.options.urls.map_update_permissions, {'map_id': this.map.options.umap_id}); }, + getAttachUrl: function () { + return L.Util.template(this.map.options.urls.map_attach_owner, {'map_id': this.map.options.umap_id}); + }, + addOwnerLink: function (element, container) { if (this.options.owner && this.options.owner.name && this.options.owner.url) { var ownerContainer = L.DomUtil.add(element, 'umap-map-owner', container, ' ' + L._('by') + ' '), diff --git a/umap/templates/umap/map_messages.html b/umap/templates/umap/map_messages.html index 5ee2c2fb..646cfd0a 100644 --- a/umap/templates/umap/map_messages.html +++ b/umap/templates/umap/map_messages.html @@ -1,6 +1,5 @@