Пример #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
 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})