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..c6173713 100644 --- a/leaflet/templatetags/leaflet_tags.py +++ b/leaflet/templatetags/leaflet_tags.py @@ -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' + def _get_plugin_names(plugin_names_from_tag_parameter): """ @@ -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 \ No newline at end of file