Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new leaflet map background configuration #4881

Merged
merged 8 commits into from
Dec 17, 2024

Conversation

robinmolen
Copy link
Contributor

Part of #2173

Changes

Adds a new admin config page/model for Leaflet map backgrounds. The map components can use these new background definitions to display custom backgrounds on the tile layer (for the formio-builder and sdk).

When a map component is fetched (by the formio-builder and/or sdk), the url for the background is fetched and passed along. This allows admins to update the Leaflet map background urls and makes sure that all map components use the new configuration.

This PR also adds some tests for the existing rewrite_for_request map component logic (updating the component, if global config should be used).

Checklist

Check off the items that are completed or not relevant.

  • Impact on features

    • Checked copying a form
    • Checked import/export of a form
    • Config checks in the configuration overview admin page
    • Problem detection in the admin email digest is handled
  • Release management

    • I have labelled the PR as "needs-backport" accordingly
  • I have updated the translations assets (you do NOT need to provide translations)

    • Ran ./bin/makemessages_js.sh
    • Ran ./bin/compilemessages_js.sh
  • Commit hygiene

    • Commit messages refer to the relevant Github issue
    • Commit messages explain the "why" of change, not the how

@robinmolen robinmolen linked an issue Dec 3, 2024 that may be closed by this pull request
@robinmolen robinmolen changed the title Feature/2173 leaflet map background Add new leaflet map background configuration Dec 3, 2024
@robinmolen robinmolen force-pushed the feature/2173-leaflet-map-background branch from 3f7f899 to ad008b4 Compare December 3, 2024 11:48
Copy link

codecov bot commented Dec 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.62%. Comparing base (ac7be79) to head (b58cddf).
Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4881      +/-   ##
==========================================
+ Coverage   96.59%   96.62%   +0.02%     
==========================================
  Files         758      760       +2     
  Lines       25787    25822      +35     
  Branches     3379     3382       +3     
==========================================
+ Hits        24910    24951      +41     
+ Misses        612      606       -6     
  Partials      265      265              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@robinmolen robinmolen requested review from sergei-maertens and vaszig and removed request for sergei-maertens December 3, 2024 13:16
class LeafletMapBackground(models.Model):
identifier = models.SlugField(
_("identifier"),
help_text=_("A unique identifier for the Leaflet map background"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add a max length here, something like max_length=50

)
url = models.URLField(
_("background url"),
help_text=_(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here (max length).



@disable_admin_mfa()
class LeafletMapBackgroundTests(WebTest):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these were added for test coverage?Otherwise they do not make sense (testing the already tested django code).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check, yeah this just tests the creation and pages for the new map object.

The tests aren't particular exciting, but maybe nice as "sanity check" tests :)

identifier=component["backgroundIdentifier"]
)
# Write the background url information
component["url"] = leaflet_map_background.url
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this out from the try block and emphasize only on the line which will cause the exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sounds good. Will do 👍

@robinmolen robinmolen force-pushed the feature/2173-leaflet-map-background branch 2 times, most recently from fa2ddc5 to 5b507f9 Compare December 9, 2024 13:43
@robinmolen
Copy link
Contributor Author

This PR is depended on open-formulieren/formio-builder#197. When the formio-builder PR is merged, the @open-formulieren/formio-builder version in the PR should be updated.

Once that has been done, this PR can be merged

@robinmolen robinmolen force-pushed the feature/2173-leaflet-map-background branch 4 times, most recently from 9636d7d to c5444bc Compare December 16, 2024 08:29
@robinmolen
Copy link
Contributor Author

The formio-builder has been updated. This PR can now be fully reviewed

robinmolen added a commit that referenced this pull request Dec 16, 2024
The map background PR (#4881) will implement this properly.

For the time being, this will fix the issue
@robinmolen robinmolen mentioned this pull request Dec 16, 2024
10 tasks
Comment on lines 85 to 87
const MAP_TILE_LAYERS = jsonScriptToVar('config-MAP_TILE_LAYERS', {
default: [],
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this outside of the method onto the module level - since the data is supposed to already be in the DOM before the JS starts to run, we can evaluate this at load-time rather than doing the same evaluation every time a map gets rendered.

Comment on lines 35 to 36
"lat": "43.23",
"lng": "41.23",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these stored as string or floats with the actual formio-builder? The types say number: https://github.com/open-formulieren/types/blob/5e2e2a219b4280c162c6eeffa6a51e333706bf49/src/formio/components/map.ts#L40

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah check, yeah these should be floats. Changing it 👍

Comment on lines 195 to 204
if component.get("tileLayerIdentifier", False):
try:
tile_layer = MapTileLayer.objects.get(
identifier=component["tileLayerIdentifier"]
)
except MapTileLayer.DoesNotExist:
return None

# Add the tile layer url information
component["tileLayerUrl"] = tile_layer.url
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With some shortcuts and our beloved :walrus: operator

Suggested change
if component.get("tileLayerIdentifier", False):
try:
tile_layer = MapTileLayer.objects.get(
identifier=component["tileLayerIdentifier"]
)
except MapTileLayer.DoesNotExist:
return None
# Add the tile layer url information
component["tileLayerUrl"] = tile_layer.url
if (identifier := component.get("tileLayerIdentifier")) is not None:
tile_layer = MapTileLayer.objects.filter(identifier=identifier).first()
if tile_layer is not None:
# Add the tile layer url information
component["tileLayerUrl"] = tile_layer.url

the fallback to False introduces yet another type to handle (now we have str | None rather than str | None | bool)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay cool. Yeah that objects.filter is also pretty nice

@@ -189,6 +189,20 @@ def build_serializer_field(
class Map(BasePlugin[Component]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may be a good idea to define a MapComponent type that's more specific and includes the extra properties that are component specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah definitely, seeing as this component will get a bit bigger with the other changes..
I've added a MapComponent type and replaced some instances of Component with MapComponent

@@ -1,5 +1,6 @@
[
"config.RichTextColor",
"config.MapTileLayer",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should go in the configuration group, since the unlisted stuff doesn't get displayed to non-superusers, I think

(and that's probably a problem for the rich text colors)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh okay, yeah that would be a big problem..

Copy link
Contributor Author

@robinmolen robinmolen Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now i've left RichTextColors in the unlisted file

_("identifier"),
unique=True,
max_length=50,
help_text=_("A unique identifier for the tile layer"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
help_text=_("A unique identifier for the tile layer"),
help_text=_("A unique identifier for the tile layer."),

url = models.URLField(
_("tile layer url"),
max_length=255,
help_text=_(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need a disclaimer that the tiler layers must use EPSG:28992 (Rijksdriehoek coordinate system), using other systems will probably lead to weird results.

Comment on lines 226 to 235
fields = [
"label",
"identifier",
"url",
]
list_display = [
"label",
"identifier",
"url",
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to favour tuples rather than lists for these properties, because:

  • tuples are immutable
  • lists defined at the module level are mutable and people tend to try nasty things where they dynamically patch the admin class and add stuff (in tests) and then they get surprises 3 months later

In general I avoid using mutable datastructures at the module-level

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this? We'll handle translations close to release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, will do 👍

@robinmolen robinmolen force-pushed the feature/2173-leaflet-map-background branch from c5444bc to b58cddf Compare December 17, 2024 13:17
@sergei-maertens sergei-maertens merged commit 77b340a into master Dec 17, 2024
28 of 32 checks passed
@sergei-maertens sergei-maertens deleted the feature/2173-leaflet-map-background branch December 17, 2024 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kaartmateriaal - Selecteeren eigen achtergrondkaart (PDOK/WMS/WMTS)
3 participants