azavea/OpenTreeMap — pull request #103 by steventlamb (2013-05-04 19:16:12+00:00)
Title: Fix KML, CSV, Shapefile advanced search exports

Fixes Github Issue #76

Previously exports were only working for basic searches and were
broken for most advanced search filters. They now work for all
advanced search filters. Tested on TreeZilla.

The problem:
The advanced search routine builds up queryset for several models
and exports the queryset.query (raw sql query) to ogr2ogr, which
reruns the queries and exports to the aforementioned formats. The
problem is that Django is mishandling type coercions for certain
database fields that are stored as textual representations of other
values. For example, we store native_status as a varchar, but its
value is usually restricted to "True" or "False", which was being
sent to the querybuilder as True or False, the boolean values. The
other known example is integer values stored as varchars.

The Solution:
After discussing the possibility of enabling type coercion at the
database level, it was decided that a localized changed would be
safer than a global one. So, adding some functions that sanitize
the raw SQL that goes to ogr2ogr to be the correct textual string
instead of the represented value.