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

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

moe cleanups to code and settings

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