(WIP) Add a button to send edit link by email in anonymous mode

This commit is contained in:
Yohan Boniface 2021-12-29 17:34:11 +01:00
parent 7713281f8c
commit 69bf6593ac
4 changed files with 53 additions and 5 deletions

View file

@ -25,6 +25,10 @@ class FlatErrorList(ErrorList):
return u''.join([e for e in self]) return u''.join([e for e in self])
class SendLinkForm(forms.Form):
email = forms.EmailField()
class UpdateMapPermissionsForm(forms.ModelForm): class UpdateMapPermissionsForm(forms.ModelForm):
class Meta: class Meta:

View file

@ -1336,11 +1336,23 @@ L.U.Map.include({
data: formData, data: formData,
context: this, context: this,
callback: function (data) { callback: function (data) {
let duration = 3000 let duration = 3000,
alert = { content: L._('Map has been saved!'), level: 'info' }
if (!this.options.umap_id) { 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.options.umap_id = data.id
this.permissions.setOptions(data.permissions) 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) { } else if (!this.permissions.isDirty) {
// Do not override local changes to permissions, // Do not override local changes to permissions,
// but update in case some other editors changed them in the meantime. // but update in case some other editors changed them in the meantime.
@ -1350,11 +1362,10 @@ L.U.Map.include({
if (history && history.pushState) if (history && history.pushState)
history.pushState({}, this.options.name, data.url) history.pushState({}, this.options.name, data.url)
else window.location = data.url else window.location = data.url
if (data.info) msg = data.info alert.content = data.info || alert.content
else msg = L._('Map has been saved!')
this.once('saved', function () { this.once('saved', function () {
this.isDirty = false this.isDirty = false
this.ui.alert({ content: msg, level: 'info', duration: duration }) this.ui.alert(alert)
}) })
this.ui.closePanel() this.ui.closePanel()
this.permissions.save() 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 () { getEditUrl: function () {
return L.Util.template(this.options.urls.map_update, { return L.Util.template(this.options.urls.map_update, {
map_id: this.options.umap_id, map_id: this.options.umap_id,

View file

@ -111,6 +111,11 @@ i18n_urls += decorated_patterns(
views.UpdateMapPermissions.as_view(), views.UpdateMapPermissions.as_view(),
name="map_update_permissions", name="map_update_permissions",
), ),
re_path(
r"^map/(?P<map_id>[\d]+)/send-edit-link/$",
views.SendEditLink.as_view(),
name="map_send_edit_link",
),
re_path( re_path(
r"^map/(?P<map_id>[\d]+)/update/owner/$", r"^map/(?P<map_id>[\d]+)/update/owner/$",
views.AttachAnonymousMap.as_view(), views.AttachAnonymousMap.as_view(),

View file

@ -622,6 +622,28 @@ class AttachAnonymousMap(View):
return simple_json_response() 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): class MapDelete(DeleteView):
model = Map model = Map
pk_url_kwarg = "map_id" pk_url_kwarg = "map_id"