예제 #1
0
def postPoint(request, Form):
	"""Submit a user point submission to the database. Normalize geometry and activate push notifications."""
	form = Form(request.POST)
	form.data = form.data.copy()

	# Convert coords to valid geometry
	try:
		form.data['geom'] = normalizeGeometry(form.data['geom'])
	except(ValueError):
		# TODO provide error message to user here
		JsonResponse({'success': False})
		# messages.error(request, '<strong>' + _('Error') + '</strong><br>' + _('No point was selected for this type of report.'))

	# Validate and submit to db
	if form.is_valid():
		point = form.save()
		# Errors with push notifications should not affect reporting
		if not settings.DEBUG:
			try: pushNotification.pushNotification(point)
			except: pass

		return JsonResponse({
			'success': True,
			'point': GeoJSONSerializer().serialize([point,]),
			'point_type': point.p_type,
			'form_html': render_crispy_form(Form())
		})
	else:
		logger.debug("Form not valid")

	# Else: error occurred
	form.data['geom'] = form.data['geom'].json
	form_html = render_crispy_form(form)
	return JsonResponse({'success': False, 'form_html': form_html})
예제 #2
0
파일: restApi.py 프로젝트: SPARLab/BikeMaps
 def post(self, request, format=None):
     serializer = HazardSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         if serializer.data['properties'] is not None:
            if serializer.data['properties']['pk'] is not None:
               hazard = Hazard.objects.get(pk=(serializer.data['properties']['pk']))
               alertUsers(request, hazard)
               # Errors with push notifications should not affect reporting
               try:
                   pushNotification.pushNotification(hazard)
               except:
                   pass
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #3
0
 def post(self, request, format=None):
     serializer = HazardSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         if serializer.data['properties'] is not None:
            if serializer.data['properties']['pk'] is not None:
               hazard = Hazard.objects.get(pk=(serializer.data['properties']['pk']))
               alertUsers(request, hazard)
               # Errors with push notifications should not affect reporting
               try:
                   pushNotification.pushNotification(hazard)
               except:
                   pass
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #4
0
def postPoint(request, Form):
    """Submit a user point submission to the database. Normalize geometry and activate push notifications."""
    form = Form(request.POST)
    form.data = form.data.copy()

    # Convert coords to valid geometry
    try:
        form.data['geom'] = normalizeGeometry(form.data['geom'])
    except (ValueError):
        # TODO provide error message to user here
        JsonResponse({'success': False})
        # messages.error(request, '<strong>' + _('Error') + '</strong><br>' + _('No point was selected for this type of report.'))

    # Validate and submit to db
    if form.is_valid():
        point = form.save()
        # Errors with push notifications should not affect reporting
        if not settings.DEBUG:
            try:
                pushNotification.pushNotification(point)
            except:
                pass

        return JsonResponse({
            'success': True,
            'point': GeoJSONSerializer().serialize([
                point,
            ]),
            'point_type': point.p_type,
            'form_html': render_crispy_form(Form())
        })
    else:
        logger.debug("Form not valid")

    # Else: error occurred
    form.data['geom'] = form.data['geom'].json
    form_html = render_crispy_form(form)
    return JsonResponse({'success': False, 'form_html': form_html})