diff --git a/umap/forms.py b/umap/forms.py index 09d0ab3a..5fb2ed78 100644 --- a/umap/forms.py +++ b/umap/forms.py @@ -25,6 +25,10 @@ class FlatErrorList(ErrorList): return u' — '.join([e for e in self]) +class SendLinkForm(forms.Form): + email = forms.EmailField() + + class UpdateMapPermissionsForm(forms.ModelForm): class Meta: diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index ce0a5ef3..c317ee94 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -1336,11 +1336,23 @@ L.U.Map.include({ data: formData, context: this, callback: function (data) { - let duration = 3000 + let duration = 3000, + alert = { content: L._('Map has been saved!'), level: 'info' } if (!this.options.umap_id) { - duration = 100000 // we want a longer message at map creation (TODO UGLY) + alert.duration = 100000 // we want a longer message at map creation (TODO UGLY) this.options.umap_id = data.id this.permissions.setOptions(data.permissions) + if (data.permissions && data.permissions.anonymous_edit_url) { + alert.actions = [ + { + label: L._('Send me edit link by email'), + callback: function () { + this.sendEditLink() + }, + callbackContext: this, + }, + ] + } } else if (!this.permissions.isDirty) { // Do not override local changes to permissions, // but update in case some other editors changed them in the meantime. @@ -1350,11 +1362,10 @@ L.U.Map.include({ if (history && history.pushState) history.pushState({}, this.options.name, data.url) else window.location = data.url - if (data.info) msg = data.info - else msg = L._('Map has been saved!') + alert.content = data.info || alert.content this.once('saved', function () { this.isDirty = false - this.ui.alert({ content: msg, level: 'info', duration: duration }) + this.ui.alert(alert) }) this.ui.closePanel() this.permissions.save() @@ -1362,6 +1373,12 @@ L.U.Map.include({ }) }, + sendEditLink: function () { + var url = L.Util.template(this.options.urls.map_send_edit_link, { + map_id: this.options.umap_id, + }) + }, + getEditUrl: function () { return L.Util.template(this.options.urls.map_update, { map_id: this.options.umap_id, diff --git a/umap/urls.py b/umap/urls.py index a6bcfab4..33ffccfb 100644 --- a/umap/urls.py +++ b/umap/urls.py @@ -111,6 +111,11 @@ i18n_urls += decorated_patterns( views.UpdateMapPermissions.as_view(), name="map_update_permissions", ), + re_path( + r"^map/(?P[\d]+)/send-edit-link/$", + views.SendEditLink.as_view(), + name="map_send_edit_link", + ), re_path( r"^map/(?P[\d]+)/update/owner/$", views.AttachAnonymousMap.as_view(), diff --git a/umap/views.py b/umap/views.py index ed242369..22ef4111 100644 --- a/umap/views.py +++ b/umap/views.py @@ -622,6 +622,28 @@ class AttachAnonymousMap(View): return simple_json_response() +class SendEditLink(FormLessEditMixin, PermissionsMixin, UpdateView): + model = Map + pk_url_kwarg = 'map_id' + + def form_valid(self, form): + if (self.object.owner + or not self.object.is_anonymous_owner(self.request) + or not self.object.can_edit(self.request.user, self.request)): + return HttpResponseForbidden() + email = form.cleaned_data["email"] + from django.core.mail import send_mail + + send_mail( + _('Your secret edit link'), + _('Here is your secret edit link: %(link)s' % {"link": link}), + settings.FROM_EMAIL, + [email], + fail_silently=False, + ) + return simple_json_response(info=_("Email sent to %(email)s" % {"email": email})) + + class MapDelete(DeleteView): model = Map pk_url_kwarg = "map_id"