forked from osm-fr/osmose-frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditor.py
132 lines (107 loc) · 4.73 KB
/
editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#! /usr/bin/env python
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Frédéric Rodrigo 2014 ##
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
###########################################################################
import StringIO, urllib2
from bottle import post, request, abort
from tools import OsmSax
from tools import utils, oauth
@post('/editor/save')
def save(db, lang):
json = request.json
if not json.has_key('tag'):
abort(422)
# Changeset tags
tags = json['tag']
if not tags.has_key('comment') or tags['comment'].strip() == '':
tags['comment'] = u'Fix with Osmose'
if not tags.has_key('source') or tags['source'].strip() == '':
tags['source'] = u'Osmose'
if not tags.has_key('type') or tags['type'].strip() == '':
tags['type'] = u'fix'
tags['created_by'] = u'Osmose Editor'
reuse_changeset = json.get('reuse_changeset', True) != False
# Get an open changeset
changeset = request.session.get('changeset')
if changeset and not reuse_changeset:
_changeset_close(changeset)
changeset = None
del request.session['changeset']
elif changeset:
try:
_changeset_update(changeset, tags)
except:
changeset = None
request.session['changeset'] = changeset
if not changeset:
changeset = _changeset_create(tags)
request.session['changeset'] = changeset
# OsmChange
out = StringIO.StringIO()
o = OsmSax.OsmSaxWriter(out, "UTF-8")
o.startDocument()
o.startElement('osmChange', {"version": "0.6", "generator": "OsmSax"})
methode = {'node': o.NodeCreate, 'way': o.WayCreate, 'relation': o.RelationCreate}
for action in ('modify', 'delete'):
if json.has_key(action) and len(json[action]) > 0:
o.startElement(action, {})
for (k, e) in json[action].items():
try:
ee = utils.fetch_osm_elem(e['type'], e["id"])
except:
ee = None
if ee and ee['version'] == int(e['version']):
ee[u'changeset'] = changeset
ee['tag'] = e['tag']
methode[e['type']](ee)
else:
# FIXME reject
pass
o.endElement(action)
o.endElement('osmChange')
osmchange = out.getvalue()
# Fire the changeset
_changeset_upload(changeset, osmchange)
def _osm_changeset(tags, id='0'):
out = StringIO.StringIO()
o = OsmSax.OsmSaxWriter(out, "UTF-8")
o.startDocument()
o.startElement('osm', {"version": "0.6", "generator": u"Osmose"})
o.startElement('changeset', {"id": id, "open": "false"})
for (k,v) in tags.items():
o.Element('tag', {'k': k, 'v': v})
o.endElement('changeset')
o.endElement('osm')
return out.getvalue()
def _changeset_create(tags):
changeset = oauth.put(request.session['oauth_tokens'],
utils.remote_url_write + 'api/0.6/changeset/create',
_osm_changeset(tags))
return changeset
def _changeset_update(id, tags):
changeset = oauth.put(request.session['oauth_tokens'],
utils.remote_url_write + 'api/0.6/changeset/' + id,
_osm_changeset(tags, id=id))
def _changeset_close(id):
changeset = oauth.put(request.session['oauth_tokens'],
utils.remote_url_write + 'api/0.6/changeset/' + id + '/close')
def _changeset_upload(id, osmchange):
changeset = oauth.post(request.session['oauth_tokens'],
utils.remote_url_write + 'api/0.6/changeset/' + id + '/upload',
osmchange)