Package madrona :: Package loadshp :: Module views
[hide private]

Source Code for Module madrona.loadshp.views

 1  from django.http import HttpResponse 
 2  from django.shortcuts import render_to_response 
 3  from django.conf import settings 
 4  from django.utils import simplejson 
 5  from django.template import Context, RequestContext 
 6  from django.template.loader import get_template 
 7  from madrona.common import default_mimetypes as mimetypes 
 8  from madrona.common.utils import kml_errors 
 9  from forms import UploadForm 
10   
11   
12 -def load_single_shp(request):
13 """ 14 GET returns a form to upload a zipped shp 15 POST takes the zip, validates that it is a single-feature poly shp and returns KML 16 """ 17 user = request.user 18 if user.is_anonymous() or not user.is_authenticated(): 19 return HttpResponse('You must be logged in', status=401) 20 21 form = UploadForm() 22 if request.method == 'POST': 23 form = UploadForm(request.POST, request.FILES) 24 25 # Override the defulat form behavior and 26 # only allow single-feature polygon shps 27 form.multi_feature = False 28 form.supported_geomtypes = ['Polygon'] 29 30 if form.is_valid(): 31 layer = form.handle(request.FILES['file_obj'],user) 32 g = layer[0].geom 33 if g.srs: 34 g.transform_to(4326) 35 geoms = [g] 36 t = get_template('loadshp/loadshp.kml') 37 kml = t.render(Context({'username': user.username, 'geoms': geoms})) 38 json = simplejson.dumps({'input_kml': kml, 'status':'success'}) 39 # Jquery Form plugin requires that we wrap this in a textarea 40 # otherwise it mangles the kml 41 return HttpResponse('<textarea>' + json + '</textarea>',mimetype="text/html") 42 else: 43 json = simplejson.dumps({'error_html': form.errors['file_obj'][0], 'status':'errors'}) 44 return HttpResponse('<textarea>' + json + '</textarea>',mimetype="text/html") 45 46 elif request.method == 'GET': 47 return render_to_response('loadshp/upload.html', RequestContext(request,{'form': form,'action':request.path})) 48 49 else: 50 raise Exception("This URL does not support %s requests" % request.method)
51