From 20cea694ffa0c08e71fbe853a57176e3ab3e39d9 Mon Sep 17 00:00:00 2001 From: Ilya Zverev Date: Thu, 26 Mar 2015 11:03:36 +0300 Subject: [PATCH] removed some dependencies, made lxml optional --- server/borders-api.py | 42 +++++++++++++++++++++++++++--------------- www/borders.js | 8 ++++---- www/index.html | 3 +++ 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/server/borders-api.py b/server/borders-api.py index 8337f6d..be50c9f 100755 --- a/server/borders-api.py +++ b/server/borders-api.py @@ -2,10 +2,14 @@ from flask import Flask, g, request, json, jsonify, abort, Response from flask.ext.cors import CORS from flask.ext.compress import Compress -from lxml import etree -from xml.sax.saxutils import quoteattr import psycopg2 +try: + from lxml import etree + LXML = True +except: + LXML = False + TABLE = 'borders' OSM_TABLE = 'osm_borders' OTHER_TABLES = { 'old': 'old_borders' } @@ -94,13 +98,8 @@ def query_small_in_bbox(): @app.route('/tables') def check_osm_table(): - table = request.args.get('table') - if table in OTHER_TABLES: - table = OTHER_TABLES[table] - else: - table = TABLE osm = False - old = False + old = [] try: cur = g.conn.cursor() cur.execute('select osm_id, ST_Area(way), admin_level, name from {} limit 2;'.format(OSM_TABLE)) @@ -108,13 +107,14 @@ def check_osm_table(): osm = True except psycopg2.Error, e: pass - try: - cur.execute('select name, ST_Area(geom), modified, disabled, count_k, cmnt from {} limit 2;'.format(table)) - if cur.rowcount == 2: - old = True - except psycopg2.Error, e: - pass - return jsonify(osm=osm, table=old) + for t, tname in OTHER_TABLES.iteritems(): + try: + cur.execute('select name, ST_Area(geom), modified, disabled, count_k, cmnt from {} limit 2;'.format(tname)) + if cur.rowcount == 2: + old.append(t) + except psycopg2.Error, e: + pass + return jsonify(osm=osm, tables=old, readonly=READONLY) @app.route('/split') def split(): @@ -315,6 +315,8 @@ def draw_hull(): @app.route('/backup') def backup_do(): + if READONLY: + abort(405) cur = g.conn.cursor() cur.execute("SELECT to_char(now(), 'IYYY-MM-DD HH24:MI'), max(backup) from {};".format(BACKUP)) (timestamp, tsmax) = cur.fetchone() @@ -326,6 +328,8 @@ def backup_do(): @app.route('/restore') def backup_restore(): + if READONLY: + abort(405) ts = request.args.get('timestamp') cur = g.conn.cursor() cur.execute('SELECT count(1) from {} where backup = %s;'.format(BACKUP), (ts,)) @@ -420,6 +424,12 @@ def make_osm(): xml = xml + '' return Response(xml, mimetype='application/x-osm+xml') +def quoteattr(value): + value = value.replace('&', '&').replace('>', '>').replace('<', '<') + value = value.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ') + value = value.replace('"', '"') + return '"{}"'.format(value) + def ring_hash(refs): #return json.dumps(refs) return hash(tuple(sorted(refs))) @@ -481,6 +491,8 @@ def bbox_contains(outer, inner): def import_osm(): if READONLY: abort(405) + if not LXML: + return import_error('importing is disabled due to absent lxml library') f = request.files['file'] if not f: return import_error('failed upload') diff --git a/www/borders.js b/www/borders.js index 61604f7..e32d10b 100644 --- a/www/borders.js +++ b/www/borders.js @@ -3,7 +3,7 @@ var STYLE_SELECTED = { stroke: true, color: '#ff3', weight: 3, fill: true, fillO var FILL_TOO_SMALL = '#0f0'; var FILL_TOO_BIG = '#800'; var FILL_ZERO = 'black'; -var OLD_BORDERS_NAME = 'old'; +var OLD_BORDERS_NAME; // filled in checkHasOSM() var map, borders = {}, bordersLayer, selectedId, editing = false; var size_good = 5, size_bad = 100; @@ -32,11 +32,11 @@ function init() { function checkHasOSM() { $.ajax(server + '/tables', { - data: { 'table': OLD_BORDERS_NAME }, success: function(res) { if( res.osm ) $('#osm_actions').css('display', 'block'); - if( res.table ) { + if( res.tables && res.tables.length > 0 ) { + OLD_BORDERS_NAME = res.tables[0]; $('#old_action').css('display', 'block'); $('#josm_old').css('display', 'inline'); } @@ -60,7 +60,7 @@ function updateBorders() { simplified: simplified }); - if( oldBordersLayer != null ) { + if( oldBordersLayer != null && OLD_BORDERS_NAME ) { oldBordersLayer.clearLayers(); $.ajax(server + '/bbox', { data: { diff --git a/www/index.html b/www/index.html index 03a5e12..3f348cf 100644 --- a/www/index.html +++ b/www/index.html @@ -65,6 +65,9 @@
+
+ Статистика +