fix: star control was using old post method

Also, only display it if the user is already logged in, given
we do not deal anymore with login during ajax calls.
This commit is contained in:
Yohan Boniface 2024-02-16 10:47:51 +01:00
parent 3f3392952c
commit b3ce523ba2
2 changed files with 39 additions and 13 deletions

View file

@ -401,7 +401,7 @@ U.Map = L.Map.extend({
this._controls.search = new U.SearchControl()
this._controls.embed = new L.Control.Embed(this, this.options.embedOptions)
this._controls.tilelayersChooser = new U.TileLayerChooser(this)
this._controls.star = new U.StarControl(this)
if (this.options.user) this._controls.star = new U.StarControl(this)
this._controls.editinosm = new L.Control.EditInOSM({
position: 'topleft',
widgetOptions: {
@ -468,6 +468,7 @@ U.Map = L.Map.extend({
status = this.options[`${name}Control`]
if (status === false) continue
control = this._controls[name]
if (!control) continue
control.addTo(this)
if (status === undefined || status === null)
L.DomUtil.addClass(control._container, 'display-on-more')
@ -1142,24 +1143,22 @@ U.Map = L.Map.extend({
})
},
star: function () {
star: async function () {
if (!this.options.umap_id)
return this.ui.alert({
content: L._('Please save the map first'),
level: 'error',
})
const url = this.urls.get('map_star', { map_id: this.options.umap_id })
this.post(url, {
context: this,
callback: function (data) {
this.options.starred = data.starred
let msg = data.starred
? L._('Map has been starred')
: L._('Map has been unstarred')
this.ui.alert({ content: msg, level: 'info' })
this.renderControls()
},
})
const [data, response, error] = await this.server.post(url)
if (!error) {
this.options.starred = data.starred
let msg = data.starred
? L._('Map has been starred')
: L._('Map has been unstarred')
this.ui.alert({ content: msg, level: 'info' })
this.renderControls()
}
},
geometry: function () {

View file

@ -0,0 +1,27 @@
import re
import pytest
from playwright.sync_api import expect
from umap.models import Star
pytestmark = pytest.mark.django_db
def test_star_control_is_visible_if_logged_in(map, live_server, page, login, user):
login(user)
assert not Star.objects.count()
page.goto(f"{live_server.url}{map.get_absolute_url()}")
page.get_by_title("More controls").click()
control = page.locator(".leaflet-control-star")
expect(control).to_be_visible()
with page.expect_response(re.compile(".*/star/")):
control.click()
assert Star.objects.count() == 1
def test_no_star_control_if_not_logged_in(map, live_server, page):
page.goto(f"{live_server.url}{map.get_absolute_url()}")
page.get_by_title("More controls").click()
control = page.locator(".leaflet-control-star")
expect(control).to_be_hidden()