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

[feature] add map to admin's change list (WIP - DO NOT MERGE) #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions leaflet/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LeafletGeoAdmin(ModelAdmin):
map_width = '100%'
map_height = '400px'
display_raw = False
list_geom_field = None

def formfield_for_dbfield(self, db_field, **kwargs):
"""
Expand Down Expand Up @@ -55,3 +56,6 @@ class LeafletMap(self.widget):
map_height = self.map_height
display_raw = self.display_raw
return LeafletMap


change_list_template = 'leaflet/admin/change_list_geo.html'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this is a rather radical change, I would do another admin class instead, at least the time to stabilize the whole thing

99 changes: 99 additions & 0 deletions leaflet/templates/leaflet/admin/change_list_geo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{% extends 'admin/change_list.html' %}
{% load leaflet_tags %}
{% load admin_list_leaflet %}
Copy link

Choose a reason for hiding this comment

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

Hi,
This file "admin_list_leaflet" haven’t in the commit and templatetags. Where does it come from? I am interested in this feature, it’s a good idea.
Thanks

Copy link
Author

Choose a reason for hiding this comment

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

Hi,
I'm not sure anymore (didn't look at it for some time), maybe it was a mistake ? did you try to remove that line ?

Copy link

Choose a reason for hiding this comment

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

I just doing the test,

  • I with an error : 'admin_list_leaflet' is not a valid tag library: Template library admin_list_leaflet not found, tried
  • Without no menus and no layer
    nolistmapadmin



{% block stylesheets %}
{{ block.super }}
{% include 'leaflet/css.html' %}

<style>
.leaflet-container-default{
min-width: 300px;
}
</style>

{% endblock %}

{% block javascripts %}
{{ block.super }}
{% include 'leaflet/js.html' %}
{% endblock %}


{% block result_list %}
{% if cl.model_admin.list_geom_field %}

{% leaflet_map "map" %}

<script type="text/javascript">

(function($) {

$('#map').addClass('grp-module');

// GeoJSON object
var geojsonFeatures = {
"type": "FeatureCollection",
"features": [
{% for result in cl.result_list %}
{
"type": "Feature",
"geometry": {{result|geom_field:cl.model_admin.list_geom_field|safe}},
"properties": {"pk": "{{result.pk}}",}
}
{% if not forloop.last %},{% endif %}
{% endfor %}
]
}


window.addEventListener('map:init', function (e) {

var target_map = e.detail.map;


// 1. We create and add the layer to the map
var featuresLayer = L.geoJson(geojsonFeatures, {
onEachFeature: onEachFeature
});
featuresLayer.addTo(target_map);
target_map.fitBounds(featuresLayer.getBounds());

// 2. We hook into _selection_actions checkboxes to provide selection features
$('input[name="_selected_action"]').click(function(){
var id = $(this).val();
var checkbox = $('input[name="_selected_action"][value="'+id+'"]');
var checked = $(checkbox).is(':checked');
featuresLayer.eachLayer(function (layer) {
if(layer.feature.properties.pk == id) {
if(!checked){
layer.setStyle({fillColor :'blue'})
}
else{
layer.setStyle({fillColor :'yellow'})
}
}
});
});
}, false);

// This functions add click and doubleclick handlers to the layer
function onEachFeature(feature, layer) {
layer.on('click',function(){
var checkbox = $('input[name="_selected_action"][value="'+this.feature.properties.pk+'"]');
checkbox.click();
});
layer.on('dblclick',function(){
window.location = "{% url 'admin:index' %}{{cl.opts.app_label}}/{{cl.opts.model_name}}/"+this.feature.properties.pk+'/';
});
}

})(grp.jQuery);

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should try to avoid inline Javascript in templates.

Instead, you could prepare the geojson in the admin class, and put the JS in a separated file, taking some ready-to-use data.

As for the window location, it could be a geojson property computed on the python side too

</script>


{% endif %}
{{block.super}}
{% endblock %}
22 changes: 21 additions & 1 deletion leaflet/templatetags/leaflet_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ def leaflet_json_config():

return json.dumps(settings_as_json)

@register.filter
def geom_field(instance, field_name):
"""
Returns the geojson representation for a model instance given the geometry field name. Works accross relations with the objA__objB notation.
"""
fields = field_name.split("__")
try:
obj = instance
for field in fields:
obj = getattr( obj, field )
geom_field = obj
except AttributeError:
geom_field = None

# geom_field = getattr(instance, field_name)
if geom_field:
return geom_field.json
else:
return 'null'
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would definitely deserve some tests :)



def _get_plugin_names(plugin_names_from_tag_parameter):
"""
Expand Down Expand Up @@ -137,4 +157,4 @@ def _get_all_resources_for_plugins(plugin_names, resource_type):
if plugin_name in PLUGINS:
result.extend(PLUGINS[plugin_name].get(resource_type, []))

return result
return result