source: subversion/applications/editors/django/osmeditor/simple/views.py @ 13479

Last change on this file since 13479 was 13479, checked in by crschmidt, 10 years ago

Refactor obj view to use osmparser. All output should be the same. Drop old
get_osm_obj parsing in views.py.
diffstat says: 2 files changed, 45 insertions(+), 119 deletions(-)

File size: 4.2 KB
Line 
1import xml.dom.minidom as m
2from django.conf import settings
3from django import forms
4from django.shortcuts import render_to_response
5from django.http import HttpResponse, HttpResponseRedirect
6try:
7    import httplib2
8except:
9    from third import httplib2
10
11try:
12    from xml.etree.cElementTree import Element, SubElement, tostring
13except:
14    from third.ElementTree import Element, SubElement, tostring
15
16import urllib
17
18from lib import osmparser
19
20def osmparser_obj(type, id, xml=None):
21    if not xml:
22        xml = get_xml_string(type, id)
23    output = osmparser.parseString(xml)
24    return output["%ss" % type][int(id)] 
25
26def get_xml_string(type, id):
27    url = "%s/%s/%s/full" % (settings.OSM_API, type, id)
28    if type == "node":
29        url = url.replace('/full','') # required because the API 404s on node/x/full - wtf?
30    u = urllib.urlopen(url)
31    data = u.read()
32    return data
33
34def edit_osm_obj(type, id, post, session={}):
35    obj = osmparser_obj(type, id)
36    if obj.timestamp != post['timestamp']:
37        raise Exception("Object changed since you started editing it.")
38   
39    changed = False
40    for key in filter(lambda x: x.startswith("delete_"), post.keys()):
41        k = key.replace("delete_key_", "")
42        if k in obj.tags:
43            changed = True
44            del obj.tags[k]
45        else:
46            raise Exception("Huh? %s is not in tags" % k)
47   
48    for key in filter(lambda x: x.startswith("key_"), post.keys()):
49        k = key.replace("key_", "")
50        if k in obj.tags and post[key] != obj.tags[k]:
51            changed = True
52            obj.tags[k] = post[key]
53   
54    for key in filter(lambda x: x.startswith("new_key_"), post.keys()):
55        new_id = key.replace("new_key_", "")
56        kname = "new_key_%s" % new_id
57        vname = "new_value_%s" % new_id
58        if kname in post and vname in post:
59            k = post[kname]
60            v = post[vname]
61            if k and v:
62                obj.tags[k] = v
63                changed = True
64           
65    if not changed: return
66
67    username = post.get('username', None) or session.get("username", None)
68    password = post.get('password', None) or session.get("password", None)
69    if not username or not password:
70        raise Exception("Need username and password")
71    xml = obj.toxml()
72    change_obj(type, id, xml, username, password)
73
74def change_obj(type, id, xml, username, password):   
75    url = "%s/%s/%s" % (settings.OSM_API, type, id)
76    http = httplib2.Http()   
77    http.add_credentials(username, password)
78    resp, content = http.request(url, "PUT", body=xml)   
79    if int(resp.status) != 200:
80        raise Exception("%s, %s \n%s" % (resp.status, url, content))
81
82def load(request, type="node", id=0):
83    if request.method == "POST":
84        edit_osm_obj(type, id, request.POST, request.session)
85        return HttpResponseRedirect("/%s/%s/" % (type, id))   
86    xml = get_xml_string(type, id)
87    obj = osmparser_obj(type, id, xml=xml)
88   
89    return render_to_response("obj.html",{'obj':obj, 'obj_xml': xml,
90        'logged_in': ('username' in request.session)})
91
92def home(request):
93    return render_to_response("home.html", {'logged_in': 'username' in request.session})
94
95class UserForm(forms.Form):
96    email = forms.CharField(max_length=255)
97    password = forms.CharField(widget=forms.PasswordInput)
98
99def login(request):
100    if request.method == "POST":
101        form = UserForm(request.POST)
102        if form.is_valid():
103            username = form.cleaned_data['email']
104            password = form.cleaned_data['password']
105            http = httplib2.Http()   
106            http.add_credentials(username, password)
107            url = "%s/user/details" % (settings.OSM_API)
108            resp, content = http.request(url, "GET")
109            if int(resp.status) == 200:
110                request.session['username'] = username
111                request.session['password'] = password
112                return HttpResponseRedirect("/")   
113    else:
114        form = UserForm()
115    return render_to_response("login.html", {'form': form})   
116
117def api_proxy(request, url):
118    http = httplib2.Http()   
119    url = "%s/%s" % (settings.OSM_API, url)
120    resp, content = http.request(url, "GET")
121    return HttpResponse(content, content_type="application/osm+xml")
Note: See TracBrowser for help on using the repository browser.