From 904336a298a555795137c9feafbd5f52a6e1cd28 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Fri, 6 Nov 2015 17:50:54 +0100 Subject: [PATCH] Added plpythonu function example --- .../example/server_func_example.sql | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 server/lib/python/cartodb_geocoder/example/server_func_example.sql diff --git a/server/lib/python/cartodb_geocoder/example/server_func_example.sql b/server/lib/python/cartodb_geocoder/example/server_func_example.sql new file mode 100644 index 0000000..259069d --- /dev/null +++ b/server/lib/python/cartodb_geocoder/example/server_func_example.sql @@ -0,0 +1,24 @@ +CREATE OR REPLACE +FUNCTION geocode_admin0(search text, tx_id bigint, user_id name) + RETURNS Geometry AS +$$ + import logging + from sys import path + path.append( '/home/ubuntu/www/cartodb-geocoder/server/lib/python/cartodb_geocoder' ) + import quota_service + + LOG_FILENAME = '/tmp/plpython.log' + logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG) + + qs = quota_service.QuotaService(logging, user_id, tx_id) + if qs.check_user_quota(): + result = plpy.execute("SELECT geom FROM geocode_admin0_polygons(Array[\'{0}\']::text[])".format(search)) + logging.debug("Number of rows: {0} --- Status: {1}".format(result.nrows(), result.status())) + if result.status() == 5 and result.nrows() == 1: + qs.increment_georeference_use() + return result[0]["geom"] + else: + raise Exception('Something wrong with the georefence operation') + else: + raise Exception('Not enough quota for this user') +$$ LANGUAGE plpythonu; \ No newline at end of file