From ca872c7f9f4d176f471a2a7a6bedd9a0f698043c Mon Sep 17 00:00:00 2001 From: Olivier Date: Thu, 5 Nov 2015 12:25:52 +0000 Subject: [PATCH 1/2] [feature] list_geom_field option for LeafletGeoAdmin to add a map to the change list --- leaflet/admin.py | 4 + .../leaflet/admin/change_list_geo.html | 99 +++++++++++++++++++ leaflet/templatetags/leaflet_tags.py | 6 +- 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 leaflet/templates/leaflet/admin/change_list_geo.html diff --git a/leaflet/admin.py b/leaflet/admin.py index 484bf992..d9bc4717 100644 --- a/leaflet/admin.py +++ b/leaflet/admin.py @@ -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): """ @@ -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' \ No newline at end of file diff --git a/leaflet/templates/leaflet/admin/change_list_geo.html b/leaflet/templates/leaflet/admin/change_list_geo.html new file mode 100644 index 00000000..84f730db --- /dev/null +++ b/leaflet/templates/leaflet/admin/change_list_geo.html @@ -0,0 +1,99 @@ +{% extends 'admin/change_list.html' %} +{% load leaflet_tags %} +{% load admin_list_leaflet %} + + +{% block stylesheets %} + {{ block.super }} + {% include 'leaflet/css.html' %} + + + +{% endblock %} + +{% block javascripts %} + {{ block.super }} + {% include 'leaflet/js.html' %} +{% endblock %} + + +{% block result_list %} +{% if cl.model_admin.list_geom_field %} + +{% leaflet_map "map" %} + + + + +{% endif %} +{{block.super}} +{% endblock %} \ No newline at end of file diff --git a/leaflet/templatetags/leaflet_tags.py b/leaflet/templatetags/leaflet_tags.py index c17dfe05..ff3f20ed 100644 --- a/leaflet/templatetags/leaflet_tags.py +++ b/leaflet/templatetags/leaflet_tags.py @@ -110,6 +110,10 @@ def leaflet_json_config(): return json.dumps(settings_as_json) +@register.filter +def geom_field(instance, field_name): + return getattr(instance, field_name).json + def _get_plugin_names(plugin_names_from_tag_parameter): """ @@ -137,4 +141,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 \ No newline at end of file From 5dd37375740f40f6d199fee6069ac0dc6b612ce5 Mon Sep 17 00:00:00 2001 From: Olivier Date: Thu, 5 Nov 2015 13:23:44 +0000 Subject: [PATCH 2/2] list_geom_fiel now works with relA__fieldB notation --- leaflet/templatetags/leaflet_tags.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/leaflet/templatetags/leaflet_tags.py b/leaflet/templatetags/leaflet_tags.py index ff3f20ed..c6173713 100644 --- a/leaflet/templatetags/leaflet_tags.py +++ b/leaflet/templatetags/leaflet_tags.py @@ -112,7 +112,23 @@ def leaflet_json_config(): @register.filter def geom_field(instance, field_name): - return getattr(instance, field_name).json + """ + 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' def _get_plugin_names(plugin_names_from_tag_parameter):