def form_valid(self, form):
# We make sure to call the parent's form_valid() method because
# it might do some processing (in the case of CreateView, it will
# call form.save() for example).
response = super(GeoTarget, self).form_valid(form)
if self.request.is_ajax():
cons_ids = []
kwargs = {'state_cd': form.cleaned_data['state']}
if form.cleaned_data['primary_only']:
kwargs['is_primary'] = True
# Get all constituent addresses in the state
cons_addrs = ConstituentAddress.objects.filter(**kwargs)
geojson = json.loads(form.cleaned_data['geojson'])
if geojson['type'] == 'FeatureCollection':
# todo: fetch number, but stick to 1st for now
logger.debug('is FeatureCollection')
geojson = geojson['features'][0]['geometry']
# elif geojson['type'] not ['MultiPolygon', 'Polygon']:
poly = GEOSGeometry(json.dumps(geojson))
for con in cons_addrs:
point = Point(y=con.latitude, x=con.longitude)
if poly.contains(point):
cons_ids.append(con.cons_id)
return JsonResponse(cons_ids, safe=False)
else:
return response
评论列表
文章目录