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

Source Code for Module madrona.manipulators.views

  1  from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, HttpResponseBadRequest, HttpResponseServerError, HttpResponseForbidden 
  2  from django.template import RequestContext 
  3  from django.shortcuts import get_object_or_404, render_to_response 
  4  from madrona.common import default_mimetypes as mimetypes 
  5  from django.template.loader import render_to_string 
  6   
  7  from madrona.manipulators.manipulators import * 
  8  from django.db import models 
  9   
 10  from django.contrib.gis.geos import * 
 11  from madrona.studyregion.models import StudyRegion 
 12  from django.conf import settings 
 13   
 14  from django.contrib.contenttypes.models import ContentType 
 15  from django.utils import simplejson 
 16  from madrona.common.utils import clean_geometry 
 17   
 18   
19 -def mpaManipulatorList(request, app_name, model_name):
20 ''' 21 Handler for AJAX mpa manipulators request 22 ''' 23 try: 24 model = ContentType.objects.get(app_label=app_name, model=model_name) 25 manipulators = model.model_class().Options.manipulators 26 except Exception, e: 27 return HttpResponse("The following error was reported: '" + e.message + "', while generating manipulator list from application: " + app_name + " and model: " + model_name, status=404) 28 29 manip_text = [(manipulator.Options.name) for manipulator in manipulators] 30 31 return HttpResponse(simplejson.dumps(manip_text))
32
33 -def multi_generic_manipulator_view(request, manipulators):
34 ''' 35 multi_generic_manipulator_view takes a request and a list of manipulators 36 and runs the manipulators in the order provided 37 ''' 38 # conversion jiggery-pokery to get the QueryDict into appropriate kwarg format 39 kwargs = {} 40 for key,val in request.POST.items(): 41 kwargs[str(key)] = str(val) 42 try: 43 submitted = kwargs['target_shape'] 44 except: 45 return HttpResponse("Target shape not provided", status=500) 46 # parse out which manipulators are requested 47 if manipulators: 48 manipulator_list = manipulators.split(',') 49 else: 50 manipulator_list = ['NullManipulator'] 51 52 html_response = '' 53 54 # run the manipulators in the order presented 55 for manipulator in manipulator_list: 56 # try to bind the incoming manipulator string to an existing class 57 manipClass = manipulatorsDict.get(manipulator) 58 if not manipClass: 59 return HttpResponse("Manipulator " + manipulator + " does not exist.", status=404) 60 61 try: 62 # 'GET' requests assume the intent is to get a related parameter-entry form 63 if request.method == 'GET': 64 if manipClass.Form.available: 65 form = manipClass.Form() 66 return render_to_response('common/base_form.html', RequestContext(request,{'form': form})) 67 else: # this manipulator has no form, just error out 68 return HttpResponse("Manipulator " + manipulator + " does not support GET requests.", status=501) 69 70 else: # 'POST' request: run this manipulator 71 if manipClass.Form.available: # validate a related form, if such exists 72 form = manipClass.Form(kwargs) 73 if form.is_valid(): 74 initial_result = form.manipulation 75 else: # invalid parameters - bounce form back to user 76 return HttpResponse(simplejson.dumps({"message": "form is not valid (missing arguments?)", "html": render_to_string('common/base_form.html', {'form': form}, RequestContext(request))})) 77 else: # no form exists - run this manipulator directly, passing the POST params directly as kwargs 78 manip_inst = manipClass(**kwargs) 79 initial_result = manip_inst.manipulate() 80 81 result = ensure_keys(initial_result) 82 new_shape = result['clipped_shape'] 83 84 # put the resulting shape back into the kwargs as the target_shape 85 kwargs['target_shape'] = new_shape.wkt 86 html_response = html_response + result["html"] 87 88 except manipClass.InvalidGeometryException, e: 89 return respond_with_template(e.html, submitted, None, e.success) 90 except manipClass.InternalException, e: 91 return respond_with_template(e.html, submitted, None, e.success) 92 except manipClass.HaltManipulations, e: 93 return respond_with_template(e.html, submitted, None, e.success) 94 except Exception as e: 95 return respond_with_error(message=str(e)) 96 #end manipulator for loop 97 98 #manipulators ran fine and the resulting shape is ready for outbound processing 99 new_shape.transform(settings.GEOMETRY_DB_SRID) 100 new_shape.transform(settings.GEOMETRY_CLIENT_SRID) 101 new_shape = clean_geometry(new_shape) 102 # #we should probably move this static value 20 to a settings variable 103 # # new_shape = new_shape.simplify(20, preserve_topology=True) 104 # new_shape.transform(settings.GEOMETRY_CLIENT_SRID) 105 # new_shape = clean_geometry(new_shape) 106 107 return respond_with_template(html_response, submitted, new_shape, result["success"])
108
109 -def respond_with_template(status_html, submitted, final_shape, success="1"):
110 if final_shape: 111 final_shape_kml = display_kml(final_shape) 112 else: 113 final_shape_kml = '' 114 user_shape = parsekml(submitted) 115 user_shape.srid = settings.GEOMETRY_CLIENT_SRID 116 user_shape.transform(settings.GEOMETRY_DB_SRID) 117 118 return HttpResponse(simplejson.dumps({"html": status_html, "submitted": submitted, "user_shape": user_shape.wkt, "final_shape_kml": final_shape_kml, "success": success}))
119
120 -def respond_with_error(key='unexpected', message=''):
121 status_html = render_to_string(BaseManipulator.Options.html_templates[key], {'MEDIA_URL':settings.MEDIA_URL, 'INTERNAL_MESSAGE': message}) 122 return HttpResponse(simplejson.dumps({"html": status_html, "geojson_clipped": None, "success": "0"}), status=500)
123
124 -def ensure_keys(values):
125 values.setdefault("html", "") 126 values.setdefault("clipped_shape", None) 127 values.setdefault("success", "1") 128 return values
129
130 -def testView(request):
131 trans_geom = StudyRegion.objects.current().geometry 132 133 w = trans_geom.extent[0] 134 s = trans_geom.extent[1] 135 e = trans_geom.extent[2] 136 n = trans_geom.extent[3] 137 138 center_lat = trans_geom.centroid.y 139 center_lon = trans_geom.centroid.x 140 141 target_shape = Polygon(LinearRing([Point(center_lon, center_lat), 142 Point(e, center_lat), 143 Point(e, s), 144 Point(center_lon, s), 145 Point(center_lon, center_lat)])) 146 147 target_shape.set_srid(settings.GEOMETRY_DB_SRID) 148 target_shape.transform(settings.GEOMETRY_CLIENT_SRID) 149 150 new_req = HttpRequest() 151 new_req.method = 'POST' 152 new_req.POST.update({'target_shape':target_shape.wkt, "north":"40", "south":"20", "east":"-117", "west":"-118"}) 153 response = multi_generic_manipulator_view(new_req, 'ClipToStudyRegion,ClipToGraticule') 154 #response = multi_generic_manipulator_view( new_req, 'ClipToStudyRegion' ) 155 return response
156