fix: use the regular delete view instead of ajax

This commit is contained in:
David Larlet 2024-02-07 16:58:02 -05:00
parent bc8679a597
commit 1acadc746a
No known key found for this signature in database
GPG key ID: 3E2953A359E7E7BD
5 changed files with 36 additions and 26 deletions

View file

@ -418,8 +418,7 @@ table.maps thead {
table.maps a:not(.icon-link) {
text-decoration: underline;
}
table.maps button.map-opener,
table.maps button.map-clone {
table.maps button.map-icon {
padding: 0;
border: none;
background: transparent;

View file

@ -20,7 +20,7 @@
</td>
<td>
{{ map_inst.preview_settings|json_script:unique_id }}
<button class="map-opener" data-map-id="{{ unique_id }}"
<button class="map-icon map-opener" data-map-id="{{ unique_id }}"
title="{% translate "Open preview" %}">
<span class="icon-dashboard icon-view"></span>
<span class="sr-only">{% translate "Open preview" %}</span>
@ -58,17 +58,22 @@
</a>
<form action="{% url 'map_clone' map_inst.pk %}" method="post">
{% csrf_token %}
<button class="map-clone" type="submit"
<button class="map-icon" type="submit"
title="{% translate "Clone" %}">
<span class="icon-dashboard icon-duplicate"></span>
<span class="sr-only">{% translate "Clone" %}</span>
</button>
</form>
<a href="{% url 'map_delete' map_inst.pk %}" class="icon-link"
title="{% translate "Delete" %}">
<span class="icon-dashboard icon-delete"></span>
<span class="sr-only">{% translate "Delete" %}</span>
</a>
<form action="{% url 'map_delete' map_inst.pk %}"
method="post" class="map-delete">
{% csrf_token %}
<input type="hidden" name="next" value="{% url 'user_dashboard' %}">
<button class="map-icon" type="submit"
title="{% translate "Delete" %}">
<span class="icon-dashboard icon-delete"></span>
<span class="sr-only">{% translate "Delete" %}</span>
</button>
</form>
</td>
</tr>
{% endwith %}
@ -114,18 +119,12 @@
</span>
</div>
{% endif %}
<script type="module">
<script>
!(function () {
for (const deleteLink of document.querySelectorAll("table.maps a[href$='delete/']")) {
deleteLink.addEventListener('click', async (event) => {
event.preventDefault()
const ui = new L.U.UI(document.querySelector('header'))
const server = new window.umap.ServerRequest(ui)
if (confirm(L._('Are you sure you want to delete this map?'))) {
const [data, response, error] = await server.post(deleteLink.href)
if (response.ok) {
window.location.reload()
}
for (const deleteForm of document.querySelectorAll('table.maps form.map-delete')) {
deleteForm.addEventListener('submit', (event) => {
if (!confirm(L._('Are you sure you want to delete this map?'))) {
event.preventDefault()
}
})
}

View file

@ -12,6 +12,6 @@ def test_owner_can_delete_map_after_confirmation(map, live_server, login):
delete_button = page.get_by_title("Delete")
expect(delete_button).to_be_visible()
page.on("dialog", lambda dialog: dialog.accept())
with page.expect_response(f"/en/map/{map.pk}/update/delete/"):
with page.expect_navigation():
delete_button.click()
assert Map.objects.all().count() == 0

View file

@ -109,7 +109,9 @@ def test_update(client, map, post_data):
def test_delete(client, map, datalayer):
url = reverse("map_delete", args=(map.pk,))
client.login(username=map.owner.username, password="123123")
response = client.post(url, {}, follow=True)
response = client.post(
url, headers={"X-Requested-With": "XMLHttpRequest"}, follow=True
)
assert response.status_code == 200
assert not Map.objects.filter(pk=map.pk).exists()
assert not DataLayer.objects.filter(pk=datalayer.pk).exists()
@ -312,7 +314,9 @@ def test_only_owner_can_delete(client, map, user):
map.editors.add(user)
url = reverse("map_delete", kwargs={"map_id": map.pk})
client.login(username=user.username, password="123123")
response = client.post(url, {}, follow=True)
response = client.post(
url, headers={"X-Requested-With": "XMLHttpRequest"}, follow=True
)
assert response.status_code == 403
@ -384,7 +388,9 @@ def test_anonymous_update_with_cookie_should_work(cookieclient, anonymap, post_d
@pytest.mark.usefixtures("allow_anonymous")
def test_anonymous_delete(cookieclient, anonymap):
url = reverse("map_delete", args=(anonymap.pk,))
response = cookieclient.post(url, {}, follow=True)
response = cookieclient.post(
url, headers={"X-Requested-With": "XMLHttpRequest"}, follow=True
)
assert response.status_code == 200
assert not Map.objects.filter(pk=anonymap.pk).count()
# Test response is a json
@ -395,7 +401,9 @@ def test_anonymous_delete(cookieclient, anonymap):
@pytest.mark.usefixtures("allow_anonymous")
def test_no_cookie_cant_delete(client, anonymap):
url = reverse("map_delete", args=(anonymap.pk,))
response = client.post(url, {}, follow=True)
response = client.post(
url, headers={"X-Requested-With": "XMLHttpRequest"}, follow=True
)
assert response.status_code == 403

View file

@ -848,7 +848,11 @@ class MapDelete(DeleteView):
if not self.object.can_delete(self.request.user, self.request):
return HttpResponseForbidden(_("Only its owner can delete the map."))
self.object.delete()
return simple_json_response(redirect="/")
home_url = reverse("home")
if is_ajax(self.request):
return simple_json_response(redirect=home_url)
else:
return HttpResponseRedirect(form.data.get("next") or home_url)
class MapClone(PermissionsMixin, View):